Skip to content

Commit d4059db

Browse files
Use namespaced Twig
1 parent aa04f35 commit d4059db

File tree

97 files changed

+845
-571
lines changed

Some content is hidden

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

97 files changed

+845
-571
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: 4 additions & 8 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\Twig;
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(Twig $twig)
4339
{
4440
$this->twig = $twig;
4541
}
4642

4743
/**
48-
* @return \Twig_Environment $twig
44+
* @return Twig $twig
4945
*/
5046
protected function getTwigEnvironment()
5147
{
@@ -165,7 +161,7 @@ private function getMetadata($type, $entity)
165161
return false;
166162
}
167163

168-
return !$param->getClass() || $param->getClass()->getName() !== 'Twig_Environment';
164+
return !$param->getClass() || !in_array($param->getClass()->getName(), array('Twig_Environment', 'Twig\Twig'));
169165
});
170166

171167
// format args

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

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

2126
/**
2227
* Command that will validate your template syntax and output encountered errors.
@@ -36,18 +41,13 @@ public function __construct($name = 'lint:twig')
3641
parent::__construct($name);
3742
}
3843

39-
/**
40-
* Sets the twig environment.
41-
*
42-
* @param \Twig_Environment $twig
43-
*/
44-
public function setTwigEnvironment(\Twig_Environment $twig)
44+
public function setTwigEnvironment(Twig $twig)
4545
{
4646
$this->twig = $twig;
4747
}
4848

4949
/**
50-
* @return \Twig_Environment $twig
50+
* @return Twig $twig
5151
*/
5252
protected function getTwigEnvironment()
5353
{
@@ -117,7 +117,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
117117
return $this->display($input, $output, $filesInfo);
118118
}
119119

120-
private function getFilesInfo(\Twig_Environment $twig, array $filenames)
120+
private function getFilesInfo(Twig $twig, array $filenames)
121121
{
122122
$filesInfo = array();
123123
foreach ($filenames as $filename) {
@@ -140,16 +140,16 @@ protected function findFiles($filename)
140140
throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename));
141141
}
142142

