vendor/jms/serializer/src/Handler/LazyHandlerRegistry.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace JMS\Serializer\Handler;
  4. use JMS\Serializer\Exception\InvalidArgumentException;
  5. use Psr\Container\ContainerInterface as PsrContainerInterface;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. final class LazyHandlerRegistry extends HandlerRegistry
  8. {
  9.     /**
  10.      * @var PsrContainerInterface|ContainerInterface
  11.      */
  12.     private $container;
  13.     /**
  14.      * @var array
  15.      */
  16.     private $initializedHandlers = [];
  17.     /**
  18.      * @param PsrContainerInterface|ContainerInterface $container
  19.      * @param array $handlers
  20.      */
  21.     public function __construct($container, array $handlers = [])
  22.     {
  23.         if (!$container instanceof PsrContainerInterface && !$container instanceof ContainerInterface) {
  24.             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)));
  25.         }
  26.         parent::__construct($handlers);
  27.         $this->container $container;
  28.     }
  29.     /**
  30.      * {@inheritdoc}
  31.      */
  32.     public function registerHandler(int $directionstring $typeNamestring $format$handler): void
  33.     {
  34.         parent::registerHandler($direction$typeName$format$handler);
  35.         unset($this->initializedHandlers[$direction][$typeName][$format]);
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function getHandler(int $directionstring $typeNamestring $format)
  41.     {
  42.         if (isset($this->initializedHandlers[$direction][$typeName][$format])) {
  43.             return $this->initializedHandlers[$direction][$typeName][$format];
  44.         }
  45.         if (!isset($this->handlers[$direction][$typeName][$format])) {
  46.             return null;
  47.         }
  48.         $handler $this->handlers[$direction][$typeName][$format];
  49.         if (\is_array($handler) && \is_string($handler[0]) && $this->container->has($handler[0])) {
  50.             $handler[0] = $this->container->get($handler[0]);
  51.         }
  52.         return $this->initializedHandlers[$direction][$typeName][$format] = $handler;
  53.     }
  54. }