8000 minor #22962 Use namespaced Twig (nicolas-grekas) · symfony/symfony@36f0050 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 36f0050

Browse files
minor #22962 Use namespaced Twig (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- Use namespaced Twig | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | needs twigphp/Twig#2490 and twigphp/Twig#2491 | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- c3d1262 Use namespaced Twig
2 parents 32f6534 + c3d1262 commit 36f0050

File tree

98 files changed

+773
-576
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+773
-576
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"paragonie/random_compat": "~1.0",
2222
"symfony/polyfill-apcu": "~1.1",
2323
"symfony/polyfill-mbstring": "~1.1",
24-
"twig/twig": "~1.28|~2.0",
24+
"twig/twig": "~1.34|~2.4",
2525
"psr/log": "~1.0"
2626
},
2727
"replace": {

src/Symfony/Bridge/Twig/Command/DebugCommand.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Input\InputOption;
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use Symfony\Component\Console\Output\OutputInterface;
19+
use Twig\Environment;
1920

2021
/**
2122
* Lists twig functions, filters, globals and tests present in the current project.
@@ -34,18 +35,13 @@ public function __construct($name = 'debug:twig')
3435
parent::__construct($name);
3536
}
3637

37-
/**
38-
* Sets the twig environment.
39-
*
40-
* @param \Twig_Environment $twig
41-
*/
42-
public function setTwigEnvironment(\Twig_Environment $twig)
38+
public function setTwigEnvironment(Environment $twig)
4339
{
4440
$this->twig = $twig;
4541
}
4642

4743
/**
48-
* @return \Twig_Environment $twig
44+
* @return Environment $twig
4945
*/
5046
protected function getTwigEnvironment()
5147
{

src/Symfony/Bridge/Twig/Command/LintCommand.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
use Symfony\Component\Console\Input\InputOption;
1818
use Symfony\Component\Console\Output\OutputInterface;
1919
use Symfony\Component\Finder\Finder;
20+
use Twig\Environment;
21+
use Twig\Error\Error;
22+
use Twig\Loader\ArrayLoader;
23+
use Twig\Source;
2024

2125
/**
2226
* Command that will validate your template syntax and output encountered errors.
@@ -36,18 +40,13 @@ public function __construct($name = 'lint:twig')
3640
parent::__construct($name);
3741
}
3842

39-
/**
40-
* Sets the twig environment.
41-
*
42-
* @param \Twig_Environment $twig
43-
*/
44-
public function setTwigEnvironment(\Twig_Environment $twig)
43+
public function setTwigEnvironment(Environment $twig)
4544
{
4645
$this->twig = $twig;
4746
}
4847

4948
/**
50-
* @return \Twig_Environment $twig
49+
* @return Environment $twig
5150
*/
5251
protected function getTwigEnvironment()
5352
{
@@ -117,7 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
117116
return $this->display($input, $output, $filesInfo);
118117
}
119118

120-
private function getFilesInfo(\Twig_Environment $twig, array $filenames)
119+
private function getFilesInfo(Environment $twig, array $filenames)
121120
{
122121
$filesInfo = array();
123122
foreach ($filenames as $filename) {
@@ -140,16 +139,16 @@ protected function findFiles($filename)
140139
throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename));
141140
}
142141

143-
private function validate(\Twig_Environment $twig, $template, $file)
142+
private function validate(Environment $twig, $template, $file)
144143
{
145144
$realLoader = $twig->getLoader();
146145
try {
147-
$temporaryLoader = new \Twig_Loader_Array(array((string) $file => $template));
146+
$temporaryLoader = new ArrayLoader(array((string) $file => $template));
148147
$twig->setLoader($temporaryLoader);
149-
$nodeTree = $twig->parse($twig->tokenize(new \Twig_Source($template, (string) $file)));
148+
$nodeTree = $twig->parse($twig->tokenize(new Source($template, (string) $file)));
150149
$twig->compile($nodeTree);
151150
$twig->setLoader($realLoader);
152-
} catch (\Twig_Error $e) {
151+
} catch (Error $e) {
153152
$twig->setLoader($realLoader);
154153

155154
return array('template' => $template, 'file' => $file, 'valid' => false, 'exception' => $e);
@@ -207,7 +206,7 @@ private function displayJson(OutputInterface $output, $filesInfo)
207206
return min($errors, 1);
208207
}
209208

210-
private function renderException(OutputInterface $output, $template, \Twig_Error $exception, $file = null)
209+
private function renderException(OutputInterface $output, $template, Error $exception, $file = null)
211210
{
212211
$line = $exception->getTemplateLine();
213212

src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
18+
use Twig\Markup;
19+
use Twig\Profiler\Dumper\HtmlDumper;
20+
use Twig\Profiler\Profile;
1821

1922
/**
2023
* TwigDataCollector.
@@ -26,7 +29,7 @@ class TwigDataCollector extends DataCollector implements LateDataCollectorInterf
2629
private $profile;
2730
private $computed;
2831

29-
public function __construct(\Twig_Profiler_Profile $profile)
32+
public function __construct(Profile $profile)
3033
{
3134
$this->profile = $profile;
3235
}
@@ -73,9 +76,9 @@ public function getMacroCount()
7376

7477
public function getHtmlCallGraph()
7578
{
76-
$dumper = new \Twig_Profiler_Dumper_Html();
79+
$dumper = new HtmlDumper();
7780

78-
return new \Twig_Markup($dumper->dump($this->getProfile()), 'UTF-8');
81+
return new Markup($dumper->dump($this->getProfile()), 'UTF-8');
7982
}
8083

8184
public function getProfile()
@@ -96,7 +99,7 @@ private function getComputedData($index)
9699
return $this->computed[$index];
97100
}
98101

99-
private function computeData(\Twig_Profiler_Profile $profile)
102+
private function computeData(Profile $profile)
100103
{
101104
$data = array(
102105
'template_count' => 0,

src/Symfony/Bridge/Twig/Extension/AssetExtension.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
use Symfony\Component\Asset\Packages;
1515
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
16+
use Twig\Extension\AbstractExtension;
17+
use Twig\TwigFunction;
1618

1719
/**
1820
* Twig extension for the Symfony Asset component.
1921
*
2022
* @author Fabien Potencier <fabien@symfony.com>
2123
*/
22-
class AssetExtension extends \Twig_Extension
24+
class AssetExtension extends AbstractExtension
2325
{
2426
private $packages;
2527
private $foundationExtension;
@@ -40,9 +42,9 @@ public function __construct(Packages $packages, HttpFoundationExtension $foundat
4042
public function getFunctions()
4143
{
4244
return array(
43-
new \Twig_SimpleFunction('asset', array($this, 'getAssetUrl')),
44-
new \Twig_SimpleFunction('asset_version', array($this, 'getAssetVersion')),
45-
new \Twig_SimpleFunction('assets_version', array($this, 'getAssetsVersion'), array('deprecated' => true, 'alternative' => 'asset_version')),
45+
new TwigFunction('asset', array($this, 'getAssetUrl')),
46+
new TwigFunction('asset_version', array($this, 'getAssetVersion')),
47+
new TwigFunction('assets_version', array($this, 'getAssetsVersion'), array('deprecated' => true, 'alternative' => 'asset_version')),
4648
);
4749
}
4850

src/Symfony/Bridge/Twig/Extension/CodeExtension.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111

1212
namespace Symfony\Bridge\Twig\Extension;
1313

14+
use Twig\Extension\AbstractExtension;
15+
use Twig\TwigFilter;
16+
1417
/**
1518
* Twig extension relate to PHP code and used by the profiler and the default exception templates.
1619
*
1720
* @author Fabien Potencier <fabien@symfony.com>
1821
*/
19-
class CodeExtension extends \Twig_Extension
22+
class CodeExtension extends AbstractExtension
2023
{
2124
private $fileLinkFormat;
2225
private $rootDir;
@@ -42,14 +45,14 @@ public function __construct($fileLinkFormat, $rootDir, $charset)
4245
public function getFilters()
4346
{
4447
return array(
45-
new \Twig_SimpleFilter('abbr_class', array($this, 'abbrClass'), array('is_safe' => array('html'))),
46-
new \Twig_SimpleFilter('abbr_method', array($this, 'abbrMethod'), array('is_safe' => array('html'))),
47-
new \Twig_SimpleFilter('format_args', array($this, 'formatArgs'), array('is_safe' => array('html'))),
48-
new \Twig_SimpleFilter('format_args_as_text', array($this, 'formatArgsAsText')),
49-
new \Twig_SimpleFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))),
50-
new \Twig_SimpleFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))),
51-
new \Twig_SimpleFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
52-
new \Twig_SimpleFilter('file_link', array($this, 'getFileLink')),
48+
new TwigFilter('abbr_class', array($this, 'abbrClass'), array('is_safe' => array('html'))),
49+
new TwigFilter('abbr_method', array($this, 'abbrMethod'), array('is_safe' => array('html'))),
50+
new TwigFilter('format_args', array($this, 'formatArgs'), array('is_safe' => array('html'))),
51+
new TwigFilter('format_args_as_text', array($this, 'formatArgsAsText')),
52+
new TwigFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))),
53+
new TwigFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))),
54+
new TwigFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
55+
new TwigFilter('file_link', array($this, 'getFileLink')),
5356
);
5457
}
5558

