-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathWrapper.php
More file actions
149 lines (116 loc) · 5.41 KB
/
Copy pathWrapper.php
File metadata and controls
149 lines (116 loc) · 5.41 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
<?php
namespace Livewire\Blaze\Compiler;
use Livewire\Blaze\BladeService;
use Livewire\Blaze\BlazeManager;
use Livewire\Blaze\Compiler\DirectiveCompiler;
use Livewire\Blaze\Support\Utils;
use Illuminate\Support\Arr;
/**
* Compiles Blaze component templates into PHP function definitions.
*/
class Wrapper
{
protected PropsCompiler $propsCompiler;
protected AwareCompiler $awareCompiler;
protected UseExtractor $useExtractor;
public function __construct(
protected BladeService $blade,
protected BlazeManager $manager,
) {
$this->propsCompiler = new PropsCompiler;
$this->awareCompiler = new AwareCompiler;
$this->useExtractor = new UseExtractor;
}
/**
* Compile a component template into a function definition.
*
* @param string $compiled The compiled template (after TagCompiler processing)
* @param string $path The component file path
* @param string|null $source The original source template (for detecting $slot usage)
*/
public function wrap(string $compiled, string $path, ?string $source = null): string
{
$source ??= $compiled;
$name = ($this->manager->isFolding() ? '__' : '_') . Utils::hash($path);
$sourceUsesThis = str_contains($source, '$this') || str_contains($compiled, '@entangle') || str_contains($compiled, '@script') || str_contains($compiled, '@assets');
$compiled = $this->blade->compileUseStatements($compiled);
$compiled = $this->blade->restoreRawBlocks($compiled);
$compiled = $this->blade->storeVerbatimBlocks($compiled);
$imports = '';
$compiled = $this->useExtractor->extract($compiled, function ($statement) use (&$imports) {
$imports .= $statement . "\n";
});
$compiled = $this->blade->preStoreUncompiledBlocks($compiled);
$output = '';
$output .= '<'.'?php' . "\n";
$output .= $imports;
$output .= 'if (!function_exists(\''.$name.'\')):'."\n";
$output .= 'function '.$name.'($__blaze, $__data = [], $__slots = [], $__bound = [], $__keys = [], $__this = null) {'."\n";
if ($sourceUsesThis) {
$output .= '$__blazeFn = function () use ($__blaze, $__data, $__slots, $__bound, $__keys) {'."\n";
}
$output .= $this->globalVariables($source, $compiled);
$output .= 'if (($__data[\'attributes\'] ?? null) instanceof \Illuminate\View\ComponentAttributeBag) { $__data = $__data + $__data[\'attributes\']->all(); unset($__data[\'attributes\']); }'."\n";
$output .= 'extract($__slots, EXTR_SKIP); unset($__slots);'."\n";
$output .= 'extract($__data, EXTR_SKIP);'."\n";
$output .= 'extract($__env->getShared(), EXTR_SKIP);'."\n";
$output .= '$attributes = \\Livewire\\Blaze\\Runtime\\BlazeAttributeBag::make($__data, $__bound, $__keys);'."\n";
$output .= 'unset($__data, $__bound, $__keys);'."\n";
$output .= 'ob_start();' . "\n";
$output .= '?>' . "\n";
$compiled = DirectiveCompiler::make()
->directive('props', $this->propsCompiler->compile(...))
->directive('aware', $this->awareCompiler->compile(...))
->compile($compiled);
$compiled = $this->blade->restoreRawBlocks($compiled);
$output .= $compiled;
$output .= '<?php' . "\n";
$contentHandler = $this->manager->isFolding() ? '$__blaze->processPassthroughContent(\'ltrim\', ltrim(ob_get_clean()))' : 'ltrim(ob_get_clean())';
$output .= 'echo ' . $contentHandler . ';' . "\n";
if ($sourceUsesThis) {
$output .= '}; if ($__this !== null) { $__blazeFn->call($__this); } else { $__blazeFn(); }'."\n";
}
$output .= '} endif; ?>';
return $output;
}
protected function globalVariables(string $source, string $compiled): string
{
$output = '';
$output .= '$__env = $__blaze->env;' . "\n";
if ($this->hasEchoHandlers() && ($this->hasEchoSyntax($source) || $this->hasEchoSyntax($compiled))) {
$output .= '$__bladeCompiler = app(\'blade.compiler\');' . "\n";
}
$output .= implode("\n", array_filter(Arr::map([
[['$app'], '$app = $__blaze->app;'],
[['$errors', '@error'], '$errors = $__blaze->errors;'],
[['$__livewire', '@entangle', '@this'], '$__livewire = $__env->shared(\'__livewire\');'],
[['@this'], '$_instance = $__livewire;'],
[['$slot'], '$__slots[\'slot\'] ??= new \Illuminate\View\ComponentSlot(\'\');'],
], function ($data) use ($source, $compiled) {
[$patterns, $variable] = $data;
foreach ($patterns as $pattern) {
if (str_contains($source, $pattern) || str_contains($compiled, $pattern)) {
return $variable;
}
}
return null;
}))) . "\n";
return $output;
}
/**
* Check if the Blade compiler has any echo handlers registered.
*/
protected function hasEchoHandlers(): bool
{
$compiler = $this->blade->compiler;
$reflection = new \ReflectionProperty($compiler, 'echoHandlers');
return ! empty($reflection->getValue($compiler));
}
/**
* Check if the source contains Blade echo syntax.
*/
protected function hasEchoSyntax(string $source): bool
{
return preg_match('/\{\{.+?\}\}|\{!!.+?!!\}/s', $source) === 1;
}
}