1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-22 20:35:54 +01:00
vichan/inc/lib/Twig/NodeVisitor/Optimizer.php

254 lines
8.4 KiB
PHP
Raw Normal View History

2011-10-05 06:22:53 +02:00
<?php
/*
* This file is part of Twig.
*
2018-05-10 12:24:53 +02:00
* (c) Fabien Potencier
2011-10-05 06:22:53 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Twig_NodeVisitor_Optimizer tries to optimizes the AST.
*
* This visitor is always the last registered one.
*
* You can configure which optimizations you want to activate via the
* optimizer mode.
*
2018-05-10 12:24:53 +02:00
* @final
*
2013-08-01 21:20:12 +02:00
* @author Fabien Potencier <fabien@symfony.com>
2011-10-05 06:22:53 +02:00
*/
2018-05-10 12:24:53 +02:00
class Twig_NodeVisitor_Optimizer extends Twig_BaseNodeVisitor
2011-10-05 06:22:53 +02:00
{
2018-05-10 12:24:53 +02:00
const OPTIMIZE_ALL = -1;
const OPTIMIZE_NONE = 0;
const OPTIMIZE_FOR = 2;
const OPTIMIZE_RAW_FILTER = 4;
const OPTIMIZE_VAR_ACCESS = 8;
2011-10-05 06:22:53 +02:00
protected $loops = array();
2018-05-10 12:24:53 +02:00
protected $loopsTargets = array();
2011-10-05 06:22:53 +02:00
protected $optimizers;
2013-08-01 21:20:12 +02:00
protected $prependedNodes = array();
protected $inABody = false;
2011-10-05 06:22:53 +02:00
/**
2018-05-10 12:24:53 +02:00
* @param int $optimizers The optimizer mode
2011-10-05 06:22:53 +02:00
*/
public function __construct($optimizers = -1)
{
2018-05-10 12:24:53 +02:00
if (!is_int($optimizers) || $optimizers > (self::OPTIMIZE_FOR | self::OPTIMIZE_RAW_FILTER | self::OPTIMIZE_VAR_ACCESS)) {
2011-10-05 06:22:53 +02:00
throw new InvalidArgumentException(sprintf('Optimizer mode "%s" is not valid.', $optimizers));
}
$this->optimizers = $optimizers;
}
2018-05-10 12:24:53 +02:00
protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
2011-10-05 06:22:53 +02:00
{
if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
$this->enterOptimizeFor($node, $env);
}
2018-05-10 12:24:53 +02:00
if (PHP_VERSION_ID < 50400 && self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('Twig_Extension_Sandbox')) {
2013-08-01 21:20:12 +02:00
if ($this->inABody) {
if (!$node instanceof Twig_Node_Expression) {
2018-05-10 12:24:53 +02:00
if ('Twig_Node' !== get_class($node)) {
2013-08-01 21:20:12 +02:00
array_unshift($this->prependedNodes, array());
}
} else {
$node = $this->optimizeVariables($node, $env);
}
} elseif ($node instanceof Twig_Node_Body) {
$this->inABody = true;
}
}
2011-10-05 06:22:53 +02:00
return $node;
}
2018-05-10 12:24:53 +02:00
protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
2011-10-05 06:22:53 +02:00
{
2013-08-01 21:20:12 +02:00
$expression = $node instanceof Twig_Node_Expression;
2011-10-05 06:22:53 +02:00
if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
$this->leaveOptimizeFor($node, $env);
}
if (self::OPTIMIZE_RAW_FILTER === (self::OPTIMIZE_RAW_FILTER & $this->optimizers)) {
$node = $this->optimizeRawFilter($node, $env);
}
2013-08-01 21:20:12 +02:00
$node = $this->optimizePrintNode($node, $env);
2018-05-10 12:24:53 +02:00
if (self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('Twig_Extension_Sandbox')) {
2013-08-01 21:20:12 +02:00
if ($node instanceof Twig_Node_Body) {
$this->inABody = false;
} elseif ($this->inABody) {
2018-05-10 12:24:53 +02:00
if (!$expression && 'Twig_Node' !== get_class($node) && $prependedNodes = array_shift($this->prependedNodes)) {
2013-08-01 21:20:12 +02:00
$nodes = array();
foreach (array_unique($prependedNodes) as $name) {
2018-05-10 12:24:53 +02:00
$nodes[] = new Twig_Node_SetTemp($name, $node->getTemplateLine());
2013-08-01 21:20:12 +02:00
}
$nodes[] = $node;
$node = new Twig_Node($nodes);
}
}
}
return $node;
}
2018-05-10 12:24:53 +02:00
protected function optimizeVariables(Twig_NodeInterface $node, Twig_Environment $env)
2013-08-01 21:20:12 +02:00
{
if ('Twig_Node_Expression_Name' === get_class($node) && $node->isSimple()) {
$this->prependedNodes[0][] = $node->getAttribute('name');
2018-05-10 12:24:53 +02:00
return new Twig_Node_Expression_TempName($node->getAttribute('name'), $node->getTemplateLine());
2013-08-01 21:20:12 +02:00
}
2011-10-05 06:22:53 +02:00
return $node;
}
/**
2013-08-01 21:20:12 +02:00
* Optimizes print nodes.
*
* It replaces:
*
* * "echo $this->render(Parent)Block()" with "$this->display(Parent)Block()"
2011-10-05 06:22:53 +02:00
*
2018-05-10 12:24:53 +02:00
* @return Twig_NodeInterface
2011-10-05 06:22:53 +02:00
*/
2018-05-10 12:24:53 +02:00
protected function optimizePrintNode(Twig_NodeInterface $node, Twig_Environment $env)
2011-10-05 06:22:53 +02:00
{
2013-08-01 21:20:12 +02:00
if (!$node instanceof Twig_Node_Print) {
return $node;
}
2018-05-10 12:24:53 +02:00
$exprNode = $node->getNode('expr');
2013-08-01 21:20:12 +02:00
if (
2018-05-10 12:24:53 +02:00
$exprNode instanceof Twig_Node_Expression_BlockReference ||
$exprNode instanceof Twig_Node_Expression_Parent
2013-08-01 21:20:12 +02:00
) {
2018-05-10 12:24:53 +02:00
$exprNode->setAttribute('output', true);
2011-10-05 06:22:53 +02:00
2018-05-10 12:24:53 +02:00
return $exprNode;
2011-10-05 06:22:53 +02:00
}
return $node;
}
/**
* Removes "raw" filters.
*
2018-05-10 12:24:53 +02:00
* @return Twig_NodeInterface
2011-10-05 06:22:53 +02:00
*/
2018-05-10 12:24:53 +02:00
protected function optimizeRawFilter(Twig_NodeInterface $node, Twig_Environment $env)
2011-10-05 06:22:53 +02:00
{
if ($node instanceof Twig_Node_Expression_Filter && 'raw' == $node->getNode('filter')->getAttribute('value')) {
return $node->getNode('node');
}
return $node;
}
/**
* Optimizes "for" tag by removing the "loop" variable creation whenever possible.
*/
2018-05-10 12:24:53 +02:00
protected function enterOptimizeFor(Twig_NodeInterface $node, Twig_Environment $env)
2011-10-05 06:22:53 +02:00
{
if ($node instanceof Twig_Node_For) {
// disable the loop variable by default
$node->setAttribute('with_loop', false);
array_unshift($this->loops, $node);
2018-05-10 12:24:53 +02:00
array_unshift($this->loopsTargets, $node->getNode('value_target')->getAttribute('name'));
array_unshift($this->loopsTargets, $node->getNode('key_target')->getAttribute('name'));
2011-10-05 06:22:53 +02:00
} elseif (!$this->loops) {
// we are outside a loop
return;
}
// when do we need to add the loop variable back?
// the loop variable is referenced for the current loop
elseif ($node instanceof Twig_Node_Expression_Name && 'loop' === $node->getAttribute('name')) {
2018-05-10 12:24:53 +02:00
$node->setAttribute('always_defined', true);
2011-10-05 06:22:53 +02:00
$this->addLoopToCurrent();
}
2018-05-10 12:24:53 +02:00
// optimize access to loop targets
elseif ($node instanceof Twig_Node_Expression_Name && in_array($node->getAttribute('name'), $this->loopsTargets)) {
$node->setAttribute('always_defined', true);
}
2011-10-05 06:22:53 +02:00
// block reference
elseif ($node instanceof Twig_Node_BlockReference || $node instanceof Twig_Node_Expression_BlockReference) {
$this->addLoopToCurrent();
}
// include without the only attribute
elseif ($node instanceof Twig_Node_Include && !$node->getAttribute('only')) {
$this->addLoopToAll();
}
2018-05-10 12:24:53 +02:00
// include function without the with_context=false parameter
elseif ($node instanceof Twig_Node_Expression_Function
&& 'include' === $node->getAttribute('name')
&& (!$node->getNode('arguments')->hasNode('with_context')
|| false !== $node->getNode('arguments')->getNode('with_context')->getAttribute('value')
)
) {
$this->addLoopToAll();
}
2011-10-05 06:22:53 +02:00
// the loop variable is referenced via an attribute
elseif ($node instanceof Twig_Node_Expression_GetAttr
&& (!$node->getNode('attribute') instanceof Twig_Node_Expression_Constant
|| 'parent' === $node->getNode('attribute')->getAttribute('value')
)
&& (true === $this->loops[0]->getAttribute('with_loop')
|| ($node->getNode('node') instanceof Twig_Node_Expression_Name
&& 'loop' === $node->getNode('node')->getAttribute('name')
)
)
) {
$this->addLoopToAll();
}
}
/**
* Optimizes "for" tag by removing the "loop" variable creation whenever possible.
*/
2018-05-10 12:24:53 +02:00
protected function leaveOptimizeFor(Twig_NodeInterface $node, Twig_Environment $env)
2011-10-05 06:22:53 +02:00
{
if ($node instanceof Twig_Node_For) {
array_shift($this->loops);
2018-05-10 12:24:53 +02:00
array_shift($this->loopsTargets);
array_shift($this->loopsTargets);
2011-10-05 06:22:53 +02:00
}
}
protected function addLoopToCurrent()
{
$this->loops[0]->setAttribute('with_loop', true);
}
protected function addLoopToAll()
{
foreach ($this->loops as $loop) {
$loop->setAttribute('with_loop', true);
}
}
public function getPriority()
{
return 255;
}
}
2018-05-10 12:24:53 +02:00
class_alias('Twig_NodeVisitor_Optimizer', 'Twig\NodeVisitor\OptimizerNodeVisitor', false);