src/Symfony/Bridge/Twig/Extension/DumpExtension.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
use Symfony\Bridge\Twig\TokenParser\DumpTokenParser;
1515
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
1616
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
17+
use Twig\Environment;
18+
use Twig\Extension\AbstractExtension;
19+
use Twig\Template;
20+
use Twig\TwigFunction;
1721

1822
/**
1923
* Provides integration of the dump() function with Twig.
2024
*
2125
* @author Nicolas Grekas <p@tchwork.com>
2226
*/
23-
class DumpExtension extends \Twig_Extension
27+
class DumpExtension extends AbstractExtension
2428
{
2529
private $cloner;
2630

@@ -32,7 +36,7 @@ public function __construct(ClonerInterface $cloner)
3236
public function getFunctions()
3337
{
3438
return array(
35-
new \Twig_SimpleFunction('dump', array($this, 'dump'), array('is_safe' => array('html'), 'needs_context' => true, 'needs_environment' => true)),
39+
new TwigFunction('dump', array($this, 'dump'), array('is_safe' => array('html'), 'needs_context' => true, 'needs_environment' => true)),
3640
);
3741
}
3842

@@ -46,7 +50,7 @@ public function getNam 10000 e()
4650
return 'dump';
4751
}
4852

49-
public function dump(\Twig_Environment $env, $context)
53+
public function dump(Environment $env, $context)
5054
{
5155
if (!$env->isDebug()) {
5256
return;
@@ -55,7 +59,7 @@ public function dump(\Twig_Environment $env, $context)
5559
if (2 === func_num_args()) {
5660
$vars = array();
5761
foreach ($context as $key => $value) {
58-
if (!$value instanceof \Twig_Template) {
62+
if (!$value instanceof Template) {
5963
$vars[$key] = $value;
6064
}
6165
}

src/Symfony/Bridge/Twig/Extension/ExpressionExtension.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@
1212
namespace Symfony\Bridge\Twig\Extension;
1313

1414
use Symfony\Component\ExpressionLanguage\Expression;
15+
use Twig\Extension\AbstractExtension;
16+
use Twig\TwigFunction;
1517

1618
/**
1719
* ExpressionExtension gives a way to create Expressions from a template.
1820
*
1921
* @author Fabien Potencier <fabien@symfony.com>
2022
*/
21-
class ExpressionExtension extends \Twig_Extension
23+
class ExpressionExtension extends AbstractExtension
2224
{
2325
/**
2426
* {@inheritdoc}
2527
*/
2628
public function getFunctions()
2729
{
2830
return array(
29-
new \Twig_SimpleFunction('expression', array($this, 'createExpression')),
31+
new TwigFunction('expression', array($this, 'createExpression')),
3032
);
3133
}
3234

src/Symfony/Bridge/Twig/Extension/FormExtension.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@
1414
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
1515
use Symfony\Bridge\Twig\Form\TwigRendererInterface;
1616
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
17+
use Twig\Environment;
18+
use Twig\Extension\AbstractExtension;
19+
use Twig\Extension\InitRuntimeInterface;
20+
use Twig\TwigFilter;
21+
use Twig\TwigFunction;
22+
use Twig\TwigTest;
1723

1824
/**
1925
* FormExtension extends Twig with form capabilities.
2026
*
2127
* @author Fabien Potencier <fabien@symfony.com>
2228
* @author Bernhard Schussek <bschussek@gmail.com>
2329
*/
24-
class FormExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface
30+
class FormExtension extends AbstractExtension implements InitRuntimeInterface
2531
{
2632
/**
2733
* This property is public so that it can be accessed directly from compiled
@@ -39,7 +45,7 @@ public function __construct(TwigRendererInterface $renderer)
3945
/**
4046
* {@inheritdoc}
4147
*/
42-
public function initRuntime(\Twig_Environment $environment)
48+
public function initRuntime(Environment $environment)
4349
{
4450
$this->renderer->setEnvironment($environment);
4551
}
@@ -61,16 +67,16 @@ public function getTokenParsers()
6167
public function getFunctions()
6268
{
6369
return array(
64-
new \Twig_SimpleFunction('form_enctype', null, array('node_class' => 'Symfony\Bridge\Twig\Node\FormEnctypeNode', 'is_safe' => array('html'), 'deprecated' => true, 'alternative' => 'form_start')),
65-
new \Twig_SimpleFunction('form_widget', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
66-
new \Twig_SimpleFunction('form_errors', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
67-
new \Twig_SimpleFunction('form_label', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
68-
new \Twig_SimpleFunction('form_row', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
69-
new \Twig_SimpleFunction('form_rest', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
70-
new \Twig_SimpleFunction('form', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
71-
new \Twig_SimpleFunction('form_start', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
72-
new \Twig_SimpleFunction('form_end', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
73-
new \Twig_SimpleFunction('csrf_token', array($this, 'renderCsrfToken')),
70+
new TwigFunction('form_enctype', null, array('node_class' => 'Symfony\Bridge\Twig\Node\FormEnctypeNode', 'is_safe' => array('html'), 'deprecated' => true, 'alternative' => 'form_start')),
71+
new TwigFunction('form_widget', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
72+
new TwigFunction('form_errors', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
73+
new TwigFunction('form_label', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
74+
new TwigFunction('form_row', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
75+
new TwigFunction('form_rest', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
76+
new TwigFunction('form', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
77+
new TwigFunction('form_start', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
78+
new TwigFunction('form_end', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
79+
new TwigFunction('csrf_token', array($this, 'renderCsrfToken')),
7480
);
7581
}
7682

@@ -80,7 +86,7 @@ public function getFunctions()
8086
public function getFilters()
8187
{
8288
return array(
83-
new \Twig_SimpleFilter('humanize', array($this, 'humanize')),
89+
new TwigFilter('humanize', array($this, 'humanize')),
8490
);
8591
}
8692

@@ -90,7 +96,7 @@ public function getFilters()
9096
public function getTests()
9197
{
9298
return array(
93-
new \Twig_SimpleTest('selectedchoice', array($this, 'isSelectedChoice')),
99+
new TwigTest('selectedchoice', array($this, 'isSelectedChoice')),
94100
);
95101
}
96102

src/Symfony/Bridge/Twig/Extension/HttpFoundationExtension.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
use Symfony\Component\HttpFoundation\RequestStack;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\Routing\RequestContext;
17+
use Twig\Extension\AbstractExtension;
18+
use Twig\TwigFunction;
1719

1820
/**
1921
* Twig extension for the Symfony HttpFoundation component.
2022
*
2123
* @author Fabien Potencier <fabien@symfony.com>
2224
*/
23-
class HttpFoundationExtension extends \Twig_Extension
25+
class HttpFoundationExtension extends AbstractExtension
2426
{
2527
private $requestStack;
2628
private $requestContext;
@@ -37,8 +39,8 @@ public function __construct(RequestStack $requestStack, RequestContext $requestC
3739
public function getFunctions()
3840
{
3941
return array(
40-
new \Twig_SimpleFunction('absolute_url', array($this, 'generateAbsoluteUrl')),
41-
new \Twig_SimpleFunction('relative_path', array($this, 'generateRelativePath')),
42+
new TwigFunction('absolute_url', array($this, 'generateAbsoluteUrl')),
43+
new TwigFunction('relative_path', array($this, 'generateRelativePath')),
4244
);
4345
}
4446

0 commit comments

Comments
 (0)
0