Skip to content

Commit b195a3a

Browse files
committed
Merge branch '2.3' into 2.5
* 2.3: No global state for isolated tests and other fixes [TwigBundle] Moved the setting of the default escaping strategy from the Twig engine to the Twig environment [Debug] fix checkip6 [HttpFoundation] fixed error when an IP in the X-Forwarded-For HTTP header contains a port Update the note about origins of the CssSelector component. Use the correct cssselect library name in docblocks.
2 parents 07906c3 + fc10f0d commit b195a3a

File tree

5 files changed

+54
-37
lines changed

5 files changed

+54
-37
lines changed

DependencyInjection/Configuration.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ private function addTwigOptions(ArrayNodeDefinition $rootNode)
124124
$rootNode
125125
->fixXmlConfig('path')
126126
->children()
127-
->scalarNode('autoescape')->end()
127+
->variableNode('autoescape')
128+
->defaultValue(array('Symfony\Bundle\TwigBundle\TwigDefaultEscapingStrategy', 'guess'))
129+
->end()
128130
->scalarNode('autoescape_service')->defaultNull()->end()
129131
->scalarNode('autoescape_service_method')->defaultNull()->end()
130132
->scalarNode('base_template_class')->example('Twig_Template')->end()

DependencyInjection/TwigExtension.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,9 @@ public function load(array $configs, ContainerBuilder $container)
109109
}
110110

111111
if (isset($config['autoescape_service']) && isset($config['autoescape_service_method'])) {
112-
$container->findDefinition('templating.engine.twig')->addMethodCall('setDefaultEscapingStrategy', array(array(new Reference($config['autoescape_service']), $config['autoescape_service_method'])));
113-
114-
unset($config['autoescape_service'], $config['autoescape_service_method']);
115-
} elseif (!isset($config['autoescape'])) {
116-
$container->findDefinition('templating.engine.twig')->addMethodCall('setDefaultEscapingStrategy', array(array(new Reference('templating.engine.twig'), 'guessDefaultEscapingStrategy')));
112+
$config['autoescape'] = array(new Reference($config['autoescape_service']), $config['autoescape_service_method']);
117113
}
114+
unset($config['autoescape_service'], $config['autoescape_service_method']);
118115

119116
$container->setParameter('twig.options', $config);
120117

Tests/DependencyInjection/TwigExtensionTest.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public function testLoadCustomTemplateEscapingGuesserConfiguration($format)
9595
$this->loadFromFile($container, 'customTemplateEscapingGuesser', $format);
9696
$this->compileContainer($container);
9797

98-
$this->assertTemplateEscapingGuesserDefinition($container, 'my_project.some_bundle.template_escaping_guesser', 'guess');
98+
$options = $container->getParameter('twig.options');
99+
$this->assertEquals(array(new Reference('my_project.some_bundle.template_escaping_guesser'), 'guess'), $options['autoescape']);
99100
}
100101

101102
/**
@@ -108,7 +109,8 @@ public function testLoadDefaultTemplateEscapingGuesserConfiguration($format)
108109
$this->loadFromFile($container, 'empty', $format);
109110
$this->compileContainer($container);
110111

111-
$this->assertTemplateEscapingGuesserDefinition($container, 'templating.engine.twig', 'guessDefaultEscapingStrategy');
112+
$options = $container->getParameter('twig.options');
113+
$this->assertEquals(array('Symfony\Bundle\TwigBundle\TwigDefaultEscapingStrategy', 'guess'), $options['autoescape']);
112114
}
113115

114116
public function testGlobalsWithDifferentTypesAndValues()
@@ -219,18 +221,4 @@ private function loadFromFile(ContainerBuilder $container, $file, $format)
219221

220222
$loader->load($file.'.'.$format);
221223
}
222-
223-
private function assertTemplateEscapingGuesserDefinition(ContainerBuilder $container, $serviceId, $serviceMethod)
224-
{
225-
$def = $container->getDefinition('templating.engine.twig');
226-
227-
$this->assertCount(1, $def->getMethodCalls());
228-
229-
foreach ($def->getMethodCalls() as $call) {
230-
if ('setDefaultEscapingStrategy' === $call[0]) {
231-
$this->assertSame($serviceId, (string) $call[1][0][0]);
232-
$this->assertSame($serviceMethod, $call[1][0][1]);
233-
}
234-
}
235-
}
236224
}

TwigDefaultEscapingStrategy.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\TwigBundle;
13+
14+
/**
15+
* @author Fabien Potencier <[email protected]>
16+
*/
17+
class TwigDefaultEscapingStrategy
18+
{
19+
public static function guess($filename)
20+
{
21+
// remove .twig
22+
$filename = substr($filename, 0, -5);
23+
24+
// get the format
25+
$format = substr($filename, strrpos($filename, '.') + 1);
26+
27+
if ('js' === $format) {
28+
return 'js';
29+
}
30+
31+
if ('txt' === $format) {
32+
return false;
33+
}
34+
35+
return 'html';
36+
}
37+
}

TwigEngine.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,21 @@ public function __construct(\Twig_Environment $environment, TemplateNameParserIn
4141
$this->locator = $locator;
4242
}
4343

44+
/**
45+
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Inject the escaping
46+
* strategy on Twig_Environment instead
47+
*/
4448
public function setDefaultEscapingStrategy($strategy)
4549
{
4650
$this->environment->getExtension('escaper')->setDefaultStrategy($strategy);
4751
}
4852

53+
/**
54+
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Use TwigDefaultEscapingStrategy instead.
55+
*/
4956
public function guessDefaultEscapingStrategy($filename)
5057
{
51-
// remove .twig
52-
$filename = substr($filename, 0, -5);
53-
54-
// get the format
55-
$format = substr($filename, strrpos($filename, '.') + 1);
56-
57-
if ('js' === $format) {
58-
return 'js';
59-
}
60-
61-
if ('txt' === $format) {
62-
return false;
63-
}
64-
65-
return 'html';
58+
return TwigDefaultEscapingStrategy::guess($filename);
6659
}
6760

6861
/**

0 commit comments

Comments
 (0)