forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeParser.php
More file actions
930 lines (751 loc) · 29.2 KB
/
RuntimeParser.php
File metadata and controls
930 lines (751 loc) · 29.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
<?php
namespace Statamic\View\Antlers\Language\Runtime;
use Illuminate\Http\Exceptions\HttpResponseException;
use ReflectionProperty;
use Spatie\ErrorSolutions\Contracts\ProvidesSolution;
use Spatie\LaravelIgnition\Exceptions\ViewException;
use Spatie\LaravelIgnition\Exceptions\ViewExceptionWithSolution;
use Statamic\Contracts\View\Antlers\Parser;
use Statamic\Fields\Value;
use Statamic\Modifiers\ModifierNotFoundException;
use Statamic\Search\Comb\Exceptions\Exception;
use Statamic\Support\Str;
use Statamic\Tags\TagNotFoundException;
use Statamic\View\Antlers\AntlersString;
use Statamic\View\Antlers\Language\Errors\AntlersErrorCodes;
use Statamic\View\Antlers\Language\Errors\LineRetriever;
use Statamic\View\Antlers\Language\Exceptions\AntlersException;
use Statamic\View\Antlers\Language\Exceptions\RuntimeException;
use Statamic\View\Antlers\Language\Exceptions\SyntaxErrorException;
use Statamic\View\Antlers\Language\Lexer\AntlersLexer;
use Statamic\View\Antlers\Language\Nodes\AbstractNode;
use Statamic\View\Antlers\Language\Nodes\AntlersNode;
use Statamic\View\Antlers\Language\Nodes\Position;
use Statamic\View\Antlers\Language\Parser\ComponentCompiler;
use Statamic\View\Antlers\Language\Parser\DocumentParser;
use Statamic\View\Antlers\Language\Parser\LanguageKeywords;
use Statamic\View\Antlers\Language\Parser\LanguageParser;
use Statamic\View\Antlers\Language\Parser\PathParser;
use Statamic\View\Antlers\Language\Runtime\Debugging\GlobalDebugManager;
use Statamic\View\Antlers\Language\Utilities\StringUtilities;
use Statamic\View\Cascade;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;
class RuntimeParser implements Parser
{
/**
* The current DocumentParser instance.
*
* @var DocumentParser
*/
private $documentParser = null;
/**
* The current NodeProcessor instance.
*
* @var NodeProcessor
*/
private $nodeProcessor = null;
/**
* The current view template file path, if available.
*
* @var string
*/
private $view = '';
/**
* @var Cascade|null
*/
private $cascade = null;
/**
* Indicates if PHP code execution is allowed.
*
* @var bool
*/
private $allowPhp = false;
/**
* A list of pre-parsers.
*
* @var callable[]
*/
protected $preParsers = [];
/**
* A cache of previously observed render nodes.
*
* @var array
*/
protected static $standardRenderNodeCache = [];
/**
* A reference to any temporary file created during error reporting.
*
* @var string|null
*/
private $tempFileCreated = null;
/**
* The Antlers Lexer instance.
*
* @var AntlersLexer
*/
private $antlersLexer = null;
/**
* The Antlers Parser instance.
*
* @var LanguageParser
*/
private $antlersParser = null;
/**
* Keeps track of how nested the RuntimeParser instance is.
*
* @var int
*/
private $parseStack = 0;
/**
* Determines if runtime processors should be isolated.
*
* @var bool
*/
private $isolateRuntimes = false;
protected $componentCompiler;
public function __construct(DocumentParser $documentParser, NodeProcessor $nodeProcessor, AntlersLexer $lexer, LanguageParser $antlersParser)
{
$this->documentParser = $documentParser;
$this->nodeProcessor = $nodeProcessor;
$this->antlersLexer = $lexer;
$this->antlersParser = $antlersParser;
$this->componentCompiler = new ComponentCompiler();
}
/**
* Sets the RuntimeConfiguration instance.
*
* @param RuntimeConfiguration $configuration The RuntimeConfiguration instance.
* @return $this
*/
public function setRuntimeConfiguration(RuntimeConfiguration $configuration)
{
GlobalRuntimeState::$allowPhpInContent = $configuration->allowPhpInUserContent;
GlobalRuntimeState::$allowMethodsInContent = $configuration->allowMethodsInUserContent;
GlobalRuntimeState::$throwErrorOnAccessViolation = $configuration->throwErrorOnAccessViolation;
GlobalRuntimeState::$bannedVarPaths = $configuration->guardedVariablePatterns;
GlobalRuntimeState::$bannedContentVarPaths = $configuration->guardedContentVariablePatterns;
GlobalRuntimeState::$bannedTagPaths = $configuration->guardedTagPatterns;
GlobalRuntimeState::$bannedContentTagPaths = $configuration->guardedContentTagPatterns;
GlobalRuntimeState::$allowedContentTagPaths = $configuration->allowedContentTagPatterns;
GlobalRuntimeState::$bannedModifierPaths = $configuration->guardedModifiers;
GlobalRuntimeState::$bannedContentModifierPaths = $configuration->guardedContentModifiers;
GlobalRuntimeState::$allowedContentModifierPaths = $configuration->allowedContentModifiers;
$this->nodeProcessor->setRuntimeConfiguration($configuration);
foreach ($configuration->getPreparsers() as $preparser) {
$this->preparse($preparser);
}
foreach ($configuration->getVisitors() as $visitor) {
$this->documentParser->addVisitor($visitor);
}
return $this;
}
/**
* Resets the internal runtime configuration instance.
*
* @return $this
*/
public function resetRuntimeConfiguration()
{
$this->nodeProcessor->resetRuntimeConfiguration();
return $this;
}
/**
* Escapes the PHP starting tags from the input text.
*
* @param string $text The text to sanitize.
* @return string|string[]
*/
protected function sanitizePhp($text)
{
if (GlobalRuntimeState::$isEvaluatingUserData && ! GlobalRuntimeState::$allowPhpInContent) {
return StringUtilities::sanitizePhp($text);
}
if ($this->allowPhp) {
return $text;
}
return StringUtilities::sanitizePhp($text);
}
/**
* Executes all pre-parser callbacks on the input text.
*
* @param string $text The text to pre-parse.
* @return string
*/
protected function runPreParserCallbacks($text)
{
$value = $text;
foreach ($this->preParsers as $preParser) {
$value = call_user_func($preParser, $value);
}
return $value;
}
/**
* Analyzes a file's contents to determine a front matter line offset.
*
* The line offset is utilized to correctly determine
* the line nodes starting line for error reporting.
*
* @param string $path The file path.
* @return int
*/
private function getFrontMatterLineOffset($path)
{
if (file_exists($path) == false) {
return 1;
}
$contents = file_get_contents($path);
if (preg_match('/^---[\r\n?|\n]/', $contents)) {
$linedContent = StringUtilities::breakByNewLine(StringUtilities::normalizeLineEndings($contents));
$lineCount = count($linedContent);
if ($lineCount <= 1) {
unset($lineCount);
unset($contents);
return 1;
}
for ($i = 1; $i < $lineCount; $i++) {
if (Str::startsWith($linedContent[$i], '---')) {
unset($lineCount);
unset($contents);
return $i + 2;
}
}
}
unset($contents);
return 1;
}
/**
* Adds a string preparser to the list of executable preparsers.
*
* @param callable $preparser The preparser to add.
*/
public function preparse(callable $preparser)
{
$this->preParsers[] = $preparser;
}
protected function canPossiblyParseAntlers($text)
{
if ($text == null || mb_strlen(trim($text)) == 0) {
return false;
}
// Check if there may be PHP tags in the document. If
// PHP is enabled, we will let the NodeProcessor
// handle it so that we do not have to copy
// any of its PHP-specific logic here.
if ($this->allowPhp && Str::contains($text, '<?')) {
return true;
}
if (Str::contains($text, [DocumentParser::LeftBrace, '@props', '@aware', '@cascade'])) {
return true;
}
return false;
}
/**
* Adds a list of nodes to the internal node cache.
*
* @param string $text The source content.
* @param AbstractNode[] $nodes The parsed nodes.
*/
public static function pushNodeCache($text, $nodes)
{
self::$standardRenderNodeCache[md5($text)] = $nodes;
}
/**
* Returns all node cache entries.
*
* @return array
*/
public static function getNodeCache()
{
return self::$standardRenderNodeCache;
}
/**
* Restores a previous cache entry.
*
* @param string $entry The cache slug.
* @param AbstractNode[] $nodes The parsed nodes.
*/
public static function setCacheEntry($entry, $nodes)
{
self::$standardRenderNodeCache[$entry] = $nodes;
}
protected function isIgnitionInstalled()
{
return class_exists(ViewException::class) || class_exists('Spatie\LaravelIgnition\Exceptions\ViewException');
}
protected function shouldCacheRenderNodes($text)
{
return ! str_contains($text, '/noparse');
}
/**
* Parses and renders the input text, with the provided runtime data.
*
* @param string $text The text to parse and render.
* @param array $data The runtime data.
* @return AntlersString
*
* @throws AntlersException
* @throws ViewException
* @throws TagNotFoundException
* @throws Throwable
*/
protected function renderText($text, $data = [])
{
$text = $this->componentCompiler->compile($text);
$this->parseStack += 1;
$text = $this->runPreParserCallbacks($text);
if (! $this->canPossiblyParseAntlers($text)) {
$text = $this->sanitizePhp($text);
$this->parseStack -= 1;
if ($text == null) {
return new AntlersString('', $this);
}
return new AntlersString($text, $this);
}
$newLineStyle = StringUtilities::detectNewLineStyle($text);
$bufferContent = '';
try {
$parseText = $this->sanitizePhp($text);
$cacheSlug = md5($parseText);
if (! array_key_exists($cacheSlug, self::$standardRenderNodeCache) || ! $this->shouldCacheRenderNodes($text)) {
$this->documentParser->setIsVirtual($this->view == '');
if (strlen($this->view) > 0) {
$seedStartLine = $this->getFrontMatterLineOffset($this->view);
$this->documentParser->setStartLineSeed($seedStartLine);
} else {
$this->documentParser->setStartLineSeed(1);
}
if (! empty(GlobalRuntimeState::$globalTagEnterStack)) {
/** @var AntlersNode $lastTagNode */
$lastTagNode = GlobalRuntimeState::$globalTagEnterStack[count(GlobalRuntimeState::$globalTagEnterStack) - 1];
if ($lastTagNode->name->name != 'partial') {
$this->documentParser->setStartLineSeed($lastTagNode->endPosition->line);
}
}
self::$standardRenderNodeCache[$cacheSlug] = $this->documentParser->parse($parseText);
}
$renderNodes = self::$standardRenderNodeCache[$cacheSlug];
$this->nodeProcessor->setData($data);
$this->nodeProcessor->setAntlersParserInstance($this);
$this->nodeProcessor->cascade($this->cascade);
$this->nodeProcessor->mergeRuntimeAssignments(GlobalRuntimeState::$tracedRuntimeAssignments);
$bufferContent = $this->nodeProcessor->render($renderNodes);
$this->nodeProcessor->triggerRenderComplete();
} catch (AntlersException $antlersException) {
if ($this->isIgnitionInstalled()) {
throw $this->buildAntlersExceptionError($antlersException, $text, $data);
}
throw $antlersException;
} catch (ModifierNotFoundException $exception) {
if (GlobalRuntimeState::$lastNode != null && GlobalDebugManager::isDebugSessionActive()) {
$wrapper = new AntlersException($exception->getMessage());
$wrapper->node = GlobalRuntimeState::$lastNode;
$wrapper->type = AntlersErrorCodes::TYPE_MODIFIER_NOT_FOUND;
GlobalDebugManager::writeException($wrapper);
}
throw $this->addAntlersErrorDetails($exception, $text, $data);
} catch (Exception $exception) {
if (GlobalRuntimeState::$lastNode != null && GlobalDebugManager::isDebugSessionActive()) {
$wrapper = new AntlersException($exception->getMessage());
$wrapper->node = GlobalRuntimeState::$lastNode;
$wrapper->type = AntlersErrorCodes::TYPE_RUNTIME_GENERAL_FAULT;
GlobalDebugManager::writeException($wrapper);
}
throw $this->addAntlersErrorDetails($exception, $text, $data);
} catch (Throwable $throwable) {
if (GlobalRuntimeState::$lastNode != null && GlobalDebugManager::isDebugSessionActive()) {
$wrapper = new AntlersException($throwable->getMessage());
$wrapper->node = GlobalRuntimeState::$lastNode;
$wrapper->type = AntlersErrorCodes::TYPE_RUNTIME_GENERAL_FAULT;
GlobalDebugManager::writeException($wrapper);
}
throw $this->addAntlersErrorDetails($throwable, $text, $data);
}
if ($newLineStyle != "\n") {
if (Str::contains($bufferContent, "\r\n") == false) {
$bufferContent = str_replace("\n", "\r\n", $bufferContent);
}
}
$this->parseStack -= 1;
if ($this->parseStack == 0 && GlobalRuntimeState::$containsLayout == false) {
$bufferContent = LiteralReplacementManager::processReplacements($bufferContent);
$bufferContent = StackReplacementManager::processReplacements($bufferContent);
$bufferContent = str_replace(DocumentParser::getLeftBraceEscape(), DocumentParser::LeftBrace, $bufferContent);
$bufferContent = str_replace(DocumentParser::getRightBraceEscape(), DocumentParser::RightBrace, $bufferContent);
}
if (GlobalRuntimeState::$containsLayout && $this->view == GlobalRuntimeState::$shareVariablesTemplateTrigger) {
// Force the root runtime assignments to be merged into the global state.
GlobalRuntimeState::$layoutVariables = $this->nodeProcessor->getRuntimeAssignments();
}
return new AntlersString($bufferContent, $this);
}
private function cleanUpTempFiles()
{
// Automatically clean up the temporary file.
// If we wrap this in app()->terminating,
// the file exists long enough for the
// Ignition renderer to display it.
if ($this->tempFileCreated != null) {
$tmpFile = $this->tempFileCreated;
app()->terminating(function () use ($tmpFile) {
@unlink($tmpFile);
});
$this->tempFileCreated = null;
}
}
private function addAntlersErrorDetails($exception, $text, $data)
{
// This is important to not completely bomb out the tests.
if (! class_exists(ViewException::class)) {
return $exception;
}
if ($exception instanceof ViewException || $exception instanceof ViewExceptionWithSolution) {
return $exception;
}
if ($exception instanceof HttpException || $exception instanceof HttpResponseException) {
return $exception;
}
$exceptionClass = ViewException::class;
if (in_array(ProvidesSolution::class, class_implements($exception))) {
$exceptionClass = ViewExceptionWithSolution::class;
}
$line = null;
if (GlobalRuntimeState::$lastNode != null) {
$line = GlobalRuntimeState::$lastNode->startPosition->line;
}
$newException = new $exceptionClass($exception->getMessage(), 0, 1, $this->view, $line, $exception);
if ($exceptionClass === ViewExceptionWithSolution::class) {
$newException->setSolution($exception->getSolution());
}
$rebuiltTrace = $this->buildStackTrace(GlobalRuntimeState::$lastNode, $text);
$rebuiltTrace = array_merge($rebuiltTrace, $exception->getTrace());
$traceProperty = new ReflectionProperty('Exception', 'trace');
$traceProperty->setValue($newException, $rebuiltTrace);
$this->cleanUpTempFiles();
return $newException;
}
private function buildAntlersExceptionError(AntlersException $antlersException, $text, $data)
{
if ($antlersException->node == null && GlobalRuntimeState::$lastNode != null) {
$antlersException->node = GlobalRuntimeState::$lastNode;
} elseif ($antlersException->node == null && GlobalRuntimeState::$lastNode == null) {
throw $antlersException;
}
$rebuiltTrace = $this->buildStackTrace($antlersException->node, $text);
// Build up a new Ignition exception.
$typeText = '';
if ($antlersException->type != '') {
$typeText = '['.$antlersException->type.']: ';
}
$newMessage = $typeText.$antlersException->getMessage().' '.LineRetriever::getErrorLineAndCharText($antlersException->node);
$exceptionLine = 1;
if ($antlersException->node->startPosition != null) {
$exceptionLine = $antlersException->node->startPosition->line;
}
$exceptionClass = ViewException::class;
if (class_exists('Spatie\LaravelIgnition\Exceptions\ViewException')) {
$exceptionClass = 'Spatie\LaravelIgnition\Exceptions\ViewException';
}
$exceptionView = $this->view;
if (GlobalRuntimeState::$userContentEvalState != null && ! empty(GlobalRuntimeState::$userContentEvalState)) {
$dynamicExceptionLine = $exceptionLine;
if (GlobalRuntimeState::$userContentEvalState[1] instanceof AbstractNode) {
/** @var AbstractNode $node */
$node = GlobalRuntimeState::$userContentEvalState[1];
if ($node->startPosition != null) {
if (count($rebuiltTrace) >= 1) {
$rebuiltTrace[0]['line'] = $node->startPosition->line;
}
}
}
if (GlobalRuntimeState::$userContentEvalState[0] instanceof Value) {
/** @var Value $value */
$value = GlobalRuntimeState::$userContentEvalState[0];
$valueContent = (string) $value;
$handle = $value->handle();
$newMessage .= ' Exception thrown while parsing an Antlers enabled field ['.$handle.']';
$valueAugmentable = $value->augmentable();
if ($valueAugmentable != null) {
if (method_exists($valueAugmentable, 'path')) {
$path = $valueAugmentable->path();
$additionalInfo = <<<'INFO'
{{#
{name} field defined in
{path}
#}}
INFO;
$additionalInfo = str_replace('{name}', $handle, $additionalInfo);
$additionalInfo = str_replace('{path}', $path, $additionalInfo);
$valueContent .= $additionalInfo;
}
}
$this->makeTempAntlersFile($valueContent);
$exceptionView = $this->tempFileCreated;
$rebuiltTrace[] = [
'file' => $this->tempFileCreated,
'line' => $dynamicExceptionLine,
];
}
}
$ignitionException = new $exceptionClass($newMessage, 0, 1, $exceptionView, $exceptionLine, $antlersException);
$traceProperty = new ReflectionProperty('Exception', 'trace');
$traceProperty->setValue($ignitionException, $rebuiltTrace);
$ignitionException->setViewData($data);
$this->cleanUpTempFiles();
if (GlobalDebugManager::isDebugSessionActive()) {
GlobalDebugManager::writeException($antlersException);
}
return $ignitionException;
}
private function makeTempAntlersFile($documentText)
{
$extension = 'html';
if ($this->allowPhp) {
$extension = 'php';
}
$antlersDirectory = storage_path('antlers/');
if (! file_exists($antlersDirectory)) {
@mkdir($antlersDirectory, 0755);
}
$this->tempFileCreated = storage_path('antlers/'.sha1($documentText).'.antlers.'.$extension);
file_put_contents($this->tempFileCreated, $documentText);
}
private function buildStackTrace(AbstractNode $activeNode, $documentText)
{
if (count(GlobalRuntimeState::$templateFileStack) == 0) {
$this->makeTempAntlersFile($documentText);
$debugTrace = debug_backtrace();
// The first item in the trace should be our current buildStackTrace call.
// The second item should be the call to RuntimeParser renderText method.
// The third item should be the user-land item that invoked the parse.
$userLandCode = $debugTrace[2];
$currentStack = [
[$this->tempFileCreated, $activeNode],
[$userLandCode['file'], $userLandCode['line']],
];
} else {
$currentStack = array_reverse(GlobalRuntimeState::$templateFileStack);
}
$currentStack[0][1] = $activeNode;
$stackTrace = [];
foreach ($currentStack as $stackItem) {
$stackNode = $stackItem[1];
$stackFile = $stackItem[0];
if (is_object($stackNode)) {
$stackLine = LineRetriever::getErrorLine($stackNode);
} elseif (is_numeric($stackNode)) {
$stackLine = $stackNode;
} else {
$stackLine = 1;
}
$stackTrace[] = [
'file' => $stackFile,
'line' => $stackLine,
];
}
return $stackTrace;
}
public function isolateRuntimes($isolate)
{
$this->isolateRuntimes = $isolate;
return $this;
}
private function cloneRuntimeParser()
{
$parser = (new RuntimeParser(
$this->documentParser,
$this->nodeProcessor->cloneProcessor(),
$this->antlersLexer, $this->antlersParser
))->allowPhp($this->allowPhp);
foreach ($this->preParsers as $preParser) {
$parser->preparse($preParser);
}
if ($this->cascade != null) {
$parser->cascade($this->cascade);
}
return $parser;
}
private function shouldIsolate()
{
if ($this->isolateRuntimes) {
return true;
}
if (GlobalRuntimeState::$isEvaluatingUserData) {
return true;
}
if (GlobalRuntimeState::$requiresRuntimeIsolation) {
return true;
}
return false;
}
public function parse($text, $data = [])
{
if ($this->shouldIsolate()) {
return $this->cloneRuntimeParser()->renderText($text, $data);
}
return $this->renderText($text, $data);
}
public function render($text, $data = [])
{
return $this->renderText($text, $data);
}
public function valueWithNoparse($text)
{
// Just pass-thru.
return $text;
}
public function extractNoparse($text)
{
// Just pass-thru.
return $text;
}
public function allowPhp($allow = true)
{
$this->allowPhp = $allow;
$this->nodeProcessor->allowPhp($this->allowPhp);
return $this;
}
public function cascade($cascade)
{
$this->cascade = $cascade;
return $this;
}
public function parseView($view, $text, $data = [])
{
$previousIsEvaluatingUserData = GlobalRuntimeState::$isEvaluatingUserData;
GlobalRuntimeState::$isEvaluatingUserData = false;
$existingView = $this->view;
try {
return $this->renderViewContent($view, $text, $data);
} finally {
$this->view = $existingView;
array_pop(GlobalRuntimeState::$templateFileStack);
GlobalRuntimeState::$currentExecutionFile = $this->view;
GlobalRuntimeState::$isEvaluatingUserData = $previousIsEvaluatingUserData;
}
}
private function renderViewContent($view, $text, $data = [])
{
$this->view = $view;
GlobalRuntimeState::$templateFileStack[] = [$view, null];
if (count(GlobalRuntimeState::$templateFileStack) > 1) {
GlobalRuntimeState::$templateFileStack[count(GlobalRuntimeState::$templateFileStack) - 2][1] = GlobalRuntimeState::$lastNode;
}
GlobalRuntimeState::$currentExecutionFile = $this->view;
if (GlobalDebugManager::$isConnected) {
GlobalDebugManager::registerPathLocator($this->view);
}
$data = array_merge($data, [
'view' => $this->cascade->getViewData($view),
]);
return $this->renderText($text, $data);
}
public function injectNoparse($text)
{
return $text;
}
/**
* Constructs a "virtual" node from the provided text.
*
* @param string $text The node content.
* @return AntlersNode
*/
private function wrapText($text)
{
$node = new AntlersNode();
$mockPosition = new Position();
$mockPosition->offset = 0;
$mockPosition->line = 0;
$mockPosition->char = 0;
$node->startPosition = $mockPosition;
$node->endPosition = $mockPosition;
$node->content = $text;
return $node;
}
/**
* Tests if the full Antlers Lexer & Parser should be used to parse the provided text.
*
* @param string $text The input to test.
* @return bool
*/
private function shouldUseFullParserForVariable($text)
{
return Str::contains($text, [
DocumentParser::Punctuation_Pipe,
DocumentParser::LeftBracket,
DocumentParser::RightBracket,
DocumentParser::LeftBrace,
DocumentParser::RightBrace,
DocumentParser::LeftParen,
DocumentParser::RightParent,
DocumentParser::Punctuation_Exclamation,
DocumentParser::Punctuation_Question,
DocumentParser::Punctuation_Ampersand,
DocumentParser::Punctuation_Equals,
LanguageKeywords::LogicalXor,
LanguageKeywords::LogicalOr,
LanguageKeywords::LogicalNot,
LanguageKeywords::LogicalAnd,
]);
}
/**
* Takes a scope-notated key and finds the value for it in the given
* array or object.
*
* @param string $key Dot-notated key to find
* @param mixed $default Default value to use if not found
* @return mixed
*
* @throws RuntimeException
* @throws SyntaxErrorException
*/
public function getVariable($key, $context, $default = null)
{
// If the incoming key has the | character, we will assume that
// it contains modifiers. To handle this case, we will hand
// the incoming string off to the Antlers lexer, parser
// and finally, through the runtime processor & env.
if ($this->shouldUseFullParserForVariable($key)) {
// 1: Create a wrapper node to contain the text.
$wrappedNode = $this->wrapText($key);
$tokens = $this->antlersLexer->tokenize($wrappedNode, $key);
$antlersNodes = $this->antlersParser->parse($tokens);
// SG LG
$variableNode = $antlersNodes[0]->nodes[0];
// We don't want to share the previous processor instance and
// accidentally mess up it's internal state while handling
// any variables that are being resolved for parameters.
/** @var NodeProcessor $processor */
$processor = app(NodeProcessor::class);
$processor->cascade($this->cascade)->setData($context);
// Evaluate the variable, and return the results.
return $processor->evaluateDeferredVariable($variableNode);
}
$pathParser = new PathParser();
$path = $pathParser->parse($key);
$pathDataManager = new PathDataManager();
$pathDataManager->setIsPaired(false);
$pathDataManager->cascade($this->cascade);
$results = $pathDataManager->getDataWithExistence($path, $context);
if ($results[0] === true) {
return $results[1];
}
return $default;
}
/**
* Sets a render callback.
*
* @return Parser
*/
public function callback($callback)
{
return $this;
}
/**
* Clears the standard render node cache.
*/
public static function clearRenderNodeCache()
{
self::$standardRenderNodeCache = [];
}
}