HttpKernelExtension.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\Bridge\Twig\Extension;
  11. use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
  12. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  13. /**
  14. * Provides integration with the HttpKernel component.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class HttpKernelExtension extends \Twig_Extension
  19. {
  20. private $handler;
  21. /**
  22. * Constructor.
  23. *
  24. * @param FragmentHandler $handler A FragmentHandler instance
  25. */
  26. public function __construct(FragmentHandler $handler)
  27. {
  28. $this->handler = $handler;
  29. }
  30. public function getFunctions()
  31. {
  32. return array(
  33. new \Twig_SimpleFunction('render',array($this, 'renderFragment'), array('is_safe' => array('html'))),
  34. new \Twig_SimpleFunction('render_*', array($this, 'renderFragmentStrategy'), array('is_safe' => array('html'))),
  35. new \Twig_SimpleFunction('controller', array($this, 'controller')),
  36. );
  37. }
  38. /**
  39. * Renders a fragment.
  40. *
  41. * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
  42. * @param array $options An array of options
  43. *
  44. * @return string The fragment content
  45. *
  46. * @see Symfony\Component\HttpKernel\Fragment\FragmentHandler::render()
  47. */
  48. public function renderFragment($uri, $options = array())
  49. {
  50. $strategy = isset($options['strategy']) ? $options['strategy'] : 'inline';
  51. unset($options['strategy']);
  52. return $this->handler->render($uri, $strategy, $options);
  53. }
  54. /**
  55. * Renders a fragment.
  56. *
  57. * @param string $strategy A strategy name
  58. * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
  59. * @param array $options An array of options
  60. *
  61. * @return string The fragment content
  62. *
  63. * @see Symfony\Component\HttpKernel\Fragment\FragmentHandler::render()
  64. */
  65. public function renderFragmentStrategy($strategy, $uri, $options = array())
  66. {
  67. return $this->handler->render($uri, $strategy, $options);
  68. }
  69. public function controller($controller, $attributes = array(), $query = array())
  70. {
  71. return new ControllerReference($controller, $attributes, $query);
  72. }
  73. public function getName()
  74. {
  75. return 'http_kernel';
  76. }
  77. }