-
-
Notifications
You must be signed in to change notification settings - Fork 632
Expand file tree
/
Copy pathStatamicTagCompiler.php
More file actions
171 lines (137 loc) · 5.52 KB
/
Copy pathStatamicTagCompiler.php
File metadata and controls
171 lines (137 loc) · 5.52 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
<?php
namespace Statamic\View\Blade;
use Illuminate\Support\Str;
use Statamic\View\Antlers\Language\Analyzers\TagIdentifierAnalyzer;
use Statamic\View\Blade\Concerns\CompilesComponents;
use Statamic\View\Blade\Concerns\CompilesNavs;
use Statamic\View\Blade\Concerns\CompilesNocache;
use Statamic\View\Blade\Concerns\CompilesPartials;
use Stillat\BladeParser\Compiler\CompilerServices\AttributeCompiler;
use Stillat\BladeParser\Nodes\Components\ComponentNode;
use Stillat\BladeParser\Parser\DocumentParser;
class StatamicTagCompiler
{
use CompilesComponents,
CompilesNavs,
CompilesNocache,
CompilesPartials;
protected AttributeCompiler $attributeCompiler;
protected array $statamicTags = ['statamic', 's'];
protected string $prependCompiledContent = '';
protected string $appendCompiledContent = '';
protected bool $interceptNav = true;
public function __construct()
{
$this->attributeCompiler = (new AttributeCompiler())
->prefixEscapedParametersWith('attr:')
->wrapResultIn(['as', 'scope'], function ($value) {
return "\\Statamic\\View\\Blade\\StatamicTagCompiler::adjustDynamicVariableName($value)";
});
}
protected function getComponentContent(ComponentNode $node): string
{
if ($node->isClosedBy === null || $node->isSelfClosing) {
return $node->content;
}
return $node->outerDocumentContent;
}
public static function adjustDynamicVariableName(string $variableName): string
{
return ltrim($variableName, '$');
}
protected function compileParameters(array $params): string
{
return '\Statamic\View\Blade\BladeTagHost::filterParams('.$this->attributeCompiler->compile($params).')';
}
public function prependCompiledContent(string $content): static
{
$this->prependCompiledContent = $content;
return $this;
}
public function appendCompiledContent(string $content): static
{
$this->appendCompiledContent = $content;
return $this;
}
public function setInterceptNav(bool $interceptNav): static
{
$this->interceptNav = $interceptNav;
return $this;
}
public function compile(string $template): string
{
if (! Str::contains($template, ['<statamic:', '<statamic-', '<s:', '<s-'])) {
return $template;
}
return (new DocumentParser())
->registerCustomComponentTags($this->statamicTags)
->onlyParseComponents()
->parseTemplate($template)
->toDocument()
->getRootNodes()
->map(function ($node) {
if (! $node instanceof ComponentNode) {
return $node->unescapedContent;
}
if (! in_array(mb_strtolower($node->componentPrefix), $this->statamicTags)) {
return $node->outerDocumentContent;
}
if ($node->isClosingTag && ! $node->isSelfClosing) {
return '';
}
if ($node->tagName === 'nocache') {
return $this->compileNocache($node);
} elseif ($this->isPartial($node)) {
return $this->compilePartial($node);
} elseif ($this->interceptNav && $this->isStructure($node->tagName)) {
return $this->compileNav($node);
}
return $this->compileComponent($node);
})->join('');
}
protected function isStructure(string $tagName): bool
{
$tagName = (string) str($tagName)->before(':')->lower();
return in_array($tagName, ['nav', 'structure', 'children']);
}
protected function isPartial(ComponentNode $component): bool
{
return $component->tagName == 'partial' || str($component->tagName)->lower()->startsWith('partial:');
}
protected function extractMethodNames(ComponentNode $component): array
{
[$name, $methodPart] = TagIdentifierAnalyzer::splitNameAndMethodPart($component->tagName);
$originalMethod = $methodPart ?: 'index';
$method = Str::camel($originalMethod);
return [$name, $method, $originalMethod];
}
protected function isPairedComponent(ComponentNode $component): bool
{
return $component->isClosedBy != null && ! $component->isSelfClosing;
}
protected function compileTemplate(ComponentNode $component, string $template, string $nestedContent, ?array $params = null, array $additional = []): string
{
if ($params === null) {
$params = $component->parameters;
}
[$name, $method, $originalMethod] = $this->extractMethodNames($component);
$isPair = 'false';
if ($this->isPairedComponent($component)) {
$isPair = 'true';
}
return (string) str($template)
->swap(array_merge([
'$tagName' => $name,
'$fullTagName' => $component->tagName,
'$tagMethod' => "'".$method."'",
'$originalMethod' => "'".$originalMethod."'",
'$params' => $this->compileParameters($params),
'$isPair' => $isPair,
'#compiled#' => $nestedContent,
'#compiledEncoded#' => base64_encode($nestedContent),
'VarSuffix' => Str::random(32),
'#prepend#' => $this->prependCompiledContent,
'#append#' => $this->appendCompiledContent,
], $additional));
}
}