vendor/jms/serializer/src/JMS/Serializer/Handler/LazyHandlerRegistry.php line 29

Open in your IDE?
  1. <?php
  2. namespace JMS\Serializer\Handler;
  3. use Psr\Container\ContainerInterface as PsrContainerInterface;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. class LazyHandlerRegistry extends HandlerRegistry
  6. {
  7.     private $container;
  8.     private $initializedHandlers = array();
  9.     public function __construct($container, array $handlers = array())
  10.     {
  11.         if (!$container instanceof PsrContainerInterface && !$container instanceof ContainerInterface) {
  12.             throw new \InvalidArgumentException(sprintf('The container must be an instance of %s or %s (%s given).'PsrContainerInterface::class, ContainerInterface::class, \is_object($container) ? \get_class($container) : \gettype($container)));
  13.         }
  14.         parent::__construct($handlers);
  15.         $this->container $container;
  16.     }
  17.     public function registerHandler($direction$typeName$format$handler)
  18.     {
  19.         parent::registerHandler($direction$typeName$format$handler);
  20.         unset($this->initializedHandlers[$direction][$typeName][$format]);
  21.     }
  22.     public function getHandler($direction$typeName$format)
  23.     {
  24.         if (isset($this->initializedHandlers[$direction][$typeName][$format])) {
  25.             return $this->initializedHandlers[$direction][$typeName][$format];
  26.         }
  27.         if (!isset($this->handlers[$direction][$typeName][$format])) {
  28.             return null;
  29.         }
  30.         $handler $this->handlers[$direction][$typeName][$format];
  31.         if (\is_array($handler) && \is_string($handler[0]) && $this->container->has($handler[0])) {
  32.             $handler[0] = $this->container->get($handler[0]);
  33.         }
  34.         return $this->initializedHandlers[$direction][$typeName][$format] = $handler;
  35.     }
  36. }