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

@@ -9,25 +9,29 @@
* file that was distributed with this source code.
*/
namespace Twig\Profiler\Dumper;
use Twig\Profiler\Profile;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class Twig_Profiler_Dumper_Base
abstract class BaseDumper
{
private $root;
public function dump(Twig_Profiler_Profile $profile)
public function dump(Profile $profile)
{
return $this->dumpProfile($profile);
}
abstract protected function formatTemplate(Twig_Profiler_Profile $profile, $prefix);
abstract protected function formatTemplate(Profile $profile, $prefix);
abstract protected function formatNonTemplate(Twig_Profiler_Profile $profile, $prefix);
abstract protected function formatNonTemplate(Profile $profile, $prefix);
abstract protected function formatTime(Twig_Profiler_Profile $profile, $percent);
abstract protected function formatTime(Profile $profile, $percent);
private function dumpProfile(Twig_Profiler_Profile $profile, $prefix = '', $sibling = false)
private function dumpProfile(Profile $profile, $prefix = '', $sibling = false)
{
if ($profile->isRoot()) {
$this->root = $profile->getDuration();
@@ -49,7 +53,7 @@ abstract class Twig_Profiler_Dumper_Base
$str = sprintf("%s %s\n", $start, $this->formatTime($profile, $percent));
}
$nCount = count($profile->getProfiles());
$nCount = \count($profile->getProfiles());
foreach ($profile as $i => $p) {
$str .= $this->dumpProfile($p, $prefix, $i + 1 !== $nCount);
}
@@ -58,5 +62,4 @@ abstract class Twig_Profiler_Dumper_Base
}
}
class_alias('Twig_Profiler_Dumper_Base', 'Twig\Profiler\Dumper\BaseDumper', false);
class_exists('Twig_Profiler_Profile');
class_alias('Twig\Profiler\Dumper\BaseDumper', 'Twig_Profiler_Dumper_Base');

View File

