make twig extension handle custom template escaping guesser
This commit is contained in:
parent
e94346ed6c
commit
c2c1ed0728
|
@ -1,6 +1,11 @@
|
||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
2.3.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added option to configure a custom template escaping guesser
|
||||||
|
|
||||||
2.2.0
|
2.2.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|
|
@ -119,6 +119,8 @@ class Configuration implements ConfigurationInterface
|
||||||
->fixXmlConfig('path')
|
->fixXmlConfig('path')
|
||||||
->children()
|
->children()
|
||||||
->scalarNode('autoescape')->end()
|
->scalarNode('autoescape')->end()
|
||||||
|
->scalarNode('autoescape_service')->defaultNull()->end()
|
||||||
|
->scalarNode('autoescape_service_method')->defaultNull()->end()
|
||||||
->scalarNode('base_template_class')->example('Twig_Template')->end()
|
->scalarNode('base_template_class')->example('Twig_Template')->end()
|
||||||
->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
|
->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
|
||||||
->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
|
->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
|
||||||
|
|
|
@ -101,8 +101,6 @@ class TwigExtension extends Extension
|
||||||
$config['extensions']
|
$config['extensions']
|
||||||
);
|
);
|
||||||
|
|
||||||
$container->setParameter('twig.options', $config);
|
|
||||||
|
|
||||||
if ($container->getParameter('kernel.debug')) {
|
if ($container->getParameter('kernel.debug')) {
|
||||||
$loader->load('debug.xml');
|
$loader->load('debug.xml');
|
||||||
|
|
||||||
|
@ -110,10 +108,19 @@ class TwigExtension extends Extension
|
||||||
$container->setAlias('debug.templating.engine.twig', 'templating.engine.twig');
|
$container->setAlias('debug.templating.engine.twig', 'templating.engine.twig');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($config['autoescape'])) {
|
if (isset($config['autoescape_service']) && 0 === strpos($config['autoescape_service'], '@') && isset($config['autoescape_service_method'])) {
|
||||||
|
$container->findDefinition('templating.engine.twig')->addMethodCall('setDefaultEscapingStrategy', array(array(new Reference(substr($config['autoescape_service'], 1)), $config['autoescape_service_method'])));
|
||||||
|
|
||||||
|
unset(
|
||||||
|
$config['autoescape_service'],
|
||||||
|
$config['autoescape_service_method']
|
||||||
|
);
|
||||||
|
} elseif (!isset($config['autoescape'])) {
|
||||||
$container->findDefinition('templating.engine.twig')->addMethodCall('setDefaultEscapingStrategy', array(array(new Reference('templating.engine.twig'), 'guessDefaultEscapingStrategy')));
|
$container->findDefinition('templating.engine.twig')->addMethodCall('setDefaultEscapingStrategy', array(array(new Reference('templating.engine.twig'), 'guessDefaultEscapingStrategy')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$container->setParameter('twig.options', $config);
|
||||||
|
|
||||||
$this->addClassesToCompile(array(
|
$this->addClassesToCompile(array(
|
||||||
'Twig_Environment',
|
'Twig_Environment',
|
||||||
'Twig_Extension',
|
'Twig_Extension',
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
<xsd:attribute name="auto-reload" type="xsd:string" />
|
<xsd:attribute name="auto-reload" type="xsd:string" />
|
||||||
<xsd:attribute name="autoescape" type="xsd:string" />
|
<xsd:attribute name="autoescape" type="xsd:string" />
|
||||||
|
<xsd:attribute name="autoescape_service" type="xsd:string" />
|
||||||
|
<xsd:attribute name="autoescape_service_method" type="xsd:string" />
|
||||||
<xsd:attribute name="base-template-class" type="xsd:string" />
|
<xsd:attribute name="base-template-class" type="xsd:string" />
|
||||||
<xsd:attribute name="cache" type="xsd:string" />
|
<xsd:attribute name="cache" type="xsd:string" />
|
||||||
<xsd:attribute name="charset" type="xsd:string" />
|
<xsd:attribute name="charset" type="xsd:string" />
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$container->loadFromExtension('twig', array(
|
||||||
|
'autoescape_service' => '@my_project.some_bundle.template_escaping_guesser',
|
||||||
|
'autoescape_service_method' => 'guess',
|
||||||
|
));
|
|
@ -0,0 +1,3 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$container->loadFromExtension('twig', array());
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" ?>
|
||||||
|
|
||||||
|
<container xmlns="http://symfony.com/schema/dic/services"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:twig="http://symfony.com/schema/dic/twig"
|
||||||
|
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
|
||||||
|
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
|
||||||
|
|
||||||
|
<twig:config autoescape_service="@my_project.some_bundle.template_escaping_guesser" autoescape_service_method="guess" />
|
||||||
|
</container>
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" ?>
|
||||||
|
|
||||||
|
<container xmlns="http://symfony.com/schema/dic/services"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:twig="http://symfony.com/schema/dic/twig"
|
||||||
|
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
|
||||||
|
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
|
||||||
|
|
||||||
|
<twig:config />
|
||||||
|
</container>
|
|
@ -0,0 +1,3 @@
|
||||||
|
twig:
|
||||||
|
autoescape_service: @my_project.some_bundle.template_escaping_guesser
|
||||||
|
autoescape_service_method: guess
|
|
@ -0,0 +1 @@
|
||||||
|
twig:
|
|
@ -83,6 +83,32 @@ class TwigExtensionTest extends TestCase
|
||||||
$this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
|
$this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getFormats
|
||||||
|
*/
|
||||||
|
public function testLoadCustomTemplateEscapingGuesserConfiguration($format)
|
||||||
|
{
|
||||||
|
$container = $this->createContainer();
|
||||||
|
$container->registerExtension(new TwigExtension());
|
||||||
|
$this->loadFromFile($container, 'customTemplateEscapingGuesser', $format);
|
||||||
|
$this->compileContainer($container);
|
||||||
|
|
||||||
|
$this->assertTemplateEscapingGuesserDefinition($container, 'my_project.some_bundle.template_escaping_guesser', 'guess');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getFormats
|
||||||
|
*/
|
||||||
|
public function testLoadDefaultTemplateEscapingGuesserConfiguration($format)
|
||||||
|
{
|
||||||
|
$container = $this->createContainer();
|
||||||
|
$container->registerExtension(new TwigExtension());
|
||||||
|
$this->loadFromFile($container, 'empty', $format);
|
||||||
|
$this->compileContainer($container);
|
||||||
|
|
||||||
|
$this->assertTemplateEscapingGuesserDefinition($container, 'templating.engine.twig', 'guessDefaultEscapingStrategy');
|
||||||
|
}
|
||||||
|
|
||||||
public function testGlobalsWithDifferentTypesAndValues()
|
public function testGlobalsWithDifferentTypesAndValues()
|
||||||
{
|
{
|
||||||
$globals = array(
|
$globals = array(
|
||||||
|
@ -189,4 +215,18 @@ class TwigExtensionTest extends TestCase
|
||||||
|
|
||||||
$loader->load($file.'.'.$format);
|
$loader->load($file.'.'.$format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function assertTemplateEscapingGuesserDefinition(ContainerBuilder $container, $serviceId, $serviceMethod)
|
||||||
|
{
|
||||||
|
$def = $container->getDefinition('templating.engine.twig');
|
||||||
|
|
||||||
|
$this->assertCount(1, $def->getMethodCalls());
|
||||||
|
|
||||||
|
foreach ($def->getMethodCalls() as $call) {
|
||||||
|
if ('setDefaultEscapingStrategy' === $call[0]) {
|
||||||
|
$this->assertSame($serviceId, (string) $call[1][0][0]);
|
||||||
|
$this->assertSame($serviceMethod, $call[1][0][1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue