LazyHandlerRegistry.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace JMS\SerializerBundle\Serializer\Handler;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. class LazyHandlerRegistry extends HandlerRegistry
  5. {
  6. private $container;
  7. private $initializedHandlers = array();
  8. public function __construct(ContainerInterface $container, array $handlers = array())
  9. {
  10. parent::__construct($handlers);
  11. $this->container = $container;
  12. }
  13. public function registerHandler($direction, $typeName, $format, $handler)
  14. {
  15. parent::registerHandler($direction, $typeName, $format, $handler);
  16. unset($this->initializedHandlers[$direction][$typeName][$format]);
  17. }
  18. public function getHandler($direction, $typeName, $format)
  19. {
  20. if (isset($this->initializedHandlers[$direction][$typeName][$format])) {
  21. return $this->initializedHandlers[$direction][$typeName][$format];
  22. }
  23. if ( ! isset($this->handlers[$direction][$typeName][$format])) {
  24. return null;
  25. }
  26. $handler = $this->handlers[$direction][$typeName][$format];
  27. if (is_array($handler) && is_string($handler[0]) && $this->container->has($handler[0])) {
  28. $handler[0] = $this->container->get($handler[0]);
  29. }
  30. return $this->initializedHandlers[$direction][$typeName][$format] = $handler;
  31. }
  32. }