/
ApplicationBuilder.php
385 lines (333 loc) · 10.7 KB
/
ApplicationBuilder.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
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
<?php
namespace Illuminate\Foundation\Configuration;
use Closure;
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
use Illuminate\Contracts\Http\Kernel as HttpKernel;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Bootstrap\RegisterProviders;
use Illuminate\Foundation\Events\DiagnosingHealth;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as AppEventServiceProvider;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as AppRouteServiceProvider;
use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;
use Laravel\Folio\Folio;
class ApplicationBuilder
{
/**
* The service provider that are marked for registration.
*
* @var array
*/
protected array $pendingProviders = [];
/**
* The Folio / page middleware that have been defined by the user.
*
* @var array
*/
protected array $pageMiddleware = [];
/**
* Create a new application builder instance.
*/
public function __construct(protected Application $app)
{
}
/**
* Register the standard kernel classes for the application.
*
* @return $this
*/
public function withKernels()
{
$this->app->singleton(
\Illuminate\Contracts\Http\Kernel::class,
\Illuminate\Foundation\Http\Kernel::class,
);
$this->app->singleton(
\Illuminate\Contracts\Console\Kernel::class,
\Illuminate\Foundation\Console\Kernel::class,
);
return $this;
}
/**
* Register additional service providers.
*
* @param array $providers
* @param bool $withBootstrapProviders
* @return $this
*/
public function withProviders(array $providers = [], bool $withBootstrapProviders = true)
{
RegisterProviders::merge(
$providers,
$withBootstrapProviders
? $this->app->getBootstrapProvidersPath()
: null
);
return $this;
}
/**
* Register the core event service provider for the application.
*
* @param array $discover
* @return $this
*/
public function withEvents(array $discover = [])
{
if (count($discover) > 0) {
AppEventServiceProvider::setEventDiscoveryPaths($discover);
}
if (! isset($this->pendingProviders[AppEventServiceProvider::class])) {
$this->app->booting(function () {
$this->app->register(AppEventServiceProvider::class);
});
}
$this->pendingProviders[AppEventServiceProvider::class] = true;
return $this;
}
/**
* Register the braodcasting services for the application.
*
* @param string $channels
* @return $this
*/
public function withBroadcasting(string $channels)
{
$this->app->booted(function () use ($channels) {
Broadcast::routes();
if (file_exists($channels)) {
require $channels;
}
});
return $this;
}
/**
* Register the routing services for the application.
*
* @param \Closure|null $using
* @param string|null $web
* @param string|null $api
* @param string|null $commands
* @param string|null $channels
* @param string|null $pages
* @param string|null $apiPrefix
* @param callable|null $then
* @return $this
*/
public function withRouting(?Closure $using = null,
?string $web = null,
?string $api = null,
?string $commands = null,
?string $channels = null,
?string $pages = null,
?string $health = null,
string $apiPrefix = 'api',
?callable $then = null)
{
if (is_null($using) && (is_string($web) || is_string($api) || is_string($pages) || is_string($health)) || is_callable($then)) {
$using = $this->buildRoutingCallback($web, $api, $pages, $health, $apiPrefix, $then);
}
AppRouteServiceProvider::loadRoutesUsing($using);
$this->app->booting(function () {
$this->app->register(AppRouteServiceProvider::class, force: true);
});
if (is_string($commands) && realpath($commands) !== false) {
$this->withCommands([$commands]);
}
if (is_string($channels) && realpath($channels) !== false) {
$this->withBroadcasting($channels);
}
return $this;
}
/**
* Create the routing callback for the application.
*
* @param string|null $web
* @param string|null $api
* @param string|null $pages
* @param string|null $health
* @param string $apiPrefix
* @param callable|null $then
* @return \Closure
*/
protected function buildRoutingCallback(?string $web,
?string $api,
?string $pages,
?string $health,
string $apiPrefix,
?callable $then)
{
return function () use ($web, $api, $pages, $health, $apiPrefix, $then) {
if (is_string($api) && realpath($api) !== false) {
Route::middleware('api')->prefix($apiPrefix)->group($api);
}
if (is_string($health)) {
Route::middleware('web')->get($health, function () {
Event::dispatch(new DiagnosingHealth);
return View::file(__DIR__.'/../resources/health-up.blade.php');
});
}
if (is_string($web) && realpath($web) !== false) {
Route::middleware('web')->group($web);
}
if (is_string($pages) &&
realpath($pages) !== false &&
class_exists(Folio::class)) {
Folio::route($pages, middleware: $this->pageMiddleware);
}
if (is_callable($then)) {
$then($this->app);
}
};
}
/**
* Register the global middleware, middleware groups, and middleware aliases for the application.
*
* @param callable|null $callback
* @return $this
*/
public function withMiddleware(?callable $callback = null)
{
$this->app->afterResolving(HttpKernel::class, function ($kernel) use ($callback) {
$middleware = (new Middleware)
->redirectGuestsTo(fn () => route('login'));
if (! is_null($callback)) {
$callback($middleware);
}
$this->pageMiddleware = $middleware->getPageMiddleware();
$kernel->setGlobalMiddleware($middleware->getGlobalMiddleware());
$kernel->setMiddlewareGroups($middleware->getMiddlewareGroups());
$kernel->setMiddlewareAliases($middleware->getMiddlewareAliases());
if ($priorities = $middleware->getMiddlewarePriority()) {
$kernel->setMiddlewarePriority($priorities);
}
});
return $this;
}
/**
* Register additional Artisan commands with the application.
*
* @param array $commands
* @return $this
*/
public function withCommands(array $commands = [])
{
if (empty($commands)) {
$commands = [$this->app->path('Console/Commands')];
}
$this->app->afterResolving(ConsoleKernel::class, function ($kernel) use ($commands) {
[$commands, $paths] = collect($commands)->partition(fn ($command) => class_exists($command));
[$routes, $paths] = $paths->partition(fn ($path) => is_file($path));
$kernel->addCommands($commands->all());
$kernel->addCommandPaths($paths->all());
$kernel->addCommandRoutePaths($routes->all());
});
return $this;
}
/**
* Register additional Artisan route paths.
*
* @param array $paths
* @return $this
*/
protected function withCommandRouting(array $paths)
{
$this->app->afterResolving(ConsoleKernel::class, function ($kernel) use ($paths) {
$kernel->setCommandRoutePaths($paths);
});
}
/**
* Register and configure the application's exception handler.
*
* @param callable|null $using
* @return $this
*/
public function withExceptions(?callable $using = null)
{
$this->app->singleton(
\Illuminate\Contracts\Debug\ExceptionHandler::class,
\Illuminate\Foundation\Exceptions\Handler::class
);
$using ??= fn () => true;
$this->app->afterResolving(
\Illuminate\Foundation\Exceptions\Handler::class,
fn ($handler) => $using(new Exceptions($handler)),
);
return $this;
}
/**
* Register an array of container bindings to be bound when the application is booting.
*
* @param array $bindings
* @return $this
*/
public function withBindings(array $bindings)
{
return $this->registered(function ($app) use ($bindings) {
foreach ($bindings as $abstract => $concrete) {
$app->bind($abstract, $concrete);
}
});
}
/**
* Register an array of singleton container bindings to be bound when the application is booting.
*
* @param array $singletons
* @return $this
*/
public function withSingletons(array $singletons)
{
return $this->registered(function ($app) use ($singletons) {
foreach ($singletons as $abstract => $concrete) {
if (is_string($abstract)) {
$app->singleton($abstract, $concrete);
} else {
$app->singleton($concrete);
}
}
});
}
/**
* Register a callback to be invoked when the application's service providers are registered.
*
* @param callable $callback
* @return $this
*/
public function registered(callable $callback)
{
$this->app->registered($callback);
return $this;
}
/**
* Register a callback to be invoked when the application is "booting".
*
* @param callable $callback
* @return $this
*/
public function booting(callable $callback)
{
$this->app->booting($callback);
return $this;
}
/**
* Register a callback to be invoked when the application is "booted".
*
* @param callable $callback
* @return $this
*/
public function booted(callable $callback)
{
$this->app->booted($callback);
return $this;
}
/**
* Get the application instance.
*
* @return \Illuminate\Foundation\Application
*/
public function create()
{
return $this->app;
}
}