Skip to content

Use namespaced Twig #22962

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 1 commit into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"paragonie/random_compat": "~1.0",
"symfony/polyfill-apcu": "~1.1",
"symfony/polyfill-mbstring": "~1.1",
"twig/twig": "~1.28|~2.0",
"twig/twig": "~1.34|~2.4",
"psr/log": "~1.0"
},
"replace": {
Expand Down
10 changes: 3 additions & 7 deletions src/Symfony/Bridge/Twig/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Twig\Environment;

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

/**
* Sets the twig environment.
*
* @param \Twig_Environment $twig
*/
public function setTwigEnvironment(\Twig_Environment $twig)
public function setTwigEnvironment(Environment $twig)
{
$this->twig = $twig;
}

/**
* @return \Twig_Environment $twig
* @return Environment $twig
*/
protected function getTwigEnvironment()
{
Expand Down
25 changes: 12 additions & 13 deletions src/Symfony/Bridge/Twig/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Twig\Environment;
use Twig\Error\Error;
use Twig\Loader\ArrayLoader;
use Twig\Source;

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

/**
* Sets the twig environment.
*
* @param \Twig_Environment $twig
*/
public function setTwigEnvironment(\Twig_Environment $twig)
public function setTwigEnvironment(Environment $twig)
{
$this->twig = $twig;
}

/**
* @return \Twig_Environment $twig
* @return Environment $twig
*/
protected function getTwigEnvironment()
{
Expand Down Expand Up @@ -117,7 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $this->display($input, $output, $filesInfo);
}

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

private function validate(\Twig_Environment $twig, $template, $file)
private function validate(Environment $twig, $template, $file)
{
$realLoader = $twig->getLoader();
try {
$temporaryLoader = new \Twig_Loader_Array(array((string) $file => $template));
$temporaryLoader = new ArrayLoader(array((string) $file => $template));
$twig->setLoader($temporaryLoader);
$nodeTree = $twig->parse($twig->tokenize(new \Twig_Source($template, (string) $file)));
$nodeTree = $twig->parse($twig->tokenize(new Source($template, (string) $file)));
$twig->compile($nodeTree);
$twig->setLoader($realLoader);
} catch (\Twig_Error $e) {
} catch (Error $e) {
$twig->setLoader($realLoader);

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

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

Expand Down
11 changes: 7 additions & 4 deletions src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig\Markup;
use Twig\Profiler\Dumper\HtmlDumper;
use Twig\Profiler\Profile;

/**
* TwigDataCollector.
Expand All @@ -26,7 +29,7 @@ class TwigDataCollector extends DataCollector implements LateDataCollectorInterf
private $profile;
private $computed;

public function __construct(\Twig_Profiler_Profile $profile)
public function __construct(Profile $profile)
{
$this->profile = $profile;
}
Expand Down Expand Up @@ -73,9 +76,9 @@ public function getMacroCount()

public function getHtmlCallGraph()
{
$dumper = new \Twig_Profiler_Dumper_Html();
$dumper = new HtmlDumper();

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

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

private function computeData(\Twig_Profiler_Profile $profile)
private function computeData(Profile $profile)
{
$data = array(
'template_count' => 0,
Expand Down
10 changes: 6 additions & 4 deletions src/Symfony/Bridge/Twig/Extension/AssetExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

use Symfony\Component\Asset\Packages;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* Twig extension for the Symfony Asset component.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class AssetExtension extends \Twig_Extension
class AssetExtension extends AbstractExtension
{
private $packages;
private $foundationExtension;
Expand All @@ -40,9 +42,9 @@ public function __construct(Packages $packages, HttpFoundationExtension $foundat
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('asset', array($this, 'getAssetUrl')),
new \Twig_SimpleFunction('asset_version', array($this, 'getAssetVersion')),
new \Twig_SimpleFunction('assets_version', array($this, 'getAssetsVersion'), array('deprecated' => true, 'alternative' => 'asset_version')),
new TwigFunction('asset', array($this, 'getAssetUrl')),
new TwigFunction('asset_version', array($this, 'getAssetVersion')),
new TwigFunction('assets_version', array($this, 'getAssetsVersion'), array('deprecated' => true, 'alternative' => 'asset_version')),
);
}

Expand Down
21 changes: 12 additions & 9 deletions src/Symfony/Bridge/Twig/Extension/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

namespace Symfony\Bridge\Twig\Extension;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

/**
* Twig extension relate to PHP code and used by the profiler and the default exception templates.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class CodeExtension extends \Twig_Extension
class CodeExtension extends AbstractExtension
{
private $fileLinkFormat;
private $rootDir;
Expand All @@ -42,14 +45,14 @@ public function __construct($fileLinkFormat, $rootDir, $charset)
public function getFilters()
{
return array(
new \Twig_SimpleFilter('abbr_class', array($this, 'abbrClass'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('abbr_method', array($this, 'abbrMethod'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('format_args', array($this, 'formatArgs'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('format_args_as_text', array($this, 'formatArgsAsText')),
new \Twig_SimpleFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('file_link', array($this, 'getFileLink')),
new TwigFilter('abbr_class', array($this, 'abbrClass'), array('is_safe' => array('html'))),
new TwigFilter('abbr_method', array($this, 'abbrMethod'), array('is_safe' => array('html'))),
new TwigFilter('format_args', array($this, 'formatArgs'), array('is_safe' => array('html'))),
new TwigFilter('format_args_as_text', array($this, 'formatArgsAsText')),
new TwigFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))),
new TwigFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))),
new TwigFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
new TwigFilter('file_link', array($this, 'getFileLink')),
);
}

Expand Down
12 changes: 8 additions & 4 deletions src/Symfony/Bridge/Twig/Extension/DumpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
use Symfony\Bridge\Twig\TokenParser\DumpTokenParser;
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Template;
use Twig\TwigFunction;

/**
* Provides integration of the dump() function with Twig.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class DumpExtension extends \Twig_Extension
class DumpExtension extends AbstractExtension
{
private $cloner;

Expand All @@ -32,7 +36,7 @@ public function __construct(ClonerInterface $cloner)
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('dump', array($this, 'dump'), array('is_safe' => array('html'), 'needs_context' => true, 'needs_environment' => true)),
new TwigFunction('dump', array($this, 'dump'), array('is_safe' => array('html'), 'needs_context' => true, 'needs_environment' => true)),
);
}

Expand All @@ -46,7 +50,7 @@ public function getName()
return 'dump';
}

public function dump(\Twig_Environment $env, $context)
public function dump(Environment $env, $context)
{
if (!$env->isDebug()) {
return;
Expand All @@ -55,7 +59,7 @@ public function dump(\Twig_Environment $env, $context)
if (2 === func_num_args()) {
$vars = array();
foreach ($context as $key => $value) {
if (!$value instanceof \Twig_Template) {
if (!$value instanceof Template) {
$vars[$key] = $value;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Bridge/Twig/Extension/ExpressionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
namespace Symfony\Bridge\Twig\Extension;

use Symfony\Component\ExpressionLanguage\Expression;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* ExpressionExtension gives a way to create Expressions from a template.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ExpressionExtension extends \Twig_Extension
class ExpressionExtension extends AbstractExtension
{
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('expression', array($this, 'createExpression')),
new TwigFunction('expression', array($this, 'createExpression')),
);
}

Expand Down
34 changes: 20 additions & 14 deletions src/Symfony/Bridge/Twig/Extension/FormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
use Symfony\Bridge\Twig\Form\TwigRendererInterface;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Extension\InitRuntimeInterface;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;

/**
* FormExtension extends Twig with form capabilities.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FormExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface
class FormExtension extends AbstractExtension implements InitRuntimeInterface
{
/**
* This property is public so that it can be accessed directly from compiled
Expand All @@ -39,7 +45,7 @@ public function __construct(TwigRendererInterface $renderer)
/**
* {@inheritdoc}
*/
public function initRuntime(\Twig_Environment $environment)
public function initRuntime(Environment $environment)
{
$this->renderer->setEnvironment($environment);
}
Expand All @@ -61,16 +67,16 @@ public function getTokenParsers()
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('form_enctype', null, array('node_class' => 'Symfony\Bridge\Twig\Node\FormEnctypeNode', 'is_safe' => array('html'), 'deprecated' => true, 'alternative' => 'form_start')),
new \Twig_SimpleFunction('form_widget', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
new \Twig_SimpleFunction('form_errors', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
new \Twig_SimpleFunction('form_label', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
new \Twig_SimpleFunction('form_row', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
new \Twig_SimpleFunction('form_rest', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
new \Twig_SimpleFunction('form', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
new \Twig_SimpleFunction('form_start', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
new \Twig_SimpleFunction('form_end', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
new \Twig_SimpleFunction('csrf_token', array($this, 'renderCsrfToken')),
new TwigFunction('form_enctype', null, array('node_class' => 'Symfony\Bridge\Twig\Node\FormEnctypeNode', 'is_safe' => array('html'), 'deprecated' => true, 'alternative' => 'form_start')),
new TwigFunction('form_widget', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
new TwigFunction('form_errors', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
new TwigFunction('form_label', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
new TwigFunction('form_row', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
new TwigFunction('form_rest', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
new TwigFunction('form', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
new TwigFunction('form_start', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
new TwigFunction('form_end', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
new TwigFunction('csrf_token', array($this, 'renderCsrfToken')),
);
}

Expand All @@ -80,7 +86,7 @@ public function getFunctions()
public function getFilters()
{
return array(
new \Twig_SimpleFilter('humanize', array($this, 'humanize')),
new TwigFilter('humanize', array($this, 'humanize')),
);
}

Expand All @@ -90,7 +96,7 @@ public function getFilters()
public function getTests()
{
return array(
new \Twig_SimpleTest('selectedchoice', array($this, 'isSelectedChoice')),
new TwigTest('selectedchoice', array($this, 'isSelectedChoice')),
);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Symfony/Bridge/Twig/Extension/HttpFoundationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* Twig extension for the Symfony HttpFoundation component.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class HttpFoundationExtension extends \Twig_Extension
class HttpFoundationExtension extends AbstractExtension
{
private $requestStack;
private $requestContext;
Expand All @@ -37,8 +39,8 @@ public function __construct(RequestStack $requestStack, RequestContext $requestC
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('absolute_url', array($this, 'generateAbsoluteUrl')),
new \Twig_SimpleFunction('relative_path', array($this, 'generateRelativePath')),
new TwigFunction('absolute_url', array($this, 'generateAbsoluteUrl')),
new TwigFunction('relative_path', array($this, 'generateRelativePath')),
);
}

Expand Down
Loading