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