Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d98fa2

Browse files
committed
[ExpressionLanguage] added the component
1 parent 091a96c commit 9d98fa2

38 files changed

+2414
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"symfony/doctrine-bridge": "self.version",
3434
"symfony/dom-crawler": "self.version",
3535
"symfony/event-dispatcher": "self.version",
36+
"symfony/expression-language": "self.version",
3637
"symfony/filesystem": "self.version",
3738
"symfony/finder": "self.version",
3839
"symfony/form": "self.version",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
2.4.0
5+
-----
6+
7+
* added the component
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ExpressionLanguage;
13+
14+
/**
15+
* Compiles a node to PHP code.
16+
*
17+
* @author Fabien Potencier <[email protected]>
18+
*/
19+
class Compiler
20+
{
21+
private $source;
22+
private $functions;
23+
24+
public function __construct(array $functions)
25+
{
26+
$this->functions = $functions;
27+
}
28+
29+
public function getFunction($name)
30+
{
31+
return $this->functions[$name];
32+
}
33+
34+
/**
35+
* Gets the current PHP code after compilation.
36+
*
37+
* @return string The PHP code
38+
*/
39+
public function getSource()
40+
{
41+
return $this->source;
42+
}
43+
44+
public function reset()
45+
{
46+
$this->source = '';
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* Compiles a node.
53+
*
54+
* @param Node\Node $node The node to compile
55+
*
56+
* @return Compiler The current compiler instance
57+
*/
58+
public function compile(Node\Node $node)
59+
{
60+
$node->compile($this);
61+
62+
return $this;
63+
}
64+
65+
public function subcompile(Node\Node $node)
66+
{
67+
$current = $this->source;
68+
$this->source = '';
69+
70+
$node->compile($this);
71+
72+
$source = $this->source;
73+
$this->source = $current;
74+
75+
return $source;
76+
}
77+
78+
/**
79+
* Adds a raw string to the compiled code.
80+
*
81+
* @param string $string The string
82+
*
83+
* @return Compiler The current compiler instance
84+
*/
85+
public function raw($string)
86+
{
87+
$this->source .= $string;
88+
89+
return $this;
90+
}
91+
92+
/**
93+
* Adds a quoted string to the compiled code.
94+
*
95+
* @param string $value The string
96+
*
97+
* @return Compiler The current compiler instance
98+
*/
99+
public function string($value)
100+
{
101+
$this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
102+
103+
return $this;
104+
}
105+
106+
/**
107+
* Returns a PHP representation of a given value.
108+
*
109+
* @param mixed $value The value to convert
110+
*
111+
* @return Compiler The current compiler instance
112+
*/
113+
public function repr($value)
114+
{
115+
if (is_int($value) || is_float($value)) {
116+
if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
117+
setlocale(LC_NUMERIC, 'C');
118+
}
119+
120+
$this->raw($value);
121+
122+
if (false !== $locale) {
123+
setlocale(LC_NUMERIC, $locale);
124+
}
125+
} elseif (null === $value) {
126+
$this->raw('null');
127+
} elseif (is_bool($value)) {
128+
$this->raw($value ? 'true' : 'false');
129+
} elseif (is_array($value)) {
130+
$this->raw('array(');
131+
$first = true;
132+
foreach ($value as $key => $value) {
133+
if (!$first) {
134+
$this->raw(', ');
135+
}
136+
$first = false;
137+
$this->repr($key);
138+
$this->raw(' => ');
139+
$this->repr($value);
140+
}
141+
$this->raw(')');
142+
} else {
143+
$this->string($value);
144+
}
145+
146+
return $this;
147+
}
148+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ExpressionLanguage;
13+
14+
/**
15+
* Represents an expression.
16+
*
17+
* @author Fabien Potencier <[email protected]>
18+
*/
19+
class Expression
20+
{
21+
private $expression;
22+
23+
/**
24+
* Constructor.
25+
*
26+
* @param string $expression An expression
27+
*/
28+
public function __construct($expression)
29+
{
30+
$this->expression = (string) $expression;
31+
}
32+
33+
/**
34+
* Gets the expression.
35+
*
36+
* @return string The expression
37+
*/
38+
public function __toString()
39+
{
40+
return $this->expression;
41+
}
42+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ExpressionLanguage;
13+
14+
/**
15+
* Allows to compile and evaluate expressions written in your own DSL.
16+
*
17+
* @author Fabien Potencier <[email protected]>
18+
*/
19+
class ExpressionLanguage
20+
{
21+
private $lexer;
22+
private $parser;
23+
private $compiler;
24+
private $cache;
25+
26+
protected $functions;
27+
28+
public function __construct()
29+
{
30+
$this->functions = array();
31+
$this->registerFunctions();
32+
}
33+
34+
/**
35+
* Compiles an expression source code.
36+
*
37+
* @param string $expression The expression to compile
38+
* @param array $names An array of valid names
39+
*
40+
* @return string The compiled PHP source code
41+
*/
42+
public function compile($expression, $names = array())
43+
{
44+
return $this->getCompiler()->compile($this->parse($expression, $names))->getSource();
45+
}
46+
47+
public function evaluate($expression, $values = array())
48+
{
49+
return $this->parse($expression, array_keys($values))->evaluate($this->functions, $values);
50+
}
51+
52+
public function addFunction($name, $compiler, $evaluator)
53+
{
54+
$this->functions[$name] = array('compiler' => $compiler, 'evaluator' => $evaluator);
55+
}
56+
57+
protected function registerFunctions()
58+
{
59+
$this->addFunction('constant', function ($constant) {
60+
return sprintf('constant(%s)', $constant);
61+
}, function (array $values, $constant) {
62+
return constant($constant);
63+
});
64+
}
65+
66+
private function getLexer()
67+
{
68+
if (null === $this->lexer) {
69+
$this->lexer = new Lexer();
70+
}
71+
72+
return $this->lexer;
73+
}
74+
75+
private function getParser()
76+
{
77+
if (null === $this->parser) {
78+
$this->parser = new Parser($this->functions);
79+
}
80+
81+
return $this->parser;
82+
}
83+
84+
private function getCompiler()
85+
{
86+
if (null === $this->compiler) {
87+
$this->compiler = new Compiler($this->functions);
88+
}
89+
90+
return $this->compiler->reset();
91+
}
92+
93+
private function parse($expression, $names)
94+
{
95+
$key = $expression.'//'.implode('-', $names);
96+
97+
if (!isset($this->cache[$key])) {
98+
$this->cache[$key] = $this->getParser()->parse($this->getLexer()->tokenize((string) $expression), $names);
99+
}
100+
101+
return $this->cache[$key];
102+
}
103+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2004-2013 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

0 commit comments

Comments
 (0)