vendor/symfony/serializer/DependencyInjection/SerializerPass.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Serializer\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Argument\BoundArgument;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  16. /**
  17.  * Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as
  18.  * encoders and normalizers to the "serializer" service.
  19.  *
  20.  * @author Javier Lopez <f12loalf@gmail.com>
  21.  * @author Robin Chalas <robin.chalas@gmail.com>
  22.  */
  23. class SerializerPass implements CompilerPassInterface
  24. {
  25.     use PriorityTaggedServiceTrait;
  26.     private $serializerService;
  27.     private $normalizerTag;
  28.     private $encoderTag;
  29.     public function __construct(string $serializerService 'serializer'string $normalizerTag 'serializer.normalizer'string $encoderTag 'serializer.encoder')
  30.     {
  31.         if (< \func_num_args()) {
  32.             trigger_deprecation('symfony/serializer''5.3''Configuring "%s" is deprecated.'__CLASS__);
  33.         }
  34.         $this->serializerService $serializerService;
  35.         $this->normalizerTag $normalizerTag;
  36.         $this->encoderTag $encoderTag;
  37.     }
  38.     public function process(ContainerBuilder $container)
  39.     {
  40.         if (!$container->hasDefinition($this->serializerService)) {
  41.             return;
  42.         }
  43.         if (!$normalizers $this->findAndSortTaggedServices($this->normalizerTag$container)) {
  44.             throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.'$this->normalizerTag$this->serializerService));
  45.         }
  46.         $serializerDefinition $container->getDefinition($this->serializerService);
  47.         $serializerDefinition->replaceArgument(0$normalizers);
  48.         if (!$encoders $this->findAndSortTaggedServices($this->encoderTag$container)) {
  49.             throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.'$this->encoderTag$this->serializerService));
  50.         }
  51.         $serializerDefinition->replaceArgument(1$encoders);
  52.         if (!$container->hasParameter('serializer.default_context')) {
  53.             return;
  54.         }
  55.         $defaultContext $container->getParameter('serializer.default_context');
  56.         foreach (array_keys(array_merge($container->findTaggedServiceIds($this->normalizerTag), $container->findTaggedServiceIds($this->encoderTag))) as $service) {
  57.             $definition $container->getDefinition($service);
  58.             $definition->setBindings(['array $defaultContext' => new BoundArgument($defaultContextfalse)] + $definition->getBindings());
  59.         }
  60.         $container->getParameterBag()->remove('serializer.default_context');
  61.     }
  62. }