@@ -9,16 +9,20 @@
* file that was distributed with this source code.
*/
namespace Twig\Profiler\Dumper;
use Twig\Profiler\Profile;
/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @final
*/
class Twig_Profiler_Dumper_Blackfire
class BlackfireDumper
{
public function dump(Twig_Profiler_Profile $profile)
public function dump(Profile $profile)
{
$data = array();
$data = [];
$this->dumpProfile('main()', $profile, $data);
$this->dumpChildren('main()', $profile, $data);
@@ -38,7 +42,7 @@ EOF;
return $str;
}
private function dumpChildren($parent, Twig_Profiler_Profile $profile, &$data)
private function dumpChildren($parent, Profile $profile, &$data)
{
foreach ($profile as $p) {
if ($p->isTemplate()) {
@@ -51,22 +55,22 @@ EOF;
}
}
private function dumpProfile($edge, Twig_Profiler_Profile $profile, &$data)
private function dumpProfile($edge, Profile $profile, &$data)
{
if (isset($data[$edge])) {
$data[$edge]['ct'] += 1;
++$data[$edge]['ct'];
$data[$edge]['wt'] += floor($profile->getDuration() * 1000000);
$data[$edge]['mu'] += $profile->getMemoryUsage();
$data[$edge]['pmu'] += $profile->getPeakMemoryUsage();
} else {
$data[$edge] = array(
$data[$edge] = [
'ct' => 1,
'wt' => floor($profile->getDuration() * 1000000),
'mu' => $profile->getMemoryUsage(),
'pmu' => $profile->getPeakMemoryUsage(),
);
];
}
}
}
class_alias('Twig_Profiler_Dumper_Blackfire', 'Twig\Profiler\Dumper\BlackfireDumper', false);
class_alias('Twig\Profiler\Dumper\BlackfireDumper', 'Twig_Profiler_Dumper_Blackfire');

View File

@@ -9,39 +9,43 @@
* file that was distributed with this source code.
*/
namespace Twig\Profiler\Dumper;
use Twig\Profiler\Profile;
/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @final
*/
class Twig_Profiler_Dumper_Html extends Twig_Profiler_Dumper_Base
class HtmlDumper extends BaseDumper
{
private static $colors = array(
private static $colors = [
'block' => '#dfd',
'macro' => '#ddf',
'template' => '#ffd',
'big' => '#d44',
);
];
public function dump(Twig_Profiler_Profile $profile)
public function dump(Profile $profile)
{
return '<pre>'.parent::dump($profile).'</pre>';
}
protected function formatTemplate(Twig_Profiler_Profile $profile, $prefix)
protected function formatTemplate(Profile $profile, $prefix)
{
return sprintf('%s└ <span style="background-color: %s">%s</span>', $prefix, self::$colors['template'], $profile->getTemplate());
}
protected function formatNonTemplate(Twig_Profiler_Profile $profile, $prefix)
protected function formatNonTemplate(Profile $profile, $prefix)
{
return sprintf('%s└ %s::%s(<span style="background-color: %s">%s</span>)', $prefix, $profile->getTemplate(), $profile->getType(), isset(self::$colors[$profile->getType()]) ? self::$colors[$profile->getType()] : 'auto', $profile->getName());
}
protected function formatTime(Twig_Profiler_Profile $profile, $percent)
protected function formatTime(Profile $profile, $percent)
{
return sprintf('<span style="color: %s">%.2fms/%.0f%%</span>', $percent > 20 ? self::$colors['big'] : 'auto', $profile->getDuration() * 1000, $percent);
}
}
class_alias('Twig_Profiler_Dumper_Html', 'Twig\Profiler\Dumper\HtmlDumper', false);
class_alias('Twig\Profiler\Dumper\HtmlDumper', 'Twig_Profiler_Dumper_Html');

View File

@@ -9,27 +9,31 @@
* file that was distributed with this source code.
*/
namespace Twig\Profiler\Dumper;
use Twig\Profiler\Profile;
/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @final
*/
class Twig_Profiler_Dumper_Text extends Twig_Profiler_Dumper_Base
class TextDumper extends BaseDumper
{
protected function formatTemplate(Twig_Profiler_Profile $profile, $prefix)
protected function formatTemplate(Profile $profile, $prefix)
{
return sprintf('%s└ %s', $prefix, $profile->getTemplate());
}
protected function formatNonTemplate(Twig_Profiler_Profile $profile, $prefix)
protected function formatNonTemplate(Profile $profile, $prefix)
{
return sprintf('%s└ %s::%s(%s)', $prefix, $profile->getTemplate(), $profile->getType(), $profile->getName());
}
protected function formatTime(Twig_Profiler_Profile $profile, $percent)
protected function formatTime(Profile $profile, $percent)
{
return sprintf('%.2fms/%.0f%%', $profile->getDuration() * 1000, $percent);
}
}
class_alias('Twig_Profiler_Dumper_Text', 'Twig\Profiler\Dumper\TextDumper', false);
class_alias('Twig\Profiler\Dumper\TextDumper', 'Twig_Profiler_Dumper_Text');

View File

@@ -9,25 +9,30 @@
* file that was distributed with this source code.
*/
namespace Twig\Profiler\Node;
use Twig\Compiler;
use Twig\Node\Node;
/**
* Represents a profile enter node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Profiler_Node_EnterProfile extends Twig_Node
class EnterProfileNode extends Node
{
public function __construct($extensionName, $type, $name, $varName)
{
parent::__construct(array(), array('extension_name' => $extensionName, 'name' => $name, 'type' => $type, 'var_name' => $varName));
parent::__construct([], ['extension_name' => $extensionName, 'name' => $name, 'type' => $type, 'var_name' => $varName]);
}
public function compile(Twig_Compiler $compiler)
public function compile(Compiler $compiler)
{
$compiler
->write(sprintf('$%s = $this->env->getExtension(', $this->getAttribute('var_name')))
->repr($this->getAttribute('extension_name'))
->raw(");\n")
->write(sprintf('$%s->enter($%s = new Twig_Profiler_Profile($this->getTemplateName(), ', $this->getAttribute('var_name'), $this->getAttribute('var_name').'_prof'))
->write(sprintf('$%s->enter($%s = new \Twig\Profiler\Profile($this->getTemplateName(), ', $this->getAttribute('var_name'), $this->getAttribute('var_name').'_prof'))
->repr($this->getAttribute('type'))
->raw(', ')
->repr($this->getAttribute('name'))
@@ -36,4 +41,4 @@ class Twig_Profiler_Node_EnterProfile extends Twig_Node
}
}
class_alias('Twig_Profiler_Node_EnterProfile', 'Twig\Profiler\Node\EnterProfileNode', false);
class_alias('Twig\Profiler\Node\EnterProfileNode', 'Twig_Profiler_Node_EnterProfile');

View File

@@ -9,19 +9,24 @@
* file that was distributed with this source code.
*/
namespace Twig\Profiler\Node;
use Twig\Compiler;
use Twig\Node\Node;
/**
* Represents a profile leave node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Profiler_Node_LeaveProfile extends Twig_Node
class LeaveProfileNode extends Node
{
public function __construct($varName)
{
parent::__construct(array(), array('var_name' => $varName));
parent::__construct([], ['var_name' => $varName]);
}
public function compile(Twig_Compiler $compiler)
public function compile(Compiler $compiler)
{
$compiler
->write("\n")
@@ -30,4 +35,4 @@ class Twig_Profiler_Node_LeaveProfile extends Twig_Node
}
}
class_alias('Twig_Profiler_Node_LeaveProfile', 'Twig\Profiler\Node\LeaveProfileNode', false);
class_alias('Twig\Profiler\Node\LeaveProfileNode', 'Twig_Profiler_Node_LeaveProfile');

View File

@@ -1,67 +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.
*/
/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @final
*/
class Twig_Profiler_NodeVisitor_Profiler extends Twig_BaseNodeVisitor
{
private $extensionName;
public function __construct($extensionName)
{
$this->extensionName = $extensionName;
}
protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
{
return $node;
}
protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
{
if ($node instanceof Twig_Node_Module) {
$varName = $this->getVarName();
$node->setNode('display_start', new Twig_Node(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::TEMPLATE, $node->getTemplateName(), $varName), $node->getNode('display_start'))));
$node->setNode('display_end', new Twig_Node(array(new Twig_Profiler_Node_LeaveProfile($varName), $node->getNode('display_end'))));
} elseif ($node instanceof Twig_Node_Block) {
$varName = $this->getVarName();
$node->setNode('body', new Twig_Node_Body(array(
new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::BLOCK, $node->getAttribute('name'), $varName),
$node->getNode('body'),
new Twig_Profiler_Node_LeaveProfile($varName),
)));
} elseif ($node instanceof Twig_Node_Macro) {
$varName = $this->getVarName();
$node->setNode('body', new Twig_Node_Body(array(
new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::MACRO, $node->getAttribute('name'), $varName),
$node->getNode('body'),
new Twig_Profiler_Node_LeaveProfile($varName),
)));
}
return $node;
}
private function getVarName()
{
return sprintf('__internal_%s', hash('sha256', uniqid(mt_rand(), true), false));
}
public function getPriority()
{
return 0;
}
}
class_alias('Twig_Profiler_NodeVisitor_Profiler', 'Twig\Profiler\NodeVisitor\ProfilerNodeVisitor', false);

