Compiler.php 3.0 KB

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