Update Twig from 1.35.0 to 1.42.4 (PHP 5.5 is now required!)

This fixes some errors on PHP 7.4 and contains even more fixes
Also bumped PHP version to 5.5 as Twig requires it.
This commit is contained in:
slawkens
2020-02-15 05:41:38 +01:00
parent d9e449b6cf
commit 8021308822
414 changed files with 9276 additions and 5531 deletions

View File

@@ -0,0 +1,72 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extension;
use Twig\Environment;
abstract class AbstractExtension implements ExtensionInterface
{
/**
* @deprecated since 1.23 (to be removed in 2.0), implement \Twig_Extension_InitRuntimeInterface instead
*/
public function initRuntime(Environment $environment)
{
}
public function getTokenParsers()
{
return [];
}
public function getNodeVisitors()
{
return [];
}
public function getFilters()
{
return [];
}
public function getTests()
{
return [];
}
public function getFunctions()
{
return [];
}
public function getOperators()
{
return [];
}
/**
* @deprecated since 1.23 (to be removed in 2.0), implement \Twig_Extension_GlobalsInterface instead
*/
public function getGlobals()
{
return [];
}
/**
* @deprecated since 1.26 (to be removed in 2.0), not used anymore internally
*/
public function getName()
{
return \get_class($this);
}
}
class_alias('Twig\Extension\AbstractExtension', 'Twig_Extension');

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -9,26 +9,29 @@
* file that was distributed with this source code.
*/
namespace Twig\Extension {
use Twig\TwigFunction;
/**
* @final
*/
class Twig_Extension_Debug extends Twig_Extension
class DebugExtension extends AbstractExtension
{
public function getFunctions()
{
// dump is safe if var_dump is overridden by xdebug
$isDumpOutputHtmlSafe = extension_loaded('xdebug')
$isDumpOutputHtmlSafe = \extension_loaded('xdebug')
// false means that it was not set (and the default is on) or it explicitly enabled
&& (false === ini_get('xdebug.overload_var_dump') || ini_get('xdebug.overload_var_dump'))
// false means that it was not set (and the default is on) or it explicitly enabled
// xdebug.overload_var_dump produces HTML only when html_errors is also enabled
&& (false === ini_get('html_errors') || ini_get('html_errors'))
|| 'cli' === PHP_SAPI
|| 'cli' === \PHP_SAPI
;
return array(
new Twig_SimpleFunction('dump', 'twig_var_dump', array('is_safe' => $isDumpOutputHtmlSafe ? array('html') : array(), 'needs_context' => true, 'needs_environment' => true)),
);
return [
new TwigFunction('dump', 'twig_var_dump', ['is_safe' => $isDumpOutputHtmlSafe ? ['html'] : [], 'needs_context' => true, 'needs_environment' => true, 'is_variadic' => true]),
];
}
public function getName()
@@ -37,7 +40,15 @@ class Twig_Extension_Debug extends Twig_Extension
}
}
function twig_var_dump(Twig_Environment $env, $context)
class_alias('Twig\Extension\DebugExtension', 'Twig_Extension_Debug');
}
namespace {
use Twig\Environment;
use Twig\Template;
use Twig\TemplateWrapper;
function twig_var_dump(Environment $env, $context, array $vars = [])
{
if (!$env->isDebug()) {
return;
@@ -45,23 +56,21 @@ function twig_var_dump(Twig_Environment $env, $context)
ob_start();
$count = func_num_args();
if (2 === $count) {
$vars = array();
if (!$vars) {
$vars = [];
foreach ($context as $key => $value) {
if (!$value instanceof Twig_Template) {
if (!$value instanceof Template && !$value instanceof TemplateWrapper) {
$vars[$key] = $value;
}
}
var_dump($vars);
} else {
for ($i = 2; $i < $count; ++$i) {
var_dump(func_get_arg($i));
foreach ($vars as $var) {
var_dump($var);
}
}
return ob_get_clean();
}
class_alias('Twig_Extension_Debug', 'Twig\Extension\DebugExtension', false);
}

View File

@@ -9,10 +9,15 @@
* file that was distributed with this source code.
*/
namespace Twig\Extension {
use Twig\NodeVisitor\EscaperNodeVisitor;
use Twig\TokenParser\AutoEscapeTokenParser;
use Twig\TwigFilter;
/**
* @final
*/
class Twig_Extension_Escaper extends Twig_Extension
class EscaperExtension extends AbstractExtension
{
protected $defaultStrategy;
@@ -28,19 +33,19 @@ class Twig_Extension_Escaper extends Twig_Extension
public function getTokenParsers()
{
return array(new Twig_TokenParser_AutoEscape());
return [new AutoEscapeTokenParser()];
}
public function getNodeVisitors()
{
return array(new Twig_NodeVisitor_Escaper());
return [new EscaperNodeVisitor()];
}
public function getFilters()
{
return array(
new Twig_SimpleFilter('raw', 'twig_raw_filter', array('is_safe' => array('all'))),
);
return [
new TwigFilter('raw', 'twig_raw_filter', ['is_safe' => ['all']]),
];
}
/**
@@ -67,7 +72,7 @@ class Twig_Extension_Escaper extends Twig_Extension
}
if ('name' === $defaultStrategy) {
$defaultStrategy = array('Twig_FileExtensionEscapingStrategy', 'guess');
$defaultStrategy = ['\Twig\FileExtensionEscapingStrategy', 'guess'];
}
$this->defaultStrategy = $defaultStrategy;
@@ -84,8 +89,8 @@ class Twig_Extension_Escaper extends Twig_Extension
{
// disable string callables to avoid calling a function named html or js,
// or any other upcoming escaping strategy
if (!is_string($this->defaultStrategy) && false !== $this->defaultStrategy) {
return call_user_func($this->defaultStrategy, $name);
if (!\is_string($this->defaultStrategy) && false !== $this->defaultStrategy) {
return \call_user_func($this->defaultStrategy, $name);
}
return $this->defaultStrategy;
@@ -97,6 +102,10 @@ class Twig_Extension_Escaper extends Twig_Extension
}
}
class_alias('Twig\Extension\EscaperExtension', 'Twig_Extension_Escaper');
}
namespace {
/**
* Marks a variable as being safe.
*
@@ -108,5 +117,4 @@ function twig_raw_filter($string)
{
return $string;
}
class_alias('Twig_Extension_Escaper', 'Twig\Extension\EscaperExtension', false);
}

View File

@@ -0,0 +1,101 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extension;
use Twig\Environment;
use Twig\NodeVisitor\NodeVisitorInterface;
use Twig\TokenParser\TokenParserInterface;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;
/**
* Interface implemented by extension classes.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface ExtensionInterface
{
/**
* Initializes the runtime environment.
*
* This is where you can load some file that contains filter functions for instance.
*
* @deprecated since 1.23 (to be removed in 2.0), implement \Twig_Extension_InitRuntimeInterface instead
*/
public function initRuntime(Environment $environment);
/**
* Returns the token parser instances to add to the existing list.
*
* @return TokenParserInterface[]
*/
public function getTokenParsers();
/**
* Returns the node visitor instances to add to the existing list.
*
* @return NodeVisitorInterface[]
*/
public function getNodeVisitors();
/**
* Returns a list of filters to add to the existing list.
*
* @return TwigFilter[]
*/
public function getFilters();
/**
* Returns a list of tests to add to the existing list.
*
* @return TwigTest[]
*/
public function getTests();
/**
* Returns a list of functions to add to the existing list.
*
* @return TwigFunction[]
*/
public function getFunctions();
/**
* Returns a list of operators to add to the existing list.
*
* @return array<array> First array of unary operators, second array of binary operators
*/
public function getOperators();
/**
* Returns a list of global variables to add to the existing list.
*
* @return array An array of global variables
*
* @deprecated since 1.23 (to be removed in 2.0), implement \Twig_Extension_GlobalsInterface instead
*/
public function getGlobals();
/**
* Returns the name of the extension.
*
* @return string The extension name
*
* @deprecated since 1.26 (to be removed in 2.0), not used anymore internally
*/
public function getName();
}
class_alias('Twig\Extension\ExtensionInterface', 'Twig_ExtensionInterface');
// Ensure that the aliased name is loaded to keep BC for classes implementing the typehint with the old aliased name.
class_exists('Twig\Environment');

View File

@@ -9,16 +9,18 @@
* file that was distributed with this source code.
*/
namespace Twig\Extension;
/**
* Enables usage of the deprecated Twig_Extension::getGlobals() method.
* Enables usage of the deprecated Twig\Extension\AbstractExtension::getGlobals() method.
*
* Explicitly implement this interface if you really need to implement the
* deprecated getGlobals() method in your extensions.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface Twig_Extension_GlobalsInterface
interface GlobalsInterface
{
}
class_alias('Twig_Extension_GlobalsInterface', 'Twig\Extension\GlobalsInterface', false);
class_alias('Twig\Extension\GlobalsInterface', 'Twig_Extension_GlobalsInterface');

View File

@@ -9,16 +9,18 @@
* file that was distributed with this source code.
*/
namespace Twig\Extension;
/**
* Enables usage of the deprecated Twig_Extension::initRuntime() method.
* Enables usage of the deprecated Twig\Extension\AbstractExtension::initRuntime() method.
*
* Explicitly implement this interface if you really need to implement the
* deprecated initRuntime() method in your extensions.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface Twig_Extension_InitRuntimeInterface
interface InitRuntimeInterface
{
}
class_alias('Twig_Extension_InitRuntimeInterface', 'Twig\Extension\InitRuntimeInterface', false);
class_alias('Twig\Extension\InitRuntimeInterface', 'Twig_Extension_InitRuntimeInterface');

View File

@@ -9,10 +9,14 @@
* file that was distributed with this source code.
*/
namespace Twig\Extension;
use Twig\NodeVisitor\OptimizerNodeVisitor;
/**
* @final
*/
class Twig_Extension_Optimizer extends Twig_Extension
class OptimizerExtension extends AbstractExtension
{
protected $optimizers;
@@ -23,7 +27,7 @@ class Twig_Extension_Optimizer extends Twig_Extension
public function getNodeVisitors()
{
return array(new Twig_NodeVisitor_Optimizer($this->optimizers));
return [new OptimizerNodeVisitor($this->optimizers)];
}
public function getName()
@@ -32,4 +36,4 @@ class Twig_Extension_Optimizer extends Twig_Extension
}
}
class_alias('Twig_Extension_Optimizer', 'Twig\Extension\OptimizerExtension', false);
class_alias('Twig\Extension\OptimizerExtension', 'Twig_Extension_Optimizer');

View File

@@ -9,34 +9,39 @@
* file that was distributed with this source code.
*/
class Twig_Extension_Profiler extends Twig_Extension
{
private $actives = array();
namespace Twig\Extension;
public function __construct(Twig_Profiler_Profile $profile)
use Twig\Profiler\NodeVisitor\ProfilerNodeVisitor;
use Twig\Profiler\Profile;
class ProfilerExtension extends AbstractExtension
{
private $actives = [];
public function __construct(Profile $profile)
{
$this->actives[] = $profile;
}
public function enter(Twig_Profiler_Profile $profile)
public function enter(Profile $profile)
{
$this->actives[0]->addProfile($profile);
array_unshift($this->actives, $profile);
}
public function leave(Twig_Profiler_Profile $profile)
public function leave(Profile $profile)
{
$profile->leave();
array_shift($this->actives);
if (1 === count($this->actives)) {
if (1 === \count($this->actives)) {
$this->actives[0]->leave();
}
}
public function getNodeVisitors()
{
return array(new Twig_Profiler_NodeVisitor_Profiler(get_class($this)));
return [new ProfilerNodeVisitor(\get_class($this))];
}
public function getName()
@@ -45,5 +50,4 @@ class Twig_Extension_Profiler extends Twig_Extension
}
}
class_alias('Twig_Extension_Profiler', 'Twig\Extension\ProfilerExtension', false);
class_exists('Twig_Profiler_Profile');
class_alias('Twig\Extension\ProfilerExtension', 'Twig_Extension_Profiler');

View File

@@ -0,0 +1,19 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extension;
/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
interface RuntimeExtensionInterface
{
}

View File

@@ -9,16 +9,22 @@
* file that was distributed with this source code.
*/
namespace Twig\Extension;
use Twig\NodeVisitor\SandboxNodeVisitor;
use Twig\Sandbox\SecurityPolicyInterface;
use Twig\TokenParser\SandboxTokenParser;
/**
* @final
*/
class Twig_Extension_Sandbox extends Twig_Extension
class SandboxExtension extends AbstractExtension
{
protected $sandboxedGlobally;
protected $sandboxed;
protected $policy;
public function __construct(Twig_Sandbox_SecurityPolicyInterface $policy, $sandboxed = false)
public function __construct(SecurityPolicyInterface $policy, $sandboxed = false)
{
$this->policy = $policy;
$this->sandboxedGlobally = $sandboxed;
@@ -26,12 +32,12 @@ class Twig_Extension_Sandbox extends Twig_Extension
public function getTokenParsers()
{
return array(new Twig_TokenParser_Sandbox());
return [new SandboxTokenParser()];
}
public function getNodeVisitors()
{
return array(new Twig_NodeVisitor_Sandbox());
return [new SandboxNodeVisitor()];
}
public function enableSandbox()
@@ -54,7 +60,7 @@ class Twig_Extension_Sandbox extends Twig_Extension
return $this->sandboxedGlobally;
}
public function setSecurityPolicy(Twig_Sandbox_SecurityPolicyInterface $policy)
public function setSecurityPolicy(SecurityPolicyInterface $policy)
{
$this->policy = $policy;
}
@@ -87,7 +93,7 @@ class Twig_Extension_Sandbox extends Twig_Extension
public function ensureToStringAllowed($obj)
{
if ($this->isSandboxed() && is_object($obj)) {
if ($this->isSandboxed() && \is_object($obj) && method_exists($obj, '__toString')) {
$this->policy->checkMethodAllowed($obj, '__toString');
}
@@ -100,4 +106,4 @@ class Twig_Extension_Sandbox extends Twig_Extension
}
}
class_alias('Twig_Extension_Sandbox', 'Twig\Extension\SandboxExtension', false);
class_alias('Twig\Extension\SandboxExtension', 'Twig_Extension_Sandbox');

View File

@@ -9,23 +9,28 @@
* file that was distributed with this source code.
*/
namespace Twig\Extension;
use Twig\NodeVisitor\NodeVisitorInterface;
use Twig\TokenParser\TokenParserInterface;
/**
* Internal class.
*
* This class is used by Twig_Environment as a staging area and must not be used directly.
* This class is used by \Twig\Environment as a staging area and must not be used directly.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @internal
*/
class Twig_Extension_Staging extends Twig_Extension
class StagingExtension extends AbstractExtension
{
protected $functions = array();
protected $filters = array();
protected $visitors = array();
protected $tokenParsers = array();
protected $globals = array();
protected $tests = array();
protected $functions = [];
protected $filters = [];
protected $visitors = [];
protected $tokenParsers = [];
protected $globals = [];
protected $tests = [];
public function addFunction($name, $function)
{
@@ -55,7 +60,7 @@ class Twig_Extension_Staging extends Twig_Extension
return $this->filters;
}
public function addNodeVisitor(Twig_NodeVisitorInterface $visitor)
public function addNodeVisitor(NodeVisitorInterface $visitor)
{
$this->visitors[] = $visitor;
}
@@ -65,7 +70,7 @@ class Twig_Extension_Staging extends Twig_Extension
return $this->visitors;
}
public function addTokenParser(Twig_TokenParserInterface $parser)
public function addTokenParser(TokenParserInterface $parser)
{
if (isset($this->tokenParsers[$parser->getTag()])) {
@trigger_error(sprintf('Overriding tag "%s" that is already registered is deprecated since version 1.30 and won\'t be possible anymore in 2.0.', $parser->getTag()), E_USER_DEPRECATED);
@@ -109,4 +114,4 @@ class Twig_Extension_Staging extends Twig_Extension
}
}
class_alias('Twig_Extension_Staging', 'Twig\Extension\StagingExtension', false);
class_alias('Twig\Extension\StagingExtension', 'Twig_Extension_Staging');

View File

@@ -1,47 +0,0 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @final
*/
class Twig_Extension_StringLoader extends Twig_Extension
{
public function getFunctions()
{
return array(
new Twig_SimpleFunction('template_from_string', 'twig_template_from_string', array('needs_environment' => true)),
);
}
public function getName()
{
return 'string_loader';
}
}
/**
* Loads a template from a string.
*
* <pre>
* {{ include(template_from_string("Hello {{ name }}")) }}
* </pre>
*
* @param Twig_Environment $env A Twig_Environment instance
* @param string $template A template as a string or object implementing __toString()
*
* @return Twig_Template
*/
function twig_template_from_string(Twig_Environment $env, $template)
{
return $env->createTemplate((string) $template);
}
class_alias('Twig_Extension_StringLoader', 'Twig\Extension\StringLoaderExtension', false);

View File

@@ -0,0 +1,54 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extension {
use Twig\TwigFunction;
/**
* @final
*/
class StringLoaderExtension extends AbstractExtension
{
public function getFunctions()
{
return [
new TwigFunction('template_from_string', 'twig_template_from_string', ['needs_environment' => true]),
];
}
public function getName()
{
return 'string_loader';
}
}
class_alias('Twig\Extension\StringLoaderExtension', 'Twig_Extension_StringLoader');
}
namespace {
use Twig\Environment;
use Twig\TemplateWrapper;
/**
* Loads a template from a string.
*
* {{ include(template_from_string("Hello {{ name }}")) }}
*
* @param string $template A template as a string or object implementing __toString()
* @param string $name An optional name of the template to be used in error messages
*
* @return TemplateWrapper
*/
function twig_template_from_string(Environment $env, $template, $name = null)
{
return $env->createTemplate((string) $template, $name);
}
}