143-
private function validate(\Twig_Environment $twig, $template, $file)
143+
private function validate(Twig $twig, $template, $file)
144144
{
145145
$realLoader = $twig->getLoader();
146146
try {
147-
$temporaryLoader = new \Twig_Loader_Array(array((string) $file => $template));
147+
$temporaryLoader = new ArrayLoader(array((string) $file => $template));
148148
$twig->setLoader($temporaryLoader);
149-
$nodeTree = $twig->parse($twig->tokenize(new \Twig_Source($template, (string) $file)));
149+
$nodeTree = $twig->parse($twig->tokenize(new Source($template, (string) $file)));
150150
$twig->compile($nodeTree);
151151
$twig->setLoader($realLoader);
152-
} catch (\Twig_Error $e) {
152+
} catch (Error $e) {
153153
$twig->setLoader($realLoader);
154154

155155
return array('template' => $template, 'file' => $file, 'valid' => false, 'exception' => $e);
@@ -207,7 +207,7 @@ private function displayJson(OutputInterface $output, $filesInfo)
207207
return min($errors, 1);
208208
}
209209

210-
private function renderException(OutputInterface $output, $template, \Twig_Error $exception, $file = null)
210+
private function renderException(OutputInterface $output, $template, Error $exception, $file = null)
211211
{
212212
$line = $exception->getTemplateLine();
213213

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
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;
21+
use Twig\Source;
1822

1923
/**
2024
* TwigDataCollector.
@@ -26,7 +30,7 @@ class TwigDataCollector extends DataCollector implements LateDataCollectorInterf
2630
private $profile;
2731
private $computed;
2832

29-
public function __construct(\Twig_Profiler_Profile $profile)
33+
public function __construct(Profile $profile)
3034
{
3135
$this->profile = $profile;
3236
}
@@ -73,9 +77,9 @@ public function getMacroCount()
7377

7478
public function getHtmlCallGraph()
7579
{
76-
$dumper = new \Twig_Profiler_Dumper_Html();
80+
$dumper = new HtmlDumper();
7781

78-
return new \Twig_Markup($dumper->dump($this->getProfile()), 'UTF-8');
82+
return new Markup($dumper->dump($this->getProfile()), 'UTF-8');
7983
}
8084

8185
public function getProfile()
@@ -96,7 +100,7 @@ private function getComputedData($index)
96100
return $this->computed[$index];
97101
}
98102

99-
private function computeData(\Twig_Profiler_Profile $profile)
103+
private function computeData(Profile $profile)
100104
{
101105
$data = array(
102106
'template_count' => 0,

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

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

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

1720
/**
1821
* Twig extension for the Symfony Asset component.
1922
*
2023
* @author Fabien Potencier <fabien@symfony.com>
2124
*/
22-
class AssetExtension extends \Twig_Extension
25+
class AssetExtension extends AbstractExtension
2326
{
2427
private $packages;
2528
private $foundationExtension;
@@ -40,9 +43,9 @@ public function __construct(Packages $packages, HttpFoundationExtension $foundat
4043
public function getFunctions()
4144
{
4245
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')),
46+
new TwigFunction('asset', array($this, 'getAssetUrl')),
47+
new TwigFunction('asset_version', array($this, 'getAssetVersion')),
48+
new TwigFunction('assets_version', array($this, 'getAssetsVersion'), array('deprecated' => true, 'alternative' => 'asset_version')),
4649
);
4750
}
4851

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

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

1212
namespace Symfony\Bridge\Twig\Extension;
1313

14+
use Twig\Extension\AbstractExtension;
15+
use Twig\Source;
16+
use Twig\TwigFilter;
17+
1418
/**
1519
* Twig extension relate to PHP code and used by the profiler and the default exception templates.
1620
*
1721
* @author Fabien Potencier <fabien@symfony.com>
1822
*/
19-
class CodeExtension extends \Twig_Extension
23+
class CodeExtension extends AbstractExtension
2024
{
2125
private $fileLinkFormat;
2226
private $rootDir;
@@ -42,14 +46,14 @@ public function __construct($fileLinkFormat, $rootDir, $charset)
4246
public function getFilters()
4347
{
4448
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')),
49+
new TwigFilter('abbr_class', array($this, 'abbrClass'), array('is_safe' => array('html'))),
50+
new TwigFilter('abbr_method', array($this, 'abbrMethod'), array('is_safe' => array('html'))),
51+
new TwigFilter('format_args', array($this, 'formatArgs'), array('is_safe' => array('html'))),
52+
new TwigFilter('format_args_as_text', array($this, 'formatArgsAsText')),
53+
new TwigFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))),
54+
new TwigFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))),
55+
new TwigFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
56+
new TwigFilter('file_link', array($this, 'getFileLink')),
5357
);
5458
}
5559

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

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

1823
/**
1924
* Provides integration of the dump() function with Twig.
2025
*
2126
* @author Nicolas Grekas <p@tchwork.com>
2227
*/
23-
class DumpExtension extends \Twig_Extension
28+
class DumpExtension extends AbstractExtension
2429
{
2530
private $cloner;
2631

@@ -32,7 +37,7 @@ public function __construct(ClonerInterface $cloner)
3237
public function getFunctions()
3338
{
3439
return array(
35-
new \Twig_SimpleFunction('dump', array($this, 'dump'), array('is_safe' => array('html'), 'needs_context' => true, 'needs_environment' => true)),
40+
new TwigFunction('dump', array($this, 'dump'), array('is_safe' => array('html'), 'needs_context' => true, 'needs_environment' => true)),
3641
);
3742
}
3843

@@ -46,7 +51,7 @@ public function getName()
4651
return 'dump';
4752
}
4853

