43 if (\preg_match_all($this->varPattern, $text, $matches)) {
44 $originals = $matches[0];
46 foreach ($vars as $index => $varStr) {
47 $variableInfos = $this->parseVar($varStr);
48 $result = \str_replace($originals[$index], $this->generator->insertVariable($variableInfos[
'name'], $variableInfos[
'safe']), $result);
56 if (\preg_match_all(
'@\{\{\s?(.*?)==(.*?)\s(.*?)\s?}}@', $text, $matches)) {
57 $originals = $matches[0];
58 $varsLeft = $matches[1];
59 $varsRight = $matches[2];
60 foreach ($originals as $index => $original) {
61 $tmp = \str_replace($varsLeft[$index], $this->generator->asVariable($varsLeft[$index]), $original);
62 $tmp = \str_replace($varsRight[$index], $this->generator->asVariable($varsRight[$index]), $tmp);
63 $result = \str_replace($original, $tmp, $result);
71 if (\preg_match_all($this->getExpressionPattern(
'if'), $text, $matches)) {
72 $originals = $matches[0];
73 $conditions = $matches[1];
74 foreach ($conditions as $index => $cond) {
75 $result = \str_replace($originals[$index], $this->generator->condition($cond), $result);
83 if (\preg_match_all($this->getExpressionPattern(
'for'), $text, $matches)) {
84 $originals = $matches[0];
85 $foreachs = $matches[1];
86 foreach ($foreachs as $index => $foreach) {
87 $foreachInfos = $this->parseForeach($foreach);
88 $result = \str_replace($originals[$index], $this->generator->foreach($foreachInfos[
'array'], $foreachInfos[
'value'], $foreachInfos[
'key']), $result);
96 if (\preg_match_all($this->blockPattern, $text, $matches)) {
97 $originals = $matches[0];
98 $blocks = $matches[1];
99 foreach ($blocks as $index => $block) {
100 $blockName = \trim($block);
101 $result = \str_replace($originals[$index], $this->generator->openBlock($blockName), $result);
107 protected function parseCallback(
string $text,
string $pattern, $callback): string {
109 if (\preg_match_all($pattern, $text, $matches)) {
110 $originals = $matches[0];
111 foreach ($originals as $elm) {
112 $result = \str_replace($elm, $callback(), $result);
120 if (\preg_match_all($pattern, $text, $matches)) {
121 $originals = $matches[0];
123 foreach ($vars as $index => $elm) {
125 $result = \str_replace($originals[$index], $callback($elm), $result);
132 $result = $this->parseCallback($fileContent,
'@\{\{\s?nonce\s?}}@', function () {
133 return $this->generator->getNonce();
135 $result = $this->parseCallback($result,
"@\{\s?nonce:\s?nonce\s?}@",
function () {
136 return $this->generator->getNonceArray();
138 $result = $this->parseCallback($result,
'@\{\{\s?_self\s?}}@',
function () {
139 return $this->generator->getSelf();
141 $result = $this->parseAllVars($result);
142 $result = $this->parseAllBlock($result);
143 $result = $this->parseEquality($result);
144 $result = $this->parseCallback($result,
'@\{%\s?endblock\s?%}@',
function () {
145 return $this->generator->closeBlock();
147 $result = $this->parseAllForeachs($result);
148 $result = $this->parseCallback($result,
'@\{%\s?endfor\s?%}@',
function () {
149 return $this->generator->endForeach();
151 $result = $this->parseAllConditions($result);
152 $result = $this->parseCallback($result,
'@\{%\s?endif\s?%}@',
function () {
153 return $this->generator->endCondition();
155 $result = $this->parseCallbackWithVar($result,
'@\{%\s?extends\s?(.*?)\s?%}@',
function ($name) {
156 $asVariable = !UString::containsValues([
'"',
"'"], $name);
157 return $this->generator->extendsTemplate($name, $asVariable);
159 $result = $this->parseCallbackWithVar($result,
'@\{%\s?include\s?(.*?)\s?%}@',
function ($name) {
160 $asVariable = !UString::containsValues([
'"',
"'"], $name);
161 return $this->generator->includeFile($name, $asVariable);
163 $result = $this->parseCallbackWithVar($result,
'@\{\{\s(.*?)\s?}}@',
function ($content) {
164 return $this->generator->getOpenVarTag() . $content . $this->generator->getCloseVarTag();
166 $result = $this->parseCallbackWithVar($result,
'@\{%\s(.*?)\s?%}@',
function ($content) {
167 return $this->generator->getOpenExpressionTag() . $content . $this->generator->getCloseExpressionTag();