| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier |
| 7 |
* (c) Armin Ronacher |
| 8 |
* |
| 9 |
* For the full copyright and license information, please view the LICENSE |
| 10 |
* file that was distributed with this source code. |
| 11 |
*/ |
| 12 |
namespace ElementorDeps\Twig; |
| 13 |
|
| 14 |
use ElementorDeps\Twig\Node\Node; |
| 15 |
/** |
| 16 |
* @author Fabien Potencier <fabien@symfony.com> |
| 17 |
*/ |
| 18 |
class Compiler |
| 19 |
{ |
| 20 |
private $lastLine; |
| 21 |
private $source; |
| 22 |
private $indentation; |
| 23 |
private $env; |
| 24 |
private $debugInfo = []; |
| 25 |
private $sourceOffset; |
| 26 |
private $sourceLine; |
| 27 |
private $varNameSalt = 0; |
| 28 |
private $didUseEcho = \false; |
| 29 |
private $didUseEchoStack = []; |
| 30 |
public function __construct(Environment $env) |
| 31 |
{ |
| 32 |
$this->env = $env; |
| 33 |
} |
| 34 |
public function getEnvironment() : Environment |
| 35 |
{ |
| 36 |
return $this->env; |
| 37 |
} |
| 38 |
public function getSource() : string |
| 39 |
{ |
| 40 |
return $this->source; |
| 41 |
} |
| 42 |
/** |
| 43 |
* @return $this |
| 44 |
*/ |
| 45 |
public function reset(int $indentation = 0) |
| 46 |
{ |
| 47 |
$this->lastLine = null; |
| 48 |
$this->source = ''; |
| 49 |
$this->debugInfo = []; |
| 50 |
$this->sourceOffset = 0; |
| 51 |
// source code starts at 1 (as we then increment it when we encounter new lines) |
| 52 |
$this->sourceLine = 1; |
| 53 |
$this->indentation = $indentation; |
| 54 |
$this->varNameSalt = 0; |
| 55 |
return $this; |
| 56 |
} |
| 57 |
/** |
| 58 |
* @return $this |
| 59 |
*/ |
| 60 |
public function compile(Node $node, int $indentation = 0) |
| 61 |
{ |
| 62 |
$this->reset($indentation); |
| 63 |
$this->didUseEchoStack[] = $this->didUseEcho; |
| 64 |
try { |
| 65 |
$this->didUseEcho = \false; |
| 66 |
$node->compile($this); |
| 67 |
if ($this->didUseEcho) { |
| 68 |
trigger_deprecation('twig/twig', '3.9', 'Using "%s" is deprecated, use "yield" instead in "%s", then flag the class with #[YieldReady].', $this->didUseEcho, \get_class($node)); |
| 69 |
} |
| 70 |
return $this; |
| 71 |
} finally { |
| 72 |
$this->didUseEcho = \array_pop($this->didUseEchoStack); |
| 73 |
} |
| 74 |
} |
| 75 |
/** |
| 76 |
* @return $this |
| 77 |
*/ |
| 78 |
public function subcompile(Node $node, bool $raw = \true) |
| 79 |
{ |
| 80 |
if (!$raw) { |
| 81 |
$this->source .= \str_repeat(' ', $this->indentation * 4); |
| 82 |
} |
| 83 |
$this->didUseEchoStack[] = $this->didUseEcho; |
| 84 |
try { |
| 85 |
$this->didUseEcho = \false; |
| 86 |
$node->compile($this); |
| 87 |
if ($this->didUseEcho) { |
| 88 |
trigger_deprecation('twig/twig', '3.9', 'Using "%s" is deprecated, use "yield" instead in "%s", then flag the class with #[YieldReady].', $this->didUseEcho, \get_class($node)); |
| 89 |
} |
| 90 |
return $this; |
| 91 |
} finally { |
| 92 |
$this->didUseEcho = \array_pop($this->didUseEchoStack); |
| 93 |
} |
| 94 |
} |
| 95 |
/** |
| 96 |
* Adds a raw string to the compiled code. |
| 97 |
* |
| 98 |
* @return $this |
| 99 |
*/ |
| 100 |
public function raw(string $string) |
| 101 |
{ |
| 102 |
$this->checkForEcho($string); |
| 103 |
$this->source .= $string; |
| 104 |
return $this; |
| 105 |
} |
| 106 |
/** |
| 107 |
* Writes a string to the compiled code by adding indentation. |
| 108 |
* |
| 109 |
* @return $this |
| 110 |
*/ |
| 111 |
public function write(...$strings) |
| 112 |
{ |
| 113 |
foreach ($strings as $string) { |
| 114 |
$this->checkForEcho($string); |
| 115 |
$this->source .= \str_repeat(' ', $this->indentation * 4) . $string; |
| 116 |
} |
| 117 |
return $this; |
| 118 |
} |
| 119 |
/** |
| 120 |
* Adds a quoted string to the compiled code. |
| 121 |
* |
| 122 |
* @return $this |
| 123 |
*/ |
| 124 |
public function string(string $value) |
| 125 |
{ |
| 126 |
$this->source .= \sprintf('"%s"', \addcslashes($value, "\x00\t\"\$\\")); |
| 127 |
return $this; |
| 128 |
} |
| 129 |
/** |
| 130 |
* Returns a PHP representation of a given value. |
| 131 |
* |
| 132 |
* @return $this |
| 133 |
*/ |
| 134 |
public function repr($value) |
| 135 |
{ |
| 136 |
if (\is_int($value) || \is_float($value)) { |
| 137 |
if (\false !== ($locale = \setlocale(\LC_NUMERIC, '0'))) { |
| 138 |
\setlocale(\LC_NUMERIC, 'C'); |
| 139 |
} |
| 140 |
$this->raw(\var_export($value, \true)); |
| 141 |
if (\false !== $locale) { |
| 142 |
\setlocale(\LC_NUMERIC, $locale); |
| 143 |
} |
| 144 |
} elseif (null === $value) { |
| 145 |
$this->raw('null'); |
| 146 |
} elseif (\is_bool($value)) { |
| 147 |
$this->raw($value ? 'true' : 'false'); |
| 148 |
} elseif (\is_array($value)) { |
| 149 |
$this->raw('array('); |
| 150 |
$first = \true; |
| 151 |
foreach ($value as $key => $v) { |
| 152 |
if (!$first) { |
| 153 |
$this->raw(', '); |
| 154 |
} |
| 155 |
$first = \false; |
| 156 |
$this->repr($key); |
| 157 |
$this->raw(' => '); |
| 158 |
$this->repr($v); |
| 159 |
} |
| 160 |
$this->raw(')'); |
| 161 |
} else { |
| 162 |
$this->string($value); |
| 163 |
} |
| 164 |
return $this; |
| 165 |
} |
| 166 |
/** |
| 167 |
* @return $this |
| 168 |
*/ |
| 169 |
public function addDebugInfo(Node $node) |
| 170 |
{ |
| 171 |
if ($node->getTemplateLine() != $this->lastLine) { |
| 172 |
$this->write(\sprintf("// line %d\n", $node->getTemplateLine())); |
| 173 |
$this->sourceLine += \substr_count($this->source, "\n", $this->sourceOffset); |
| 174 |
$this->sourceOffset = \strlen($this->source); |
| 175 |
$this->debugInfo[$this->sourceLine] = $node->getTemplateLine(); |
| 176 |
$this->lastLine = $node->getTemplateLine(); |
| 177 |
} |
| 178 |
return $this; |
| 179 |
} |
| 180 |
public function getDebugInfo() : array |
| 181 |
{ |
| 182 |
\ksort($this->debugInfo); |
| 183 |
return $this->debugInfo; |
| 184 |
} |
| 185 |
/** |
| 186 |
* @return $this |
| 187 |
*/ |
| 188 |
public function indent(int $step = 1) |
| 189 |
{ |
| 190 |
$this->indentation += $step; |
| 191 |
return $this; |
| 192 |
} |
| 193 |
/** |
| 194 |
* @return $this |
| 195 |
* |
| 196 |
* @throws \LogicException When trying to outdent too much so the indentation would become negative |
| 197 |
*/ |
| 198 |
public function outdent(int $step = 1) |
| 199 |
{ |
| 200 |
// can't outdent by more steps than the current indentation level |
| 201 |
if ($this->indentation < $step) { |
| 202 |
throw new \LogicException('Unable to call outdent() as the indentation would become negative.'); |
| 203 |
} |
| 204 |
$this->indentation -= $step; |
| 205 |
return $this; |
| 206 |
} |
| 207 |
public function getVarName() : string |
| 208 |
{ |
| 209 |
return \sprintf('__internal_compile_%d', $this->varNameSalt++); |
| 210 |
} |
| 211 |
private function checkForEcho(string $string) : void |
| 212 |
{ |
| 213 |
if ($this->didUseEcho) { |
| 214 |
return; |
| 215 |
} |
| 216 |
$this->didUseEcho = \preg_match('/^\\s*+(echo|print)\\b/', $string, $m) ? $m[1] : \false; |
| 217 |
} |
| 218 |
} |
| 219 |
|