4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\ExpressionLanguage;
15 * Compiles a node to PHP code.
17 * @author Fabien Potencier <fabien@symfony.com>
24 public function __construct(array $functions)
26 $this->functions = $functions;
29 public function getFunction($name)
31 return $this->functions[$name];
35 * Gets the current PHP code after compilation.
37 * @return string The PHP code
39 public function getSource()
44 public function reset()
54 * @param Node\Node $node The node to compile
58 public function compile(Node\Node $node)
60 $node->compile($this);
65 public function subcompile(Node\Node $node)
67 $current = $this->source;
70 $node->compile($this);
72 $source = $this->source;
73 $this->source = $current;
79 * Adds a raw string to the compiled code.
81 * @param string $string The string
85 public function raw($string)
87 $this->source .= $string;
93 * Adds a quoted string to the compiled code.
95 * @param string $value The string
99 public function string($value)
101 $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
107 * Returns a PHP representation of a given value.
109 * @param mixed $value The value to convert
113 public function repr($value)
115 if (is_int($value) || is_float($value)) {
116 if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
117 setlocale(LC_NUMERIC, 'C');
122 if (false !== $locale) {
123 setlocale(LC_NUMERIC, $locale);
125 } elseif (null === $value) {
127 } elseif (is_bool($value)) {
128 $this->raw($value ? 'true' : 'false');
129 } elseif (is_array($value)) {
130 $this->raw('array(');
132 foreach ($value as $key => $value) {
143 $this->string($value);