LazyLoadingFragmentHandler.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
  14. /**
  15. * Lazily loads fragment renderers from the dependency injection container.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class LazyLoadingFragmentHandler extends FragmentHandler
  20. {
  21. private $container;
  22. private $rendererIds = array();
  23. /**
  24. * Constructor.
  25. *
  26. * @param ContainerInterface $container A container
  27. * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
  28. * @param bool $debug Whether the debug mode is enabled or not
  29. */
  30. public function __construct(ContainerInterface $container, RequestStack $requestStack, $debug = false)
  31. {
  32. $this->container = $container;
  33. parent::__construct($requestStack, array(), $debug);
  34. }
  35. /**
  36. * Adds a service as a fragment renderer.
  37. *
  38. * @param string $renderer The render service id
  39. */
  40. public function addRendererService($name, $renderer)
  41. {
  42. $this->rendererIds[$name] = $renderer;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function render($uri, $renderer = 'inline', array $options = array())
  48. {
  49. if (isset($this->rendererIds[$renderer])) {
  50. $this->addRenderer($this->container->get($this->rendererIds[$renderer]));
  51. unset($this->rendererIds[$renderer]);
  52. }
  53. return parent::render($uri, $renderer, $options);
  54. }
  55. }