Drupal investigation

LazyLoadingFragmentHandler.php 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * RequestStack will become required in 3.0.
  27. *
  28. * @param ContainerInterface $container A container
  29. * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
  30. * @param bool $debug Whether the debug mode is enabled or not
  31. */
  32. public function __construct(ContainerInterface $container, $requestStack = null, $debug = false)
  33. {
  34. $this->container = $container;
  35. if ((null !== $requestStack && !$requestStack instanceof RequestStack) || $debug instanceof RequestStack) {
  36. $tmp = $debug;
  37. $debug = $requestStack;
  38. $requestStack = func_num_args() < 3 ? null : $tmp;
  39. @trigger_error('The '.__METHOD__.' method now requires a RequestStack to be given as second argument as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
  40. } elseif (!$requestStack instanceof RequestStack) {
  41. @trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
  42. }
  43. parent::__construct($requestStack, array(), $debug);
  44. }
  45. /**
  46. * Adds a service as a fragment renderer.
  47. *
  48. * @param string $name The service name
  49. * @param string $renderer The render service id
  50. */
  51. public function addRendererService($name, $renderer)
  52. {
  53. $this->rendererIds[$name] = $renderer;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function render($uri, $renderer = 'inline', array $options = array())
  59. {
  60. if (isset($this->rendererIds[$renderer])) {
  61. $this->addRenderer($this->container->get($this->rendererIds[$renderer]));
  62. unset($this->rendererIds[$renderer]);
  63. }
  64. return parent::render($uri, $renderer, $options);
  65. }
  66. }