Compiler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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(string $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. * @return $this
  66. */
  67. public function raw(string $string)
  68. {
  69. $this->source .= $string;
  70. return $this;
  71. }
  72. /**
  73. * Adds a quoted string to the compiled code.
  74. *
  75. * @return $this
  76. */
  77. public function string(string $value)
  78. {
  79. $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
  80. return $this;
  81. }
  82. /**
  83. * Returns a PHP representation of a given value.
  84. *
  85. * @param mixed $value The value to convert
  86. *
  87. * @return $this
  88. */
  89. public function repr($value)
  90. {
  91. if (\is_int($value) || \is_float($value)) {
  92. if (false !== $locale = setlocale(\LC_NUMERIC, 0)) {
  93. setlocale(\LC_NUMERIC, 'C');
  94. }
  95. $this->raw($value);
  96. if (false !== $locale) {
  97. setlocale(\LC_NUMERIC, $locale);
  98. }
  99. } elseif (null === $value) {
  100. $this->raw('null');
  101. } elseif (\is_bool($value)) {
  102. $this->raw($value ? 'true' : 'false');
  103. } elseif (\is_array($value)) {
  104. $this->raw('[');
  105. $first = true;
  106. foreach ($value as $key => $value) {
  107. if (!$first) {
  108. $this->raw(', ');
  109. }
  110. $first = false;
  111. $this->repr($key);
  112. $this->raw(' => ');
  113. $this->repr($value);
  114. }
  115. $this->raw(']');
  116. } else {
  117. $this->string($value);
  118. }
  119. return $this;
  120. }
  121. }