View File

@@ -0,0 +1,80 @@
<?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\Profiler\NodeVisitor;
use Twig\Environment;
use Twig\Node\BlockNode;
use Twig\Node\BodyNode;
use Twig\Node\MacroNode;
use Twig\Node\ModuleNode;
use Twig\Node\Node;
use Twig\NodeVisitor\AbstractNodeVisitor;
use Twig\Profiler\Node\EnterProfileNode;
use Twig\Profiler\Node\LeaveProfileNode;
use Twig\Profiler\Profile;
/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @final
*/
class ProfilerNodeVisitor extends AbstractNodeVisitor
{
private $extensionName;
public function __construct($extensionName)
{
$this->extensionName = $extensionName;
}
protected function doEnterNode(Node $node, Environment $env)
{
return $node;
}
protected function doLeaveNode(Node $node, Environment $env)
{
if ($node instanceof ModuleNode) {
$varName = $this->getVarName();
$node->setNode('display_start', new Node([new EnterProfileNode($this->extensionName, Profile::TEMPLATE, $node->getTemplateName(), $varName), $node->getNode('display_start')]));
$node->setNode('display_end', new Node([new LeaveProfileNode($varName), $node->getNode('display_end')]));
} elseif ($node instanceof BlockNode) {
$varName = $this->getVarName();
$node->setNode('body', new BodyNode([
new EnterProfileNode($this->extensionName, Profile::BLOCK, $node->getAttribute('name'), $varName),
$node->getNode('body'),
new LeaveProfileNode($varName),
]));
} elseif ($node instanceof MacroNode) {
$varName = $this->getVarName();
$node->setNode('body', new BodyNode([
new EnterProfileNode($this->extensionName, Profile::MACRO, $node->getAttribute('name'), $varName),
$node->getNode('body'),
new LeaveProfileNode($varName),
]));
}
return $node;
}
private function getVarName()
{
return sprintf('__internal_%s', hash('sha256', $this->extensionName));
}
public function getPriority()
{
return 0;
}
}
class_alias('Twig\Profiler\NodeVisitor\ProfilerNodeVisitor', 'Twig_Profiler_NodeVisitor_Profiler');

