4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\DependencyInjection;
14 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15 use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
20 * Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as
21 * encoders and normalizers to the "serializer" service.
23 * @author Javier Lopez <f12loalf@gmail.com>
24 * @author Robin Chalas <robin.chalas@gmail.com>
26 class SerializerPass implements CompilerPassInterface
28 use PriorityTaggedServiceTrait;
30 private $serializerService;
31 private $normalizerTag;
34 public function __construct($serializerService = 'serializer', $normalizerTag = 'serializer.normalizer', $encoderTag = 'serializer.encoder')
36 $this->serializerService = $serializerService;
37 $this->normalizerTag = $normalizerTag;
38 $this->encoderTag = $encoderTag;
41 public function process(ContainerBuilder $container)
43 if (!$container->hasDefinition($this->serializerService)) {
47 if (!$normalizers = $this->findAndSortTaggedServices($this->normalizerTag, $container)) {
48 throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->normalizerTag, $this->serializerService));
51 $serializerDefinition = $container->getDefinition($this->serializerService);
52 $serializerDefinition->replaceArgument(0, $normalizers);
54 if (!$encoders = $this->findAndSortTaggedServices($this->encoderTag, $container)) {
55 throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->encoderTag, $this->serializerService));
58 $serializerDefinition->replaceArgument(1, $encoders);