10000 added Twig runtimes for "critical" Twig extensions by fabpot · Pull Request #20094 · symfony/symfony · GitHub
[go: up one dir, main page]

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 1 commit
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
Prev Previous commit
Next Next commit
decoupled the Twig HttpKernelExtension runtime from the extension
  • Loading branch information
fabpot committed Sep 30, 2016
commit 812fbb444f1d1e4cafff5876ce7b22d5b8d4adf8
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
5 changes: 4 additions & 1 deletion src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@

<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
0