LazyLoadingFragmentHandler.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 Psr\Container\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. /**
  23. * @deprecated since version 3.3, to be removed in 4.0
  24. */
  25. private $rendererIds = array();
  26. private $initialized = array();
  27. /**
  28. * Constructor.
  29. *
  30. * @param ContainerInterface $container A container
  31. * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
  32. * @param bool $debug Whether the debug mode is enabled or not
  33. */
  34. public function __construct(ContainerInterface $container, RequestStack $requestStack, $debug = false)
  35. {
  36. $this->container = $container;
  37. parent::__construct($requestStack, array(), $debug);
  38. }
  39. /**
  40. * Adds a service as a fragment renderer.
  41. *
  42. * @param string $name The service name
  43. * @param string $renderer The render service id
  44. *
  45. * @deprecated since version 3.3, to be removed in 4.0
  46. */
  47. public function addRendererService($name, $renderer)
  48. {
  49. @trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
  50. $this->rendererIds[$name] = $renderer;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function render($uri, $renderer = 'inline', array $options = array())
  56. {
  57. // BC 3.x, to be removed in 4.0
  58. if (isset($this->rendererIds[$renderer])) {
  59. $this->addRenderer($this->container->get($this->rendererIds[$renderer]));
  60. unset($this->rendererIds[$renderer]);
  61. return parent::render($uri, $renderer, $options);
  62. }
  63. if (!isset($this->initialized[$renderer]) && $this->container->has($renderer)) {
  64. $this->addRenderer($this->container->get($renderer));
  65. $this->initialized[$renderer] = true;
  66. }
  67. return parent::render($uri, $renderer, $options);
  68. }
  69. }