diff --git a/src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php b/src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php index 1da12aaf5eb10..0e4bf525ccbc9 100644 --- a/src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php @@ -11,7 +11,6 @@ namespace Symfony\Bridge\Twig\Extension; -use Symfony\Component\HttpKernel\Fragment\FragmentHandler; use Symfony\Component\HttpKernel\Controller\ControllerReference; /** @@ -21,62 +20,16 @@ */ class HttpKernelExtension extends \Twig_Extension { - private $handler; - - /** - * Constructor. - * - * @param FragmentHandler $handler A FragmentHandler instance - */ - public function __construct(FragmentHandler $handler) - { - $this->handler = $handler; - } - public function getFunctions() { return array( - new \Twig_SimpleFunction('render', array($this, 'renderFragment'), array('is_safe' => array('html'))), - new \Twig_SimpleFunction('render_*', array($this, 'renderFragmentStrategy'), array('is_safe' => array('html'))), - new \Twig_SimpleFunction('controller', array($this, 'controller')), + new \Twig_SimpleFunction('render', array(HttpKernelRuntime::class, 'renderFragment'), array('is_safe' => array('html'))), + new \Twig_SimpleFunction('render_*', array(HttpKernelRuntime::class, 'renderFragmentStrategy'), array('is_safe' => array('html'))), + new \Twig_SimpleFunction('controller', HttpKernelRuntime::class.'::controller'), ); } - /** - * Renders a fragment. - * - * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance - * @param array $options An array of options - * - * @return string The fragment content - * - * @see FragmentHandler::render() - */ - public function renderFragment($uri, $options = array()) - { - $strategy = isset($options['strategy']) ? $options['strategy'] : 'inline'; - unset($options['strategy']); - - return $this->handler->render($uri, $strategy, $options); - } - - /** - * Renders a fragment. - * - * @param string $strategy A strategy name - * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance - * @param array $options An array of options - * - * @return string The fragment content - * - * @see FragmentHandler::render() - */ - public function renderFragmentStrategy($strategy, $uri, $options = array()) - { - return $this->handler->render($uri, $strategy, $options); - } - - public function controller($controller, $attributes = array(), $query = array()) + public static function controller($controller, $attributes = array(), $query = array()) { return new ControllerReference($controller, $attributes, $query); } diff --git a/src/Symfony/Bridge/Twig/Extension/HttpKernelRuntime.php b/src/Symfony/Bridge/Twig/Extension/HttpKernelRuntime.php new file mode 100644 index 0000000000000..3c1f1a015e10f --- /dev/null +++ b/src/Symfony/Bridge/Twig/Extension/HttpKernelRuntime.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Extension; + +use Symfony\Component\HttpKernel\Fragment\FragmentHandler; +use Symfony\Component\HttpKernel\Controller\ControllerReference; + +/** + * Provides integration with the HttpKernel component. + * + * @author Fabien Potencier + */ +class HttpKernelRuntime +{ + private $handler; + + public function __construct(FragmentHandler $handler) + { + $this->handler = $handler; + } + + /** + * Renders a fragment. + * + * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance + * @param array $options An array of options + * + * @return string The fragment content + * + * @see FragmentHandler::render() + */ + public function renderFragment($uri, $options = array()) + { + $strategy = isset($options['strategy']) ? $options['strategy'] : 'inline'; + unset($options['strategy']); + + return $this->handler->render($uri, $strategy, $options); + } + + /** + * Renders a fragment. + * + * @param string $strategy A strategy name + * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance + * @param array $options An array of options + * + * @return string The fragment content + * + * @see FragmentHandler::render() + */ + public function renderFragmentStrategy($strategy, $uri, $options = array()) + { + return $this->handler->render($uri, $strategy, $options); + } +} diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php index 403ed32fd8015..a583f661d3514 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php @@ -12,6 +12,7 @@ namespace Symfony\Bridge\Twig\Tests\Extension; use Symfony\Bridge\Twig\Extension\HttpKernelExtension; +use Symfony\Bridge\Twig\Extension\HttpKernelRuntime; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Fragment\FragmentHandler; @@ -71,7 +72,13 @@ protected function renderTemplate(FragmentHandler $renderer, $template = '{{ ren { $loader = new \Twig_Loader_Array(array('index' => $template)); $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false)); - $twig->addExtension(new HttpKernelExtension($renderer)); + $twig->addExtension(new HttpKernelExtension()); + + $loader = $this->getMock('Twig_RuntimeLoaderInterface'); + $loader->expects($this->any())->method('load')->will($this->returnValueMap(array( + array('Symfony\Bridge\Twig\Extension\HttpKernelRuntime', new HttpKernelRuntime($renderer)), + ))); + $twig->addRuntimeLoader($loader); return $twig->render('index'); } diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php index 2dc0803d0ee36..a9154ccf14cca 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php @@ -71,5 +71,17 @@ public function process(ContainerBuilder $container) if ($container->has('assets.packages')) { $container->getDefinition('twig.extension.assets')->addTag('twig.extension'); } + + if (class_exists('Symfony\Component\Yaml\Parser')) { + $container->getDefinition('twig.extension.yaml')->addTag('twig.extension'); + } + + if (class_exists('Symfony\Component\Stopwatch\Stopwatch')) { + $container->getDefinition('twig.extension.debug.stopwatch')->addTag('twig.extension'); + } + + if (class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { + $container->getDefinition('twig.extension.expression')->addTag('twig.extension'); + } } } diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index f11445a535ea5..6ff3a7c084e8a 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -95,22 +95,20 @@ - - - + - %kernel.debug% - - - + + + - + + diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php index e5d16c6ced246..11874a27d5b45 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php @@ -230,10 +230,11 @@ public function testRuntimeLoader() $container->compile(); $loader = $container->getDefinition('twig.runtime_loader'); - $this->assertEquals(array( - 'Symfony\Bridge\Twig\Form\TwigRenderer' => 'twig.form.renderer', - 'FooClass' => 'foo', - ), $loader->getArgument(1)); + $args = $loader->getArgument(1); + $this->assertArrayHasKey('Symfony\Bridge\Twig\Form\TwigRenderer', $args); + $this->assertArrayHasKey('FooClass', $args); + $this->assertContains('twig.form.renderer', $args); + $this->assertContains('foo', $args); } private function createContainer()