Compiler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ExpressionLanguage;
  11. /**
  12. * Compiles a node to PHP code.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Compiler
  17. {
  18. private $source;
  19. private $functions;
  20. public function __construct(array $functions)
  21. {
  22. $this->functions = $functions;
  23. }
  24. public function getFunction($name)
  25. {
  26. return $this->functions[$name];
  27. }
  28. /**
  29. * Gets the current PHP code after compilation.
  30. *
  31. * @return string The PHP code
  32. */
  33. public function getSource()
  34. {
  35. return $this->source;
  36. }
  37. public function reset()
  38. {
  39. $this->source = '';
  40. return $this;
  41. }
  42. /**
  43. * Compiles a node.
  44. *
  45. * @return $this
  46. */
  47. public function compile(Node\Node $node)
  48. {
  49. $node->compile($this);
  50. return $this;
  51. }
  52. public function subcompile(Node\Node $node)
  53. {
  54. $current = $this->source;
  55. $this->source = '';
  56. $node->compile($this);
  57. $source = $this->source;
  58. $this->source = $current;
  59. return $source;
  60. }
  61. /**
  62. * Adds a raw string to the compiled code.
  63. *
  64. * @param string $string The string
  65. *
  66. * @return $this
  67. */
  68. public function raw($string)
  69. {
  70. $this->source .= $string;
  71. return $this;
  72. }
  73. /**
  74. * Adds a quoted string to the compiled code.
  75. *
  76. * @param string $value The string
  77. *
  78. * @return $this
  79. */
  80. public function string($value)
  81. {
  82. $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
  83. return $this;
  84. }
  85. /**
  86. * Returns a PHP representation of a given value.
  87. *
  88. * @param mixed $value The value to convert
  89. *
  90. * @return $this
  91. */
  92. public function repr($value)
  93. {
  94. if (\is_int($value) || \is_float($value)) {
  95. if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
  96. setlocale(LC_NUMERIC, 'C');
  97. }
  98. $this->raw($value);
  99. if (false !== $locale) {
  100. setlocale(LC_NUMERIC, $locale);
  101. }
  102. } elseif (null === $value) {
  103. $this->raw('null');
  104. } elseif (\is_bool($value)) {
  105. $this->raw($value ? 'true' : 'false');
  106. } elseif (\is_array($value)) {
  107. $this->raw('array(');
  108. $first = true;
  109. foreach ($value as $key => $value) {
  110. if (!$first) {
  111. $this->raw(', ');
  112. }
  113. $first = false;
  114. $this->repr($key);
  115. $this->raw(' => ');
  116. $this->repr($value);
  117. }
  118. $this->raw(')');
  119. } else {
  120. $this->string($value);
  121. }
  122. return $this;
  123. }
  124. }