Skip to content

Commit

Permalink
drop support for PHP 5.3 and 5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 2, 2015
1 parent 176f60d commit 706d659
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 138 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
@@ -1,8 +1,6 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG
@@ -1,6 +1,6 @@
* 2.0.0 (201X-XX-XX)

* dropped support for PHP 5.2
* dropped support for PHP 5.2, 5.3, and 5.4
* removed the ability to register a global variable after the runtime or the extensions have been initialized
* improved the performance of the filesystem loader
* removed features that were deprecated in 1.x
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -27,7 +27,7 @@
"forum": "https://groups.google.com/forum/#!forum/twig-users"
},
"require": {
"php": ">=5.3.3"
"php": ">=5.5.0"
},
"autoload": {
"psr-0" : {
Expand Down
2 changes: 1 addition & 1 deletion doc/intro.rst
Expand Up @@ -24,7 +24,7 @@ The key-features are...
Prerequisites
-------------

Twig needs at least **PHP 5.3.3** to run.
Twig needs at least **PHP 5.5.0** to run.

Installation
------------
Expand Down
7 changes: 1 addition & 6 deletions lib/Twig/Error.php
Expand Up @@ -163,12 +163,7 @@ protected function guessTemplateInfo()
$template = null;
$templateClass = null;

if (PHP_VERSION_ID >= 50306) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
} else {
$backtrace = debug_backtrace();
}

$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
foreach ($backtrace as $trace) {
if (isset($trace['object']) && $trace['object'] instanceof Twig_Template && 'Twig_Template' !== get_class($trace['object'])) {
$currentClass = get_class($trace['object']);
Expand Down
39 changes: 10 additions & 29 deletions lib/Twig/Node/Expression/Name.php
Expand Up @@ -43,37 +43,18 @@ public function compile(Twig_Compiler $compiler)
->raw(']')
;
} else {
// remove the non-PHP 5.4 version when PHP 5.3 support is dropped
// as the non-optimized version is just a workaround for slow ternary operator
// when the context has a lot of variables
if (PHP_VERSION_ID >= 50400) {
// PHP 5.4 ternary operator performance was optimized
$compiler
->raw('(isset($context[')
->string($name)
->raw(']) ? $context[')
->string($name)
->raw('] : ')
;
$compiler
->raw('(isset($context[')
->string($name)
->raw(']) ? $context[')
->string($name)
->raw('] : ')
;

if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
$compiler->raw('null)');
} else {
$compiler->raw('$this->getContext($context, ')->string($name)->raw('))');
}
if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
$compiler->raw('null)');
} else {
$compiler
->raw('$this->getContext($context, ')
->string($name)
;

if ($this->getAttribute('ignore_strict_check')) {
$compiler->raw(', true');
}

$compiler
->raw(')')
;
$compiler->raw('$this->getContext($context, ')->string($name)->raw('))');
}
}
}
Expand Down
35 changes: 0 additions & 35 deletions lib/Twig/Node/SetTemp.php

This file was deleted.

43 changes: 1 addition & 42 deletions lib/Twig/NodeVisitor/Optimizer.php
Expand Up @@ -25,13 +25,13 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface
const OPTIMIZE_NONE = 0;
const OPTIMIZE_FOR = 2;
const OPTIMIZE_RAW_FILTER = 4;
// obsolete, does not do anything
const OPTIMIZE_VAR_ACCESS = 8;

protected $loops = array();
protected $loopsTargets = array();
protected $optimizers;
protected $prependedNodes = array();
protected $inABody = false;

/**
* Constructor.
Expand All @@ -56,20 +56,6 @@ public function enterNode(Twig_Node $node, Twig_Environment $env)
$this->enterOptimizeFor($node, $env);
}

if (PHP_VERSION_ID < 50400 && self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('sandbox')) {
if ($this->inABody) {
if (!$node instanceof Twig_Node_Expression) {
if (get_class($node) !== 'Twig_Node') {
array_unshift($this->prependedNodes, array());
}
} else {
$node = $this->optimizeVariables($node, $env);
}
} elseif ($node instanceof Twig_Node_Body) {
$this->inABody = true;
}
}

return $node;
}

Expand All @@ -90,33 +76,6 @@ public function leaveNode(Twig_Node $node, Twig_Environment $env)

$node = $this->optimizePrintNode($node, $env);

if (self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('sandbox')) {
if ($node instanceof Twig_Node_Body) {
$this->inABody = false;
} elseif ($this->inABody) {
if (!$expression && get_class($node) !== 'Twig_Node' && $prependedNodes = array_shift($this->prependedNodes)) {
$nodes = array();
foreach (array_unique($prependedNodes) as $name) {
$nodes[] = new Twig_Node_SetTemp($name, $node->getLine());
}

$nodes[] = $node;
$node = new Twig_Node($nodes);
}
}
}

return $node;
}

protected function optimizeVariables(Twig_Node $node, Twig_Environment $env)
{
if ('Twig_Node_Expression_Name' === get_class($node) && $node->isSimple()) {
$this->prependedNodes[0][] = $node->getAttribute('name');

return new Twig_Node_Expression_TempName($node->getAttribute('name'), $node->getLine());
}

return $node;
}

Expand Down
6 changes: 1 addition & 5 deletions lib/Twig/Test/NodeTestCase.php
Expand Up @@ -42,11 +42,7 @@ protected function getVariableGetter($name, $line = false)
{
$line = $line > 0 ? "// line {$line}\n" : '';

if (PHP_VERSION_ID >= 50400) {
return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
}

return sprintf('%s$this->getContext($context, "%s")', $line, $name);
return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
}

protected function getAttributeGetter()
Expand Down
2 changes: 1 addition & 1 deletion test/Twig/Tests/Node/Expression/NameTest.php
Expand Up @@ -28,7 +28,7 @@ public function getTests()
$env1 = new Twig_Environment(null, array('strict_variables' => false));

return array(
array($node, "// line 1\n".(PHP_VERSION_ID >= 50400 ? '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))' : '$this->getContext($context, "foo")'), $env),
array($node, "// line 1\n".'(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))', $env),
array($node, $this->getVariableGetter('foo', 1), $env1),
array($self, "// line 1\n\$this"),
array($context, "// line 1\n\$context"),
Expand Down
15 changes: 0 additions & 15 deletions test/Twig/Tests/NodeVisitor/OptimizerTest.php
Expand Up @@ -34,21 +34,6 @@ public function testRenderParentBlockOptimizer()
$this->assertTrue($node->getAttribute('output'));
}

public function testRenderVariableBlockOptimizer()
{
if (PHP_VERSION_ID >= 50400) {
return;
}

$env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false));
$stream = $env->parse($env->tokenize('{{ block(name|lower) }}', 'index'));

$node = $stream->getNode('body')->getNode(0)->getNode(1);

$this->assertEquals('Twig_Node_Expression_BlockReference', get_class($node));
$this->assertTrue($node->getAttribute('output'));
}

/**
* @dataProvider getTestsForForOptimizer
*/
Expand Down

0 comments on commit 706d659

Please sign in to comment.