Skip to content

added Twig runtimes for "critical" Twig extensions #20094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 4 additions & 51 deletions src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bridge\Twig\Extension;

use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
use Symfony\Component\HttpKernel\Controller\ControllerReference;

/**
Expand All @@ -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);
}
Expand Down
64 changes: 64 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/HttpKernelRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <fabien@symfony.com>
*/
class HttpKernelRuntime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As usual: any reason to allow inheritance?

{
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the definition of the container now depends on the existence of a class, we should define a ClassExistenceResource and add such a resource to the container for such patterns (outside the if), so that the debug container is reloaded if you install the class

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's for another PR as this is not the only place where we are doing this in a compiler pass.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #20121

$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');
}
}
}
14 changes: 6 additions & 8 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,20 @@
<argument type="service" id="router" />
</service>

<service id="twig.extension.yaml" class="Symfony\Bridge\Twig\Extension\YamlExtension" public="false">
<tag name="twig.extension" />
</service>
<service id="twig.extension.yaml" class="Symfony\Bridge\Twig\Extension\YamlExtension" public="false" />

<service id="twig.extension.debug.stopwatch" class="Symfony\Bridge\Twig\Extension\StopwatchExtension" public="false">
<tag name="twig.extension" />
<argument type="service" id="debug.stopwatch" on-invalid="ignore" />
<argument>%kernel.debug%</argument>
</service>

<service id="twig.extension.expression" class="Symfony\Bridge\Twig\Extension\ExpressionExtension" public="false">
<tag name="twig.extension" />
</service>
<service id="twig.extension.expression" class="Symfony\Bridge\Twig\Extension\ExpressionExtension" public="false" />

<service id="twig.extension.httpkernel" class="Symfony\Bridge\Twig\Extension\HttpKernelExtension" public="false" />

<service id="twig.extension.httpkernel" class="Symfony\Bridge\Twig\Extension\HttpKernelExtension" public="false">
<service id="twig.runtime.httpkernel" class="Symfony\Bridge\Twig\Extension\HttpKernelRuntime">
<argument type="service" id="fragment.handler" />
<tag name="twig.runtime" />
</service>

<service id="twig.extension.httpfoundation" class="Symfony\Bridge\Twig\Extension\HttpFoundationExtension" public="false">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down