Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpKernel\DependencyInjection;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Adds services tagged kernel.fragment_renderer as HTTP content rendering strategies.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
22 */
|
Chris@0
|
23 class FragmentRendererPass implements CompilerPassInterface
|
Chris@0
|
24 {
|
Chris@0
|
25 private $handlerService;
|
Chris@0
|
26 private $rendererTag;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * @param string $handlerService Service name of the fragment handler in the container
|
Chris@0
|
30 * @param string $rendererTag Tag name used for fragments
|
Chris@0
|
31 */
|
Chris@0
|
32 public function __construct($handlerService = 'fragment.handler', $rendererTag = 'kernel.fragment_renderer')
|
Chris@0
|
33 {
|
Chris@0
|
34 $this->handlerService = $handlerService;
|
Chris@0
|
35 $this->rendererTag = $rendererTag;
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 public function process(ContainerBuilder $container)
|
Chris@0
|
39 {
|
Chris@0
|
40 if (!$container->hasDefinition($this->handlerService)) {
|
Chris@0
|
41 return;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 $definition = $container->getDefinition($this->handlerService);
|
Chris@0
|
45 foreach ($container->findTaggedServiceIds($this->rendererTag) as $id => $tags) {
|
Chris@0
|
46 $def = $container->getDefinition($id);
|
Chris@0
|
47 if (!$def->isPublic()) {
|
Chris@0
|
48 throw new InvalidArgumentException(sprintf('The service "%s" must be public as fragment renderer are lazy-loaded.', $id));
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 if ($def->isAbstract()) {
|
Chris@0
|
52 throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as fragment renderer are lazy-loaded.', $id));
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 $class = $container->getParameterBag()->resolveValue($def->getClass());
|
Chris@0
|
56 $interface = 'Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface';
|
Chris@0
|
57
|
Chris@0
|
58 if (!is_subclass_of($class, $interface)) {
|
Chris@0
|
59 if (!class_exists($class, false)) {
|
Chris@0
|
60 throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 foreach ($tags as $tag) {
|
Chris@0
|
67 $definition->addMethodCall('addRendererService', array($tag['alias'], $id));
|
Chris@0
|
68 }
|
Chris@0
|
69 }
|
Chris@0
|
70 }
|
Chris@0
|
71 }
|