/
SupportPageComponents.php
247 lines (192 loc) · 8.46 KB
/
SupportPageComponents.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
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
<?php
namespace Livewire\Features\SupportPageComponents;
use function Livewire\{invade, on, off, once};
use Livewire\Drawer\ImplicitRouteBinding;
use Livewire\ComponentHook;
use Illuminate\View\View;
use Illuminate\View\AnonymousComponent;
use Illuminate\Support\Facades\Blade;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class SupportPageComponents extends ComponentHook
{
static function provide()
{
static::registerLayoutViewMacros();
static::resolvePageComponentRouteBindings();
}
static function registerLayoutViewMacros()
{
View::macro('layoutData', function ($data = []) {
if (! isset($this->layoutConfig)) $this->layoutConfig = new PageComponentConfig;
$this->layoutConfig->mergeParams($data);
return $this;
});
View::macro('section', function ($section) {
if (! isset($this->layoutConfig)) $this->layoutConfig = new PageComponentConfig;
$this->layoutConfig->slotOrSection = $section;
return $this;
});
View::macro('title', function ($title) {
if (! isset($this->layoutConfig)) $this->layoutConfig = new PageComponentConfig;
$this->layoutConfig->mergeParams(['title' => $title]);
return $this;
});
View::macro('slot', function ($slot) {
if (! isset($this->layoutConfig)) $this->layoutConfig = new PageComponentConfig;
$this->layoutConfig->slotOrSection = $slot;
return $this;
});
View::macro('extends', function ($view, $params = []) {
if (! isset($this->layoutConfig)) $this->layoutConfig = new PageComponentConfig;
$this->layoutConfig->type = 'extends';
$this->layoutConfig->slotOrSection = 'content';
$this->layoutConfig->view = $view;
$this->layoutConfig->mergeParams($params);
return $this;
});
View::macro('layout', function ($view, $params = []) {
if (! isset($this->layoutConfig)) $this->layoutConfig = new PageComponentConfig;
$this->layoutConfig->type = 'component';
$this->layoutConfig->slotOrSection = 'slot';
$this->layoutConfig->view = $view;
$this->layoutConfig->mergeParams($params);
return $this;
});
View::macro('response', function (callable $callback) {
if (! isset($this->layoutConfig)) $this->layoutConfig = new PageComponentConfig;
$this->layoutConfig->response = $callback;
return $this;
});
}
static function interceptTheRenderOfTheComponentAndRetreiveTheLayoutConfiguration($callback)
{
$layoutConfig = null;
$slots = [];
// Only run this handler once for the parent-most component. Otherwise child components
// will run this handler too and override the configured layout...
$handler = once(function ($target, $view, $data) use (&$layoutConfig, &$slots) {
$layoutAttr = $target->getAttributes()->whereInstanceOf(BaseLayout::class)->first();
$titleAttr = $target->getAttributes()->whereInstanceOf(BaseTitle::class)->first();
if ($layoutAttr) {
$view->layout($layoutAttr->name, $layoutAttr->params);
}
if ($titleAttr) {
$view->title($titleAttr->content);
}
$layoutConfig = $view->layoutConfig ?? new PageComponentConfig;
return function ($html, $replace, $viewContext) use ($view, $layoutConfig) {
// Gather up any slots and sections declared in the component template and store them
// to be later forwarded into the layout component itself...
$layoutConfig->viewContext = $viewContext;
};
});
on('render', $handler);
on('render.placeholder', $handler);
$callback();
off('render', $handler);
off('render.placeholder', $handler);
return $layoutConfig;
}
static function gatherMountMethodParamsFromRouteParameters($component)
{
// This allows for route parameters like "slug" in /post/{slug},
// to be passed into a Livewire component's mount method...
$route = request()->route();
if (! $route) return [];
try {
$params = (new ImplicitRouteBinding(app()))
->resolveAllParameters($route, new $component);
} catch (ModelNotFoundException $exception) {
if (method_exists($route,'getMissing') && $route->getMissing()) {
abort(
$route->getMissing()(request())
);
}
throw $exception;
}
return $params;
}
static function renderContentsIntoLayout($content, $layoutConfig)
{
try {
if ($layoutConfig->type === 'component') {
return Blade::render(<<<'HTML'
<?php $layout->viewContext->mergeIntoNewEnvironment($__env); ?>
@component($layout->view, $layout->params)
@slot($layout->slotOrSection)
{!! $content !!}
@endslot
<?php
// Manually forward slots defined in the Livewire template into the layout component...
foreach ($layout->viewContext->slots[-1] ?? [] as $name => $slot) {
$__env->slot($name, attributes: $slot->attributes->getAttributes());
echo $slot->toHtml();
$__env->endSlot();
}
?>
@endcomponent
HTML, [
'content' => $content,
'layout' => $layoutConfig,
]);
} else {
return Blade::render(<<<'HTML'
<?php $layout->viewContext->mergeIntoNewEnvironment($__env); ?>
@extends($layout->view, $layout->params)
@section($layout->slotOrSection)
{!! $content !!}
@endsection
HTML, [
'content' => $content,
'layout' => $layoutConfig,
]);
}
} catch (\Illuminate\View\ViewException $e) {
$layout = $layoutConfig->view;
if (str($e->getMessage())->startsWith('View ['.$layout.'] not found.')) {
throw new MissingLayoutException($layout);
} else {
throw $e;
}
}
}
// This needs to exist so that authorization middleware (and other middleware) have access
// to implicit route bindings based on the Livewire page component. Otherwise, Laravel
// has no implicit binding references because the action is __invoke with no params
protected static function resolvePageComponentRouteBindings()
{
// This method was introduced into Laravel 10.37.1 for this exact purpose...
if (static::canSubstituteImplicitBindings()) {
app('router')->substituteImplicitBindingsUsing(function ($container, $route, $default) {
// If the current route is a Livewire page component...
if ($componentClass = static::routeActionIsAPageComponent($route)) {
// Resolve and set all page component parameters to the current route...
(new \Livewire\Drawer\ImplicitRouteBinding($container))
->resolveAllParameters($route, new $componentClass);
} else {
// Otherwise, run the default Laravel implicit binding system...
$default();
}
});
}
}
public static function canSubstituteImplicitBindings()
{
return method_exists(app('router'), 'substituteImplicitBindingsUsing');
}
protected static function routeActionIsAPageComponent($route)
{
$action = $route->action;
if (! $action) return false;
$uses = $action['uses'] ?? false;
if (! $uses) return;
if (is_string($uses)) {
$class = str($uses)->before('@')->toString();
$method = str($uses)->after('@')->toString();
if (is_subclass_of($class, \Livewire\Component::class) && $method === '__invoke') {
return $class;
}
}
return false;
}
}