b3562b9d93b4b63bd327a8bb49ed61ca3a8b26c6
[yaffs-website] / serializer / DependencyInjection / SerializerPass.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Serializer\DependencyInjection;
13
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;
18
19 /**
20  * Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as
21  * encoders and normalizers to the "serializer" service.
22  *
23  * @author Javier Lopez <f12loalf@gmail.com>
24  * @author Robin Chalas <robin.chalas@gmail.com>
25  */
26 class SerializerPass implements CompilerPassInterface
27 {
28     use PriorityTaggedServiceTrait;
29
30     private $serializerService;
31     private $normalizerTag;
32     private $encoderTag;
33
34     public function __construct($serializerService = 'serializer', $normalizerTag = 'serializer.normalizer', $encoderTag = 'serializer.encoder')
35     {
36         $this->serializerService = $serializerService;
37         $this->normalizerTag = $normalizerTag;
38         $this->encoderTag = $encoderTag;
39     }
40
41     public function process(ContainerBuilder $container)
42     {
43         if (!$container->hasDefinition($this->serializerService)) {
44             return;
45         }
46
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));
49         }
50
51         $serializerDefinition = $container->getDefinition($this->serializerService);
52         $serializerDefinition->replaceArgument(0, $normalizers);
53
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));
56         }
57
58         $serializerDefinition->replaceArgument(1, $encoders);
59     }
60 }