[ExpressionLanguage] optimized serialization of nodes and expressions
This commit is contained in:
parent
60b9f856fd
commit
5076ec7e90
@ -15,13 +15,13 @@ use Symfony\Component\ExpressionLanguage\Compiler;
|
|||||||
|
|
||||||
class BinaryNode extends Node
|
class BinaryNode extends Node
|
||||||
{
|
{
|
||||||
private $operators = array(
|
private static $operators = array(
|
||||||
'~' => '.',
|
'~' => '.',
|
||||||
'and' => '&&',
|
'and' => '&&',
|
||||||
'or' => '||',
|
'or' => '||',
|
||||||
);
|
);
|
||||||
|
|
||||||
private $functions = array(
|
private static $functions = array(
|
||||||
'**' => 'pow',
|
'**' => 'pow',
|
||||||
'..' => 'range',
|
'..' => 'range',
|
||||||
'in' => 'in_array',
|
'in' => 'in_array',
|
||||||
@ -50,9 +50,9 @@ class BinaryNode extends Node
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->functions[$operator])) {
|
if (isset(self::$functions[$operator])) {
|
||||||
$compiler
|
$compiler
|
||||||
->raw(sprintf('%s(', $this->functions[$operator]))
|
->raw(sprintf('%s(', self::$functions[$operator]))
|
||||||
->compile($this->nodes['left'])
|
->compile($this->nodes['left'])
|
||||||
->raw(', ')
|
->raw(', ')
|
||||||
->compile($this->nodes['right'])
|
->compile($this->nodes['right'])
|
||||||
@ -62,8 +62,8 @@ class BinaryNode extends Node
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->operators[$operator])) {
|
if (isset(self::$operators[$operator])) {
|
||||||
$operator = $this->operators[$operator];
|
$operator = self::$operators[$operator];
|
||||||
}
|
}
|
||||||
|
|
||||||
$compiler
|
$compiler
|
||||||
@ -83,12 +83,12 @@ class BinaryNode extends Node
|
|||||||
$left = $this->nodes['left']->evaluate($functions, $values);
|
$left = $this->nodes['left']->evaluate($functions, $values);
|
||||||
$right = $this->nodes['right']->evaluate($functions, $values);
|
$right = $this->nodes['right']->evaluate($functions, $values);
|
||||||
|
|
||||||
if (isset($this->functions[$operator])) {
|
if (isset(self::$functions[$operator])) {
|
||||||
if ('not in' == $operator) {
|
if ('not in' == $operator) {
|
||||||
return !call_user_func('in_array', $left, $right);
|
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) {
|
switch ($operator) {
|
||||||
|
@ -15,7 +15,7 @@ use Symfony\Component\ExpressionLanguage\Compiler;
|
|||||||
|
|
||||||
class UnaryNode extends Node
|
class UnaryNode extends Node
|
||||||
{
|
{
|
||||||
private $operators = array(
|
private static $operators = array(
|
||||||
'!' => '!',
|
'!' => '!',
|
||||||
'not' => '!',
|
'not' => '!',
|
||||||
'+' => '+',
|
'+' => '+',
|
||||||
@ -32,7 +32,7 @@ class UnaryNode extends Node
|
|||||||
{
|
{
|
||||||
$compiler
|
$compiler
|
||||||
->raw('(')
|
->raw('(')
|
||||||
->raw($this->operators[$this->attributes['operator']])
|
->raw(self::$operators[$this->attributes['operator']])
|
||||||
->compile($this->nodes['node'])
|
->compile($this->nodes['node'])
|
||||||
->raw(')')
|
->raw(')')
|
||||||
;
|
;
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -16,6 +16,18 @@ use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
|
|||||||
|
|
||||||
class ArrayNodeTest extends AbstractNodeTest
|
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()
|
public function getEvaluateData()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
|
@ -27,4 +27,14 @@ Node(
|
|||||||
EOF
|
EOF
|
||||||
, (string) $node);
|
, (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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user