mirror of
https://github.com/slawkens/myaac.git
synced 2025-10-16 02:34:54 +02:00
Update Twig to v2.15.4
This commit is contained in:
@@ -23,21 +23,13 @@ use Twig\Node\Node;
|
||||
*/
|
||||
abstract class AbstractNodeVisitor implements NodeVisitorInterface
|
||||
{
|
||||
final public function enterNode(\Twig_NodeInterface $node, Environment $env)
|
||||
final public function enterNode(Node $node, Environment $env)
|
||||
{
|
||||
if (!$node instanceof Node) {
|
||||
throw new \LogicException(sprintf('%s only supports \Twig\Node\Node instances.', __CLASS__));
|
||||
}
|
||||
|
||||
return $this->doEnterNode($node, $env);
|
||||
}
|
||||
|
||||
final public function leaveNode(\Twig_NodeInterface $node, Environment $env)
|
||||
final public function leaveNode(Node $node, Environment $env)
|
||||
{
|
||||
if (!$node instanceof Node) {
|
||||
throw new \LogicException(sprintf('%s only supports \Twig\Node\Node instances.', __CLASS__));
|
||||
}
|
||||
|
||||
return $this->doLeaveNode($node, $env);
|
||||
}
|
||||
|
||||
@@ -51,7 +43,7 @@ abstract class AbstractNodeVisitor implements NodeVisitorInterface
|
||||
/**
|
||||
* Called after child nodes are visited.
|
||||
*
|
||||
* @return Node|false|null The modified node or null if the node must be removed
|
||||
* @return Node|null The modified node or null if the node must be removed
|
||||
*/
|
||||
abstract protected function doLeaveNode(Node $node, Environment $env);
|
||||
}
|
||||
|
@@ -12,6 +12,7 @@
|
||||
namespace Twig\NodeVisitor;
|
||||
|
||||
use Twig\Environment;
|
||||
use Twig\Extension\EscaperExtension;
|
||||
use Twig\Node\AutoEscapeNode;
|
||||
use Twig\Node\BlockNode;
|
||||
use Twig\Node\BlockReferenceNode;
|
||||
@@ -27,18 +28,16 @@ use Twig\Node\PrintNode;
|
||||
use Twig\NodeTraverser;
|
||||
|
||||
/**
|
||||
* @final
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class EscaperNodeVisitor extends AbstractNodeVisitor
|
||||
final class EscaperNodeVisitor extends AbstractNodeVisitor
|
||||
{
|
||||
protected $statusStack = [];
|
||||
protected $blocks = [];
|
||||
protected $safeAnalysis;
|
||||
protected $traverser;
|
||||
protected $defaultStrategy = false;
|
||||
protected $safeVars = [];
|
||||
private $statusStack = [];
|
||||
private $blocks = [];
|
||||
private $safeAnalysis;
|
||||
private $traverser;
|
||||
private $defaultStrategy = false;
|
||||
private $safeVars = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -48,7 +47,7 @@ class EscaperNodeVisitor extends AbstractNodeVisitor
|
||||
protected function doEnterNode(Node $node, Environment $env)
|
||||
{
|
||||
if ($node instanceof ModuleNode) {
|
||||
if ($env->hasExtension('\Twig\Extension\EscaperExtension') && $defaultStrategy = $env->getExtension('\Twig\Extension\EscaperExtension')->getDefaultStrategy($node->getTemplateName())) {
|
||||
if ($env->hasExtension(EscaperExtension::class) && $defaultStrategy = $env->getExtension(EscaperExtension::class)->getDefaultStrategy($node->getTemplateName())) {
|
||||
$this->defaultStrategy = $defaultStrategy;
|
||||
}
|
||||
$this->safeVars = [];
|
||||
@@ -128,7 +127,7 @@ class EscaperNodeVisitor extends AbstractNodeVisitor
|
||||
return new InlinePrint($this->getEscaperFilter($type, $expression), $node->getTemplateLine());
|
||||
}
|
||||
|
||||
protected function escapePrintNode(PrintNode $node, Environment $env, $type)
|
||||
private function escapePrintNode(PrintNode $node, Environment $env, $type)
|
||||
{
|
||||
if (false === $type) {
|
||||
return $node;
|
||||
@@ -145,7 +144,7 @@ class EscaperNodeVisitor extends AbstractNodeVisitor
|
||||
return new $class($this->getEscaperFilter($type, $expression), $node->getTemplateLine());
|
||||
}
|
||||
|
||||
protected function preEscapeFilterNode(FilterExpression $filter, Environment $env)
|
||||
private function preEscapeFilterNode(FilterExpression $filter, Environment $env)
|
||||
{
|
||||
$name = $filter->getNode('filter')->getAttribute('value');
|
||||
|
||||
@@ -164,7 +163,7 @@ class EscaperNodeVisitor extends AbstractNodeVisitor
|
||||
return $filter;
|
||||
}
|
||||
|
||||
protected function isSafeFor($type, \Twig_NodeInterface $expression, $env)
|
||||
private function isSafeFor($type, Node $expression, $env)
|
||||
{
|
||||
$safe = $this->safeAnalysis->getSafe($expression);
|
||||
|
||||
@@ -182,7 +181,7 @@ class EscaperNodeVisitor extends AbstractNodeVisitor
|
||||
return \in_array($type, $safe) || \in_array('all', $safe);
|
||||
}
|
||||
|
||||
protected function needEscaping(Environment $env)
|
||||
private function needEscaping(Environment $env)
|
||||
{
|
||||
if (\count($this->statusStack)) {
|
||||
return $this->statusStack[\count($this->statusStack) - 1];
|
||||
@@ -191,7 +190,7 @@ class EscaperNodeVisitor extends AbstractNodeVisitor
|
||||
return $this->defaultStrategy ? $this->defaultStrategy : false;
|
||||
}
|
||||
|
||||
protected function getEscaperFilter($type, \Twig_NodeInterface $node)
|
||||
private function getEscaperFilter(string $type, Node $node): FilterExpression
|
||||
{
|
||||
$line = $node->getTemplateLine();
|
||||
$name = new ConstantExpression('escape', $line);
|
||||
|
72
system/libs/Twig/NodeVisitor/MacroAutoImportNodeVisitor.php
Normal file
72
system/libs/Twig/NodeVisitor/MacroAutoImportNodeVisitor.php
Normal 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\NodeVisitor;
|
||||
|
||||
use Twig\Environment;
|
||||
use Twig\Node\Expression\AssignNameExpression;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Expression\GetAttrExpression;
|
||||
use Twig\Node\Expression\MethodCallExpression;
|
||||
use Twig\Node\Expression\NameExpression;
|
||||
use Twig\Node\ImportNode;
|
||||
use Twig\Node\ModuleNode;
|
||||
use Twig\Node\Node;
|
||||
|
||||
/**
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
final class MacroAutoImportNodeVisitor implements NodeVisitorInterface
|
||||
{
|
||||
private $inAModule = false;
|
||||
private $hasMacroCalls = false;
|
||||
|
||||
public function enterNode(Node $node, Environment $env)
|
||||
{
|
||||
if ($node instanceof ModuleNode) {
|
||||
$this->inAModule = true;
|
||||
$this->hasMacroCalls = false;
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
public function leaveNode(Node $node, Environment $env)
|
||||
{
|
||||
if ($node instanceof ModuleNode) {
|
||||
$this->inAModule = false;
|
||||
if ($this->hasMacroCalls) {
|
||||
$node->getNode('constructor_end')->setNode('_auto_macro_import', new ImportNode(new NameExpression('_self', 0), new AssignNameExpression('_self', 0), 0, 'import', true));
|
||||
}
|
||||
} elseif ($this->inAModule) {
|
||||
if (
|
||||
$node instanceof GetAttrExpression &&
|
||||
$node->getNode('node') instanceof NameExpression &&
|
||||
'_self' === $node->getNode('node')->getAttribute('name') &&
|
||||
$node->getNode('attribute') instanceof ConstantExpression
|
||||
) {
|
||||
$this->hasMacroCalls = true;
|
||||
|
||||
$name = $node->getNode('attribute')->getAttribute('value');
|
||||
$node = new MethodCallExpression($node->getNode('node'), 'macro_'.$name, $node->getNode('arguments'), $node->getTemplateLine());
|
||||
$node->setAttribute('safe', true);
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
public function getPriority()
|
||||
{
|
||||
// we must be ran before auto-escaping
|
||||
return -10;
|
||||
}
|
||||
}
|
@@ -12,6 +12,7 @@
|
||||
namespace Twig\NodeVisitor;
|
||||
|
||||
use Twig\Environment;
|
||||
use Twig\Node\Node;
|
||||
|
||||
/**
|
||||
* Interface for node visitor classes.
|
||||
@@ -23,16 +24,16 @@ interface NodeVisitorInterface
|
||||
/**
|
||||
* Called before child nodes are visited.
|
||||
*
|
||||
* @return \Twig_NodeInterface The modified node
|
||||
* @return Node The modified node
|
||||
*/
|
||||
public function enterNode(\Twig_NodeInterface $node, Environment $env);
|
||||
public function enterNode(Node $node, Environment $env);
|
||||
|
||||
/**
|
||||
* Called after child nodes are visited.
|
||||
*
|
||||
* @return \Twig_NodeInterface|false|null The modified node or null if the node must be removed
|
||||
* @return Node|null The modified node or null if the node must be removed
|
||||
*/
|
||||
public function leaveNode(\Twig_NodeInterface $node, Environment $env);
|
||||
public function leaveNode(Node $node, Environment $env);
|
||||
|
||||
/**
|
||||
* Returns the priority for this visitor.
|
||||
|
@@ -13,8 +13,6 @@ namespace Twig\NodeVisitor;
|
||||
|
||||
use Twig\Environment;
|
||||
use Twig\Node\BlockReferenceNode;
|
||||
use Twig\Node\BodyNode;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\BlockReferenceExpression;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Expression\FilterExpression;
|
||||
@@ -22,12 +20,10 @@ use Twig\Node\Expression\FunctionExpression;
|
||||
use Twig\Node\Expression\GetAttrExpression;
|
||||
use Twig\Node\Expression\NameExpression;
|
||||
use Twig\Node\Expression\ParentExpression;
|
||||
use Twig\Node\Expression\TempNameExpression;
|
||||
use Twig\Node\ForNode;
|
||||
use Twig\Node\IncludeNode;
|
||||
use Twig\Node\Node;
|
||||
use Twig\Node\PrintNode;
|
||||
use Twig\Node\SetTempNode;
|
||||
|
||||
/**
|
||||
* Tries to optimize the AST.
|
||||
@@ -37,28 +33,25 @@ use Twig\Node\SetTempNode;
|
||||
* You can configure which optimizations you want to activate via the
|
||||
* optimizer mode.
|
||||
*
|
||||
* @final
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class OptimizerNodeVisitor extends AbstractNodeVisitor
|
||||
final class OptimizerNodeVisitor extends AbstractNodeVisitor
|
||||
{
|
||||
const OPTIMIZE_ALL = -1;
|
||||
const OPTIMIZE_NONE = 0;
|
||||
const OPTIMIZE_FOR = 2;
|
||||
const OPTIMIZE_RAW_FILTER = 4;
|
||||
const OPTIMIZE_VAR_ACCESS = 8;
|
||||
public const OPTIMIZE_ALL = -1;
|
||||
public const OPTIMIZE_NONE = 0;
|
||||
public const OPTIMIZE_FOR = 2;
|
||||
public const OPTIMIZE_RAW_FILTER = 4;
|
||||
// obsolete, does not do anything
|
||||
public const OPTIMIZE_VAR_ACCESS = 8;
|
||||
|
||||
protected $loops = [];
|
||||
protected $loopsTargets = [];
|
||||
protected $optimizers;
|
||||
protected $prependedNodes = [];
|
||||
protected $inABody = false;
|
||||
private $loops = [];
|
||||
private $loopsTargets = [];
|
||||
private $optimizers;
|
||||
|
||||
/**
|
||||
* @param int $optimizers The optimizer mode
|
||||
*/
|
||||
public function __construct($optimizers = -1)
|
||||
public function __construct(int $optimizers = -1)
|
||||
{
|
||||
if (!\is_int($optimizers) || $optimizers > (self::OPTIMIZE_FOR | self::OPTIMIZE_RAW_FILTER | self::OPTIMIZE_VAR_ACCESS)) {
|
||||
throw new \InvalidArgumentException(sprintf('Optimizer mode "%s" is not valid.', $optimizers));
|
||||
@@ -73,27 +66,11 @@ class OptimizerNodeVisitor extends AbstractNodeVisitor
|
||||
$this->enterOptimizeFor($node, $env);
|
||||
}
|
||||
|
||||
if (\PHP_VERSION_ID < 50400 && self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('\Twig\Extension\SandboxExtension')) {
|
||||
if ($this->inABody) {
|
||||
if (!$node instanceof AbstractExpression) {
|
||||
if ('Twig_Node' !== \get_class($node)) {
|
||||
array_unshift($this->prependedNodes, []);
|
||||
}
|
||||
} else {
|
||||
$node = $this->optimizeVariables($node, $env);
|
||||
}
|
||||
} elseif ($node instanceof BodyNode) {
|
||||
$this->inABody = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
protected function doLeaveNode(Node $node, Environment $env)
|
||||
{
|
||||
$expression = $node instanceof AbstractExpression;
|
||||
|
||||
if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
|
||||
$this->leaveOptimizeFor($node, $env);
|
||||
}
|
||||
@@ -104,33 +81,6 @@ class OptimizerNodeVisitor extends AbstractNodeVisitor
|
||||
|
||||
$node = $this->optimizePrintNode($node, $env);
|
||||
|
||||
if (self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('\Twig\Extension\SandboxExtension')) {
|
||||
if ($node instanceof BodyNode) {
|
||||
$this->inABody = false;
|
||||
} elseif ($this->inABody) {
|
||||
if (!$expression && 'Twig_Node' !== \get_class($node) && $prependedNodes = array_shift($this->prependedNodes)) {
|
||||
$nodes = [];
|
||||
foreach (array_unique($prependedNodes) as $name) {
|
||||
$nodes[] = new SetTempNode($name, $node->getTemplateLine());
|
||||
}
|
||||
|
||||
$nodes[] = $node;
|
||||
$node = new Node($nodes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
protected function optimizeVariables(\Twig_NodeInterface $node, Environment $env)
|
||||
{
|
||||
if ('Twig_Node_Expression_Name' === \get_class($node) && $node->isSimple()) {
|
||||
$this->prependedNodes[0][] = $node->getAttribute('name');
|
||||
|
||||
return new TempNameExpression($node->getAttribute('name'), $node->getTemplateLine());
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
@@ -140,10 +90,8 @@ class OptimizerNodeVisitor extends AbstractNodeVisitor
|
||||
* It replaces:
|
||||
*
|
||||
* * "echo $this->render(Parent)Block()" with "$this->display(Parent)Block()"
|
||||
*
|
||||
* @return \Twig_NodeInterface
|
||||
*/
|
||||
protected function optimizePrintNode(\Twig_NodeInterface $node, Environment $env)
|
||||
private function optimizePrintNode(Node $node, Environment $env): Node
|
||||
{
|
||||
if (!$node instanceof PrintNode) {
|
||||
return $node;
|
||||
@@ -164,10 +112,8 @@ class OptimizerNodeVisitor extends AbstractNodeVisitor
|
||||
|
||||
/**
|
||||
* Removes "raw" filters.
|
||||
*
|
||||
* @return \Twig_NodeInterface
|
||||
*/
|
||||
protected function optimizeRawFilter(\Twig_NodeInterface $node, Environment $env)
|
||||
private function optimizeRawFilter(Node $node, Environment $env): Node
|
||||
{
|
||||
if ($node instanceof FilterExpression && 'raw' == $node->getNode('filter')->getAttribute('value')) {
|
||||
return $node->getNode('node');
|
||||
@@ -179,7 +125,7 @@ class OptimizerNodeVisitor extends AbstractNodeVisitor
|
||||
/**
|
||||
* Optimizes "for" tag by removing the "loop" variable creation whenever possible.
|
||||
*/
|
||||
protected function enterOptimizeFor(\Twig_NodeInterface $node, Environment $env)
|
||||
private function enterOptimizeFor(Node $node, Environment $env)
|
||||
{
|
||||
if ($node instanceof ForNode) {
|
||||
// disable the loop variable by default
|
||||
@@ -243,7 +189,7 @@ class OptimizerNodeVisitor extends AbstractNodeVisitor
|
||||
/**
|
||||
* Optimizes "for" tag by removing the "loop" variable creation whenever possible.
|
||||
*/
|
||||
protected function leaveOptimizeFor(\Twig_NodeInterface $node, Environment $env)
|
||||
private function leaveOptimizeFor(Node $node, Environment $env)
|
||||
{
|
||||
if ($node instanceof ForNode) {
|
||||
array_shift($this->loops);
|
||||
@@ -252,12 +198,12 @@ class OptimizerNodeVisitor extends AbstractNodeVisitor
|
||||
}
|
||||
}
|
||||
|
||||
protected function addLoopToCurrent()
|
||||
private function addLoopToCurrent()
|
||||
{
|
||||
$this->loops[0]->setAttribute('with_loop', true);
|
||||
}
|
||||
|
||||
protected function addLoopToAll()
|
||||
private function addLoopToAll()
|
||||
{
|
||||
foreach ($this->loops as $loop) {
|
||||
$loop->setAttribute('with_loop', true);
|
||||
|
@@ -23,20 +23,17 @@ use Twig\Node\Expression\NameExpression;
|
||||
use Twig\Node\Expression\ParentExpression;
|
||||
use Twig\Node\Node;
|
||||
|
||||
/**
|
||||
* @final
|
||||
*/
|
||||
class SafeAnalysisNodeVisitor extends AbstractNodeVisitor
|
||||
final class SafeAnalysisNodeVisitor extends AbstractNodeVisitor
|
||||
{
|
||||
protected $data = [];
|
||||
protected $safeVars = [];
|
||||
private $data = [];
|
||||
private $safeVars = [];
|
||||
|
||||
public function setSafeVars($safeVars)
|
||||
{
|
||||
$this->safeVars = $safeVars;
|
||||
}
|
||||
|
||||
public function getSafe(\Twig_NodeInterface $node)
|
||||
public function getSafe(Node $node)
|
||||
{
|
||||
$hash = spl_object_hash($node);
|
||||
if (!isset($this->data[$hash])) {
|
||||
@@ -56,7 +53,7 @@ class SafeAnalysisNodeVisitor extends AbstractNodeVisitor
|
||||
}
|
||||
}
|
||||
|
||||
protected function setSafe(\Twig_NodeInterface $node, array $safe)
|
||||
private function setSafe(Node $node, array $safe)
|
||||
{
|
||||
$hash = spl_object_hash($node);
|
||||
if (isset($this->data[$hash])) {
|
||||
@@ -125,8 +122,7 @@ class SafeAnalysisNodeVisitor extends AbstractNodeVisitor
|
||||
}
|
||||
} elseif ($node instanceof GetAttrExpression && $node->getNode('node') instanceof NameExpression) {
|
||||
$name = $node->getNode('node')->getAttribute('name');
|
||||
// attributes on template instances are safe
|
||||
if ('_self' == $name || \in_array($name, $this->safeVars)) {
|
||||
if (\in_array($name, $this->safeVars)) {
|
||||
$this->setSafe($node, ['all']);
|
||||
} else {
|
||||
$this->setSafe($node, []);
|
||||
@@ -138,7 +134,7 @@ class SafeAnalysisNodeVisitor extends AbstractNodeVisitor
|
||||
return $node;
|
||||
}
|
||||
|
||||
protected function intersectSafe(array $a = null, array $b = null)
|
||||
private function intersectSafe(array $a = null, array $b = null): array
|
||||
{
|
||||
if (null === $a || null === $b) {
|
||||
return [];
|
||||
|
@@ -12,6 +12,7 @@
|
||||
namespace Twig\NodeVisitor;
|
||||
|
||||
use Twig\Environment;
|
||||
use Twig\Node\CheckSecurityCallNode;
|
||||
use Twig\Node\CheckSecurityNode;
|
||||
use Twig\Node\CheckToStringNode;
|
||||
use Twig\Node\Expression\Binary\ConcatBinary;
|
||||
@@ -26,16 +27,14 @@ use Twig\Node\PrintNode;
|
||||
use Twig\Node\SetNode;
|
||||
|
||||
/**
|
||||
* @final
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class SandboxNodeVisitor extends AbstractNodeVisitor
|
||||
final class SandboxNodeVisitor extends AbstractNodeVisitor
|
||||
{
|
||||
protected $inAModule = false;
|
||||
protected $tags;
|
||||
protected $filters;
|
||||
protected $functions;
|
||||
private $inAModule = false;
|
||||
private $tags;
|
||||
private $filters;
|
||||
private $functions;
|
||||
|
||||
private $needsToStringWrap = false;
|
||||
|
||||
@@ -102,7 +101,8 @@ class SandboxNodeVisitor extends AbstractNodeVisitor
|
||||
if ($node instanceof ModuleNode) {
|
||||
$this->inAModule = false;
|
||||
|
||||
$node->getNode('constructor_end')->setNode('_security_check', new Node([new CheckSecurityNode($this->filters, $this->tags, $this->functions), $node->getNode('display_start')]));
|
||||
$node->setNode('constructor_end', new Node([new CheckSecurityCallNode(), $node->getNode('constructor_end')]));
|
||||
$node->setNode('class_end', new Node([new CheckSecurityNode($this->filters, $this->tags, $this->functions), $node->getNode('class_end')]));
|
||||
} elseif ($this->inAModule) {
|
||||
if ($node instanceof PrintNode || $node instanceof SetNode) {
|
||||
$this->needsToStringWrap = false;
|
||||
@@ -112,7 +112,7 @@ class SandboxNodeVisitor extends AbstractNodeVisitor
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function wrapNode(Node $node, $name)
|
||||
private function wrapNode(Node $node, string $name)
|
||||
{
|
||||
$expr = $node->getNode($name);
|
||||
if ($expr instanceof NameExpression || $expr instanceof GetAttrExpression) {
|
||||
@@ -120,7 +120,7 @@ class SandboxNodeVisitor extends AbstractNodeVisitor
|
||||
}
|
||||
}
|
||||
|
||||
private function wrapArrayNode(Node $node, $name)
|
||||
private function wrapArrayNode(Node $node, string $name)
|
||||
{
|
||||
$args = $node->getNode($name);
|
||||
foreach ($args as $name => $_) {
|
||||
|
Reference in New Issue
Block a user