Skip to content

Commit

Permalink
get PHP version info once on construction
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum authored and fabpot committed Jan 29, 2015
1 parent 6b434f4 commit 24ff698
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 21 deletions.
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.2.4"
"php": ">=5.2.7"
},
"autoload": {
"psr-0" : {
Expand Down
6 changes: 3 additions & 3 deletions lib/Twig/Autoloader.php
Expand Up @@ -23,10 +23,10 @@ class Twig_Autoloader
*/
public static function register($prepend = false)
{
if (version_compare(phpversion(), '5.3.0', '>=')) {
spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
} else {
if (PHP_VERSION_ID < 50300) {
spl_autoload_register(array(__CLASS__, 'autoload'));
} else {
spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Twig/Error.php
Expand Up @@ -57,7 +57,7 @@ class Twig_Error extends Exception
*/
public function __construct($message, $lineno = -1, $filename = null, Exception $previous = null)
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
if (PHP_VERSION_ID < 50300) {
$this->previous = $previous;
parent::__construct('');
} else {
Expand Down Expand Up @@ -188,7 +188,7 @@ protected function guessTemplateInfo()
$template = null;
$templateClass = null;

if (version_compare(phpversion(), '5.3.6', '>=')) {
if (PHP_VERSION_ID >= 50306) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
} else {
$backtrace = debug_backtrace();
Expand Down
6 changes: 2 additions & 4 deletions lib/Twig/Extension/Core.php
Expand Up @@ -617,7 +617,7 @@ function twig_urlencode_filter($url)
return rawurlencode($url);
}

if (version_compare(PHP_VERSION, '5.3.0', '<')) {
if (PHP_VERSION_ID < 50300) {
/**
* JSON encodes a variable.
*
Expand Down Expand Up @@ -1073,9 +1073,7 @@ function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html',
return $string;

case 'url':
// hackish test to avoid version_compare that is much slower, this works unless PHP releases a 5.10.*
// at that point however PHP 5.2.* support can be removed
if (PHP_VERSION < '5.3.0') {
if (PHP_VERSION_ID < 50300) {
return str_replace('%7E', '~', rawurlencode($string));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Node/Expression/Name.php
Expand Up @@ -46,7 +46,7 @@ public function compile(Twig_Compiler $compiler)
// 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 (version_compare(phpversion(), '5.4.0RC1', '>=')) {
if (PHP_VERSION_ID >= 50400) {
// PHP 5.4 ternary operator performance was optimized
$compiler
->raw('(isset($context[')
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/NodeVisitor/Optimizer.php
Expand Up @@ -56,7 +56,7 @@ public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
$this->enterOptimizeFor($node, $env);
}

if (!version_compare(phpversion(), '5.4.0RC1', '>=') && self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('sandbox')) {
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') {
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Test/NodeTestCase.php
Expand Up @@ -42,7 +42,7 @@ protected function getVariableGetter($name, $line = false)
{
$line = $line > 0 ? "// line {$line}\n" : '';

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

Expand Down
4 changes: 2 additions & 2 deletions test/Twig/Tests/Node/Expression/FilterTest.php
Expand Up @@ -64,7 +64,7 @@ public function getTests()
$tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)');

// filter as an anonymous function
if (version_compare(phpversion(), '5.3.0', '>=')) {
if (PHP_VERSION_ID >= 50300) {
$node = $this->createFilter(new Twig_Node_Expression_Constant('foo', 1), 'anonymous');
$tests[] = array($node, 'call_user_func_array($this->env->getFilter(\'anonymous\')->getCallable(), array("foo"))');
}
Expand Down Expand Up @@ -112,7 +112,7 @@ protected function createFilter($node, $name, array $arguments = array())

protected function getEnvironment()
{
if (version_compare(phpversion(), '5.3.0', '>=')) {
if (PHP_VERSION_ID >= 50300) {
return include 'PHP53/FilterInclude.php';
}

Expand Down
4 changes: 2 additions & 2 deletions test/Twig/Tests/Node/Expression/FunctionTest.php
Expand Up @@ -63,7 +63,7 @@ public function getTests()
$tests[] = array($node, 'twig_date_converter($this->env, 0, "America/Chicago")');

// function as an anonymous function
if (version_compare(phpversion(), '5.3.0', '>=')) {
if (PHP_VERSION_ID >= 50300) {
$node = $this->createFunction('anonymous', array(new Twig_Node_Expression_Constant('foo', 1)));
$tests[] = array($node, 'call_user_func_array($this->env->getFunction(\'anonymous\')->getCallable(), array("foo"))');
}
Expand All @@ -78,7 +78,7 @@ protected function createFunction($name, array $arguments = array())

protected function getEnvironment()
{
if (version_compare(phpversion(), '5.3.0', '>=')) {
if (PHP_VERSION_ID >= 50300) {
return include 'PHP53/FunctionInclude.php';
}

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".(version_compare(PHP_VERSION, '5.4.0') >= 0 ? '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))' : '$this->getContext($context, "foo")'), $env),
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, $this->getVariableGetter('foo', 1), $env1),
array($self, "// line 1\n\$this"),
array($context, "// line 1\n\$context"),
Expand Down
4 changes: 2 additions & 2 deletions test/Twig/Tests/Node/Expression/TestTest.php
Expand Up @@ -32,7 +32,7 @@ public function getTests()
$tests[] = array($node, '(null === "foo")');

// test as an anonymous function
if (version_compare(phpversion(), '5.3.0', '>=')) {
if (PHP_VERSION_ID >= 50300) {
$node = $this->createTest(new Twig_Node_Expression_Constant('foo', 1), 'anonymous', array(new Twig_Node_Expression_Constant('foo', 1)));
$tests[] = array($node, 'call_user_func_array($this->env->getTest(\'anonymous\')->getCallable(), array("foo", "foo"))');
}
Expand All @@ -47,7 +47,7 @@ protected function createTest($node, $name, array $arguments = array())

protected function getEnvironment()
{
if (version_compare(phpversion(), '5.3.0', '>=')) {
if (PHP_VERSION_ID >= 50300) {
return include 'PHP53/TestInclude.php';
}

Expand Down
2 changes: 1 addition & 1 deletion test/Twig/Tests/NodeVisitor/OptimizerTest.php
Expand Up @@ -36,7 +36,7 @@ public function testRenderParentBlockOptimizer()

public function testRenderVariableBlockOptimizer()
{
if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
if (PHP_VERSION_ID >= 50400) {
return;
}

Expand Down

0 comments on commit 24ff698

Please sign in to comment.