/
ExtendBlade.php
143 lines (105 loc) · 4.24 KB
/
ExtendBlade.php
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
<?php
namespace Livewire\Mechanisms\ExtendBlade;
use Illuminate\Support\Facades\Blade;
use Livewire\Mechanisms\Mechanism;
use function Livewire\invade;
use function Livewire\on;
class ExtendBlade extends Mechanism
{
protected $directives = [];
protected $precompilers = [];
protected $renderCounter = 0;
protected static $livewireComponents = [];
function startLivewireRendering($component)
{
static::$livewireComponents[] = $component;
}
function endLivewireRendering()
{
array_pop(static::$livewireComponents);
}
static function currentRendering()
{
return end(static::$livewireComponents);
}
static function isRenderingLivewireComponent()
{
return ! empty(static::$livewireComponents);
}
function boot()
{
Blade::directive('this', fn() => "window.Livewire.find('{{ \$_instance->getId() }}')");
on('render', function ($target, $view) {
$this->startLivewireRendering($target);
$undo = $this->livewireifyBladeCompiler();
$this->renderCounter++;
return function ($html) use ($view, $undo, $target) {
$this->endLivewireRendering();
$this->renderCounter--;
if ($this->renderCounter === 0) {
$undo();
}
return $html;
};
});
// This is a custom view engine that gets used when rendering
// Livewire views. Things like letting certain exceptions bubble
// to the handler, and registering custom directives like: "@this".
app()->make('view.engine.resolver')->register('blade', function () {
return new ExtendedCompilerEngine(app('blade.compiler'));
});
app()->singleton(DeterministicBladeKeys::class);
// Reset this singleton between tests and Octane requests...
on('flush-state', function () {
app()->singleton(DeterministicBladeKeys::class);
});
// We're using "precompiler" as a hook for the point in time when
// Laravel compiles a Blade view...
app('blade.compiler')->precompiler(function ($value) {
app(DeterministicBladeKeys::class)->hookIntoCompile(app('blade.compiler'), $value);
return $value;
});
}
function livewireOnlyDirective($name, $handler)
{
$this->directives[$name] = $handler;
}
function livewireOnlyPrecompiler($handler)
{
$this->precompilers[] = $handler;
}
function livewireifyBladeCompiler() {
$removals = [];
if ($this->renderCounter === 0) {
$customDirectives = app('blade.compiler')->getCustomDirectives();
$precompilers = invade(app('blade.compiler'))->precompilers;
foreach ($this->directives as $name => $handler) {
if (! isset($customDirectives[$name])) {
$customDirectives[$name] = $handler;
invade(app('blade.compiler'))->customDirectives = $customDirectives;
$removals[] = function () use ($name) {
$customDirectives = app('blade.compiler')->getCustomDirectives();
unset($customDirectives[$name]);
invade(app('blade.compiler'))->customDirectives = $customDirectives;
};
}
}
foreach ($this->precompilers as $handler) {
if (array_search($handler, $precompilers) === false) {
array_unshift($precompilers, $handler);
invade(app('blade.compiler'))->precompilers = $precompilers;
$removals[] = function () use ($handler) {
$precompilers = invade(app('blade.compiler'))->precompilers;
$index = array_search($handler, $precompilers);
if ($index === false) return;
unset($precompilers[$index]);
invade(app('blade.compiler'))->precompilers = $precompilers;
};
}
}
}
return function () use ($removals) {
while ($removals) array_pop($removals)();
};
}
}