123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace Symfony\Component\ExpressionLanguage;
- class Compiler
- {
- private $source;
- private $functions;
- public function __construct(array $functions)
- {
- $this->functions = $functions;
- }
- public function getFunction($name)
- {
- return $this->functions[$name];
- }
-
- public function getSource()
- {
- return $this->source;
- }
- public function reset()
- {
- $this->source = '';
- return $this;
- }
-
- public function compile(Node\Node $node)
- {
- $node->compile($this);
- return $this;
- }
- public function subcompile(Node\Node $node)
- {
- $current = $this->source;
- $this->source = '';
- $node->compile($this);
- $source = $this->source;
- $this->source = $current;
- return $source;
- }
-
- public function raw($string)
- {
- $this->source .= $string;
- return $this;
- }
-
- public function string($value)
- {
- $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
- return $this;
- }
-
- public function repr($value)
- {
- if (\is_int($value) || \is_float($value)) {
- if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
- setlocale(LC_NUMERIC, 'C');
- }
- $this->raw($value);
- if (false !== $locale) {
- setlocale(LC_NUMERIC, $locale);
- }
- } elseif (null === $value) {
- $this->raw('null');
- } elseif (\is_bool($value)) {
- $this->raw($value ? 'true' : 'false');
- } elseif (\is_array($value)) {
- $this->raw('[');
- $first = true;
- foreach ($value as $key => $value) {
- if (!$first) {
- $this->raw(', ');
- }
- $first = false;
- $this->repr($key);
- $this->raw(' => ');
- $this->repr($value);
- }
- $this->raw(']');
- } else {
- $this->string($value);
- }
- return $this;
- }
- }
|