View File

@@ -9,12 +9,14 @@
* file that was distributed with this source code.
*/
namespace Twig\Profiler;
/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @final
*/
class Twig_Profiler_Profile implements IteratorAggregate, Serializable
class Profile implements \IteratorAggregate, \Serializable
{
const ROOT = 'ROOT';
const BLOCK = 'block';
@@ -24,9 +26,9 @@ class Twig_Profiler_Profile implements IteratorAggregate, Serializable
private $template;
private $name;
private $type;
private $starts = array();
private $ends = array();
private $profiles = array();
private $starts = [];
private $ends = [];
private $profiles = [];
public function __construct($template = 'main', $type = self::ROOT, $name = 'main')
{
@@ -76,7 +78,7 @@ class Twig_Profiler_Profile implements IteratorAggregate, Serializable
return $this->profiles;
}
public function addProfile(Twig_Profiler_Profile $profile)
public function addProfile(self $profile)
{
$this->profiles[] = $profile;
}
@@ -84,7 +86,7 @@ class Twig_Profiler_Profile implements IteratorAggregate, Serializable
/**
* Returns the duration in microseconds.
*
* @return int
* @return float
*/
public function getDuration()
{
@@ -126,11 +128,11 @@ class Twig_Profiler_Profile implements IteratorAggregate, Serializable
*/
public function enter()
{
$this->starts = array(
$this->starts = [
'wt' => microtime(true),
'mu' => memory_get_usage(),
'pmu' => memory_get_peak_usage(),
);
];
}
/**
@@ -138,33 +140,49 @@ class Twig_Profiler_Profile implements IteratorAggregate, Serializable
*/
public function leave()
{
$this->ends = array(
$this->ends = [
'wt' => microtime(true),
'mu' => memory_get_usage(),
'pmu' => memory_get_peak_usage(),
);
];
}
public function reset()
{
$this->starts = $this->ends = $this->profiles = array();
$this->starts = $this->ends = $this->profiles = [];
$this->enter();
}
public function getIterator()
{
return new ArrayIterator($this->profiles);
return new \ArrayIterator($this->profiles);
}
public function serialize()
{
return serialize(array($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles));
return serialize($this->__serialize());
}
public function unserialize($data)
{
list($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles) = unserialize($data);
$this->__unserialize(unserialize($data));
}
/**
* @internal
*/
public function __serialize()
{
return [$this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles];
}
/**
* @internal
*/
public function __unserialize(array $data)
{
list($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles) = $data;
}
}
class_alias('Twig_Profiler_Profile', 'Twig\Profiler\Profile', false);
class_alias('Twig\Profiler\Profile', 'Twig_Profiler_Profile');