49-
public function dump(\Twig_Environment $env, $context)
54+
public function dump(Twig $env, $context)
5055
{
5156
if (!$env->isDebug()) {
5257
return;
@@ -55,7 +60,7 @@ public function dump(\Twig_Environment $env, $context)
5560
if (2 === func_num_args()) {
5661
$vars = array();
5762
foreach ($context as $key => $value) {
58-
if (!$value instanceof \Twig_Template) {
63+
if (!$value instanceof Template) {
5964
$vars[$key] = $value;
6065
}
6166
}

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

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

1414
use Symfony\Component\ExpressionLanguage\Expression;
15+
use Twig\Extension\AbstractExtension;
16+
use Twig\Source;
17+
use Twig\Template;
18+
use Twig\TwigFunction;
1519

1620
/**
1721
* ExpressionExtension gives a way to create Expressions from a template.
1822
*
1923
* @author Fabien Potencier <fabien@symfony.com>
2024
*/
21-
class ExpressionExtension extends \Twig_Extension
25+
class ExpressionExtension extends AbstractExtension
2226
{
2327
/**
2428
* {@inheritdoc}
2529
*/
2630
public function getFunctions()
2731
{
2832
return array(
29-
new \Twig_SimpleFunction('expression', array($this, 'createExpression')),
33+
new TwigFunction('expression', array($this, 'createExpression')),
3034
);
3135
}
3236

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@
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\Extension\AbstractExtension;
18+
use Twig\Source;
19+
use Twig\Template;
20+
use Twig\Token;
21+
use Twig\TwigFilter;
22+
use Twig\TwigFunction;
23+
use Twig\TwigTest;
24+
use Twig\Twig;
1725

1826
/**
1927
* FormExtension extends Twig with form capabilities.
2028
*
2129
* @author Fabien Potencier <fabien@symfony.com>
2230
* @author Bernhard Schussek <bschussek@gmail.com>
2331
*/
24-
class FormExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface
32+
class FormExtension extends AbstractExtension implements \Twig_Extension_InitRuntimeInterface
2533
{
2634
/**
2735
* This property is public so that it can be accessed directly from compiled
@@ -39,7 +47,7 @@ public function __construct(TwigRendererInterface $renderer)
3947
/**
4048
* {@inheritdoc}
4149
*/
42-
public function initRuntime(\Twig_Environment $environment)
50+
public function initRuntime(Twig $environment)
4351
{
4452
$this->renderer->setEnvironment($environment);
4553
}
@@ -61,16 +69,16 @@ public function getTokenParsers()
6169
public function getFunctions()
6270
{
6371
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')),
72+
new TwigFunction('form_enctype', null, array('node_class' => 'Symfony\Bridge\Twig\Node\FormEnctypeNode', 'is_safe' => array('html'), 'deprecated' => true, 'alternative' => 'form_start')),
73+
new TwigFunction('form_widget', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
74+
new TwigFunction('form_errors', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
75+
new TwigFunction('form_label', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
76+
new TwigFunction('form_row', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
77+
new TwigFunction('form_rest', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
78+
new TwigFunction('form', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
79+
new TwigFunction('form_start', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
80+
new TwigFunction('form_end', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
81+
new TwigFunction('csrf_token', array($this, 'renderCsrfToken')),
7482
);
7583
}
7684

@@ -80,7 +88,7 @@ public function getFunctions()
8088
public function getFilters()
8189
{
8290
return array(
83-
new \Twig_SimpleFilter('humanize', array($this, 'humanize')),
91+
new TwigFilter('humanize', array($this, 'humanize')),
8492
);
8593
}
8694

@@ -90,7 +98,7 @@ public function getFilters()
9098
public function getTests()
9199
{
92100
return array(
93-
new \Twig_SimpleTest('selectedchoice', array($this, 'isSelectedChoice')),
101+
new TwigTest('selectedchoice', array($this, 'isSelectedChoice')),
94102
);
95103
}
96104

0 commit comments

Comments
 (0)