菜谱项目

LazyLoadingFragmentHandler.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. * @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 $requestStack, $debug = false)
  33. {
  34. $this->container = $container;
  35. parent::__construct($requestStack, array(), $debug);
  36. }
  37. /**
  38. * Adds a service as a fragment renderer.
  39. *
  40. * @param string $name The service name
  41. * @param string $renderer The render service id
  42. *
  43. * @deprecated since version 3.3, to be removed in 4.0
  44. */
  45. public function addRendererService($name, $renderer)
  46. {
  47. @trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
  48. $this->rendererIds[$name] = $renderer;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function render($uri, $renderer = 'inline', array $options = array())
  54. {
  55. // BC 3.x, to be removed in 4.0
  56. if (isset($this->rendererIds[$renderer])) {
  57. $this->addRenderer($this->container->get($this->rendererIds[$renderer]));
  58. unset($this->rendererIds[$renderer]);
  59. return parent::render($uri, $renderer, $options);
  60. }
  61. if (!isset($this->initialized[$renderer]) && $this->container->has($renderer)) {
  62. $this->addRenderer($this->container->get($renderer));
  63. $this->initialized[$renderer] = true;
  64. }
  65. return parent::render($uri, $renderer, $options);
  66. }
  67. }