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\HttpKernel\DependencyInjection;
14 use Symfony\Component\DependencyInjection\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Reference;
16 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19 * Adds services tagged kernel.fragment_renderer as HTTP content rendering strategies.
21 * @author Fabien Potencier <fabien@symfony.com>
23 class FragmentRendererPass implements CompilerPassInterface
25 private $handlerService;
29 * @param string $handlerService Service name of the fragment handler in the container
30 * @param string $rendererTag Tag name used for fragments
32 public function __construct($handlerService = 'fragment.handler', $rendererTag = 'kernel.fragment_renderer')
34 $this->handlerService = $handlerService;
35 $this->rendererTag = $rendererTag;
38 public function process(ContainerBuilder $container)
40 if (!$container->hasDefinition($this->handlerService)) {
44 $definition = $container->getDefinition($this->handlerService);
45 foreach ($container->findTaggedServiceIds($this->rendererTag) as $id => $tags) {
46 $def = $container->getDefinition($id);
47 if (!$def->isPublic()) {
48 throw new \InvalidArgumentException(sprintf('The service "%s" must be public as fragment renderer are lazy-loaded.', $id));
51 if ($def->isAbstract()) {
52 throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as fragment renderer are lazy-loaded.', $id));
55 $class = $container->getParameterBag()->resolveValue($def->getClass());
56 $interface = 'Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface';
58 if (!is_subclass_of($class, $interface)) {
59 if (!class_exists($class, false)) {
60 throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
63 throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
66 foreach ($tags as $tag) {
67 if (!isset($tag['alias'])) {
68 @trigger_error(sprintf('Service "%s" will have to define the "alias" attribute on the "%s" tag as of Symfony 3.0.', $id, $this->rendererTag), E_USER_DEPRECATED);
70 // register the handler as a non-lazy-loaded one
71 $definition->addMethodCall('addRenderer', array(new Reference($id)));
73 $definition->addMethodCall('addRendererService', array($tag['alias'], $id));