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\ContainerInterface;
15 use Symfony\Component\HttpFoundation\RequestStack;
16 use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
19 * Lazily loads fragment renderers from the dependency injection container.
21 * @author Fabien Potencier <fabien@symfony.com>
23 class LazyLoadingFragmentHandler extends FragmentHandler
26 private $rendererIds = array();
31 * @param ContainerInterface $container A container
32 * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
33 * @param bool $debug Whether the debug mode is enabled or not
35 public function __construct(ContainerInterface $container, RequestStack $requestStack, $debug = false)
37 $this->container = $container;
39 parent::__construct($requestStack, array(), $debug);
43 * Adds a service as a fragment renderer.
45 * @param string $name The service name
46 * @param string $renderer The render service id
48 public function addRendererService($name, $renderer)
50 $this->rendererIds[$name] = $renderer;
56 public function render($uri, $renderer = 'inline', array $options = array())
58 if (isset($this->rendererIds[$renderer])) {
59 $this->addRenderer($this->container->get($this->rendererIds[$renderer]));
61 unset($this->rendererIds[$renderer]);
64 return parent::render($uri, $renderer, $options);