Skip to content

Commit

Permalink
[ExpressionLanguage] optimized serialization of nodes and expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienbrault authored and fabpot committed Sep 21, 2013
1 parent 60b9f85 commit 5076ec7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php
Expand Up @@ -15,13 +15,13 @@

class BinaryNode extends Node
{
private $operators = array(
private static $operators = array(
'~' => '.',
'and' => '&&',
'or' => '||',
);

private $functions = array(
private static $functions = array(
'**' => 'pow',
'..' => 'range',
'in' => 'in_array',
Expand Down Expand Up @@ -50,9 +50,9 @@ public function compile(Compiler $compiler)
return;
}

if (isset($this->functions[$operator])) {
if (isset(self::$functions[$operator])) {
$compiler
->raw(sprintf('%s(', $this->functions[$operator]))
->raw(sprintf('%s(', self::$functions[$operator]))
->compile($this->nodes['left'])
->raw(', ')
->compile($this->nodes['right'])
Expand All @@ -62,8 +62,8 @@ public function compile(Compiler $compiler)
return;
}

if (isset($this->operators[$operator])) {
$operator = $this->operators[$operator];
if (isset(self::$operators[$operator])) {
$operator = self::$operators[$operator];
}

$compiler
Expand All @@ -83,12 +83,12 @@ public function evaluate($functions, $values)
$left = $this->nodes['left']->evaluate($functions, $values);
$right = $this->nodes['right']->evaluate($functions, $values);

if (isset($this->functions[$operator])) {
if (isset(self::$functions[$operator])) {
if ('not in' == $operator) {
return !call_user_func('in_array', $left, $right);
}

return call_user_func($this->functions[$operator], $left, $right);
return call_user_func(self::$functions[$operator], $left, $right);
}

switch ($operator) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/ExpressionLanguage/Node/UnaryNode.php
Expand Up @@ -15,7 +15,7 @@

class UnaryNode extends Node
{
private $operators = array(
private static $operators = array(
'!' => '!',
'not' => '!',
'+' => '+',
Expand All @@ -32,7 +32,7 @@ public function compile(Compiler $compiler)
{
$compiler
->raw('(')
->raw($this->operators[$this->attributes['operator']])
->raw(self::$operators[$this->attributes['operator']])
->compile($this->nodes['node'])
->raw(')')
;
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/ExpressionTest.php
@@ -0,0 +1,18 @@
<?php

namespace Symfony\Component\ExpressionLanguage\Tests;

use Symfony\Component\ExpressionLanguage\Expression;

class ExpressionTest extends \PHPUnit_Framework_TestCase
{
public function testSerialization()
{
$expression = new Expression('kernel.boot()');

$serializedExpression = serialize($expression);
$unserializedExpression = unserialize($serializedExpression);

$this->assertEquals($expression, $unserializedExpression);
}
}
Expand Up @@ -16,6 +16,18 @@

class ArrayNodeTest extends AbstractNodeTest
{
public function testSerialization()
{
$node = $this->createArrayNode();
$node->addElement(new ConstantNode('foo'));

$serializedNode = serialize($node);
$unserializedNode = unserialize($serializedNode);

$this->assertEquals($node, $unserializedNode);
$this->assertNotEquals($this->createArrayNode(), $unserializedNode);
}

public function getEvaluateData()
{
return array(
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/Node/NodeTest.php
Expand Up @@ -27,4 +27,14 @@ public function testToString()
EOF
, (string) $node);
}

public function testSerialization()
{
$node = new Node(array('foo' => 'bar'), array('bar' => 'foo'));

$serializedNode = serialize($node);
$unserializedNode = unserialize($serializedNode);

$this->assertEquals($node, $unserializedNode);
}
}
@@ -0,0 +1,19 @@
<?php

namespace Symfony\Component\ExpressionLanguage\Tests;

use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
use Symfony\Component\ExpressionLanguage\ParsedExpression;

class ParsedExpressionTest extends \PHPUnit_Framework_TestCase
{
public function testSerialization()
{
$expression = new ParsedExpression('25', new ConstantNode('25'));

$serializedExpression = serialize($expression);
$unserializedExpression = unserialize($serializedExpression);

$this->assertEquals($expression, $unserializedExpression);
}
}

0 comments on commit 5076ec7

Please sign in to comment.