-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
599 lines (509 loc) · 16.5 KB
/
Copy pathApplication.php
File metadata and controls
599 lines (509 loc) · 16.5 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
<?php
/**
* Application class file.
*
* @package Mantle
*/
namespace Mantle\Application;
use Mantle\Container\Container;
use Mantle\Contracts\Bootstrapable;
use Mantle\Contracts\Kernel;
use Mantle\Framework\Manifest\Model_Manifest;
use Mantle\Framework\Manifest\Package_Manifest;
use Mantle\Support\Environment;
use RuntimeException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use function Mantle\Support\Helpers\collect;
use function Mantle\Support\Helpers\data_get;
use function Mantle\Support\Helpers\mixed;
use function Mantle\Support\Helpers\str;
/**
* Mantle Application
*/
class Application extends Container implements \Mantle\Contracts\Application {
use Concerns\Application_Callbacks;
use Concerns\Loads_Base_Configuration;
use Concerns\Loads_Environment_Variables;
use Concerns\Loads_Facades;
use Concerns\Manages_Service_Providers;
/**
* Base path of the application.
*/
protected string $base_path;
/**
* Application path of the application.
*/
protected string $app_path;
/**
* Bootstrap path of the application.
*/
protected ?string $bootstrap_path = null;
/**
* Storage path of the application.
*/
protected ?string $storage_path = null;
/**
* Root URL of the application.
*/
protected ?string $root_url = null;
/**
* Indicates if the application has been bootstrapped before.
*/
protected bool $has_been_bootstrapped = false;
/**
* Indicates if the application has "booted".
*/
protected bool $booted = false;
/**
* Environment file name.
*/
protected string $environment_file = '.env';
/**
* The custom environment path defined by the developer.
*/
protected ?string $environment_path = null;
/**
* Storage of the overridden environment name.
*/
protected ?string $environment = null;
/**
* Indicates if the application is running in the console.
*/
protected ?bool $is_running_in_console = null;
/**
* Storage of the application's namespace.
*/
protected string $namespace;
/**
* Constructor.
*
* @param string|null $base_path Base path to set.
* @param string $root_url Root URL of the application.
*/
public function __construct( ?string $base_path = null, ?string $root_url = null ) {
if ( empty( $base_path ) ) {
$base_path = match ( true ) {
isset( $_ENV['MANTLE_BASE_PATH'] ) => mixed( $_ENV['MANTLE_BASE_PATH'] )->string(), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
defined( 'MANTLE_BASE_DIR' ) => mixed( MANTLE_BASE_DIR )->string(),
default => '',
};
}
if ( ! $root_url ) {
$root_url = function_exists( 'home_url' ) ? \home_url() : '/';
}
$this->set_base_path( $base_path );
$this->set_root_url( $root_url );
$this->register_base_bindings();
$this->register_base_service_providers();
$this->register_core_aliases();
$this->register_base_services();
}
/**
* Set the base path of the application.
*
* @param string $path Path to set.
*/
public function set_base_path( string $path ): static {
$this->base_path = str( $path )->untrailingSlash()->value();
$this->instance( 'path', $this->get_base_path() );
$this->instance( 'path.bootstrap', $this->get_bootstrap_path() );
$this->instance( 'path.storage', $this->get_storage_path() );
return $this;
}
/**
* Getter for the base path.
*
* By default, this will not have a trailing slash.
*
* @param string $path Path to append.
*/
public function get_base_path( string $path = '' ): string {
if ( $path !== '' && $path !== '0' ) {
// Ensure the path being appended has a leading slash.
if ( ! str_starts_with( $path, '/' ) ) {
$path = '/' . $path;
}
return str( $this->base_path )->append( $path )->value();
}
return $this->base_path;
}
/**
* Get the path to the application "app" directory.
*
* @param string $path Path to append, optional.
*/
public function get_app_path( string $path = '' ): string {
if ( ! isset( $this->app_path ) ) {
$this->app_path = $this->get_base_path( 'app' );
}
return $this->app_path . ( $path !== '' && $path !== '0' ? DIRECTORY_SEPARATOR . $path : $path );
}
/**
* Set the application directory.
*
* @param string $path Path to use.
*/
public function set_app_path( string $path ): static {
$this->app_path = $path;
$this->instance( 'path', $path );
return $this;
}
/**
* Getter for the bootstrap path.
*
* @param string $path Path to append.
*/
public function get_bootstrap_path( string $path = '' ): string {
if ( $this->bootstrap_path ) {
return $path !== '' && $path !== '0' ? $this->bootstrap_path . DIRECTORY_SEPARATOR . $path : $this->bootstrap_path;
}
if ( function_exists( 'apply_filters' ) ) {
/**
* Filter the path to the bootstrap folder.
*
* @param string $cache_path Path to the bootstrap folder.
* @param Application $app Application instance.
*/
$this->bootstrap_path = apply_filters( 'mantle_bootstrap_path', $this->get_base_path( 'bootstrap' ), $this );
} else {
$this->bootstrap_path = $this->get_base_path( 'bootstrap' );
}
return $this->bootstrap_path . DIRECTORY_SEPARATOR . $path;
}
/**
* Getter for the storage path.
*
* @param string $path Path to append.
*/
public function get_storage_path( string $path = '' ): string {
if ( $this->storage_path ) {
return $this->storage_path . DIRECTORY_SEPARATOR . $path;
}
if ( function_exists( 'apply_filters' ) ) {
/**
* Filter the path to the storage folder.
*
* @param string $cache_path Path to the cache folder.
* @param Application $app Application instance.
*/
$this->storage_path = apply_filters( 'mantle_storage_path', $this->get_base_path( 'storage' ), $this );
} else {
$this->storage_path = $this->get_base_path( 'storage' );
}
return $this->storage_path . DIRECTORY_SEPARATOR . $path;
}
/**
* Set the root URL of the application.
*
* @param string|null $url Root URL to set, or null to use the default.
*/
public function set_root_url( ?string $url = null ): void {
if ( ! $url ) {
$url = function_exists( 'home_url' ) ? \home_url() : '/';
}
$this->root_url = $url;
}
/**
* Getter for the root URL.
* This would be the root URL to the WordPress installation.
*
* @param string $path Path to append.
*/
public function get_root_url( string $path = '' ): string {
return $this->root_url . ( $path !== '' && $path !== '0' ? '/' . $path : '' );
}
/**
* Get the cache folder root
* Folder that stores all compiled server-side assets for the application.
*
* @param string|null $path Path to append.
*/
public function get_cache_path( ?string $path = null ): string {
if ( function_exists( 'apply_filters' ) ) {
/**
* Filter the path to the cache folder.
*
* @param string $cache_path Path to the cache folder.
* @param Application $app Application instance.
*/
$cache_path = (string) apply_filters( 'mantle_cache_path', $this->get_bootstrap_path( 'cache' ), $this );
} else {
$cache_path = $this->get_bootstrap_path( 'cache' );
}
return $path ? $cache_path . DIRECTORY_SEPARATOR . $path : $cache_path;
}
/**
* Get the cached Composer packages path.
*
* Used to store all auto-loaded packages that are Composer dependencies.
*/
public function get_cached_packages_path(): string {
return $this->get_cache_path( 'packages.php' );
}
/**
* Get the cached model manifest path.
* Used to store all auto-registered models that are in the application.
*/
public function get_cached_models_path(): string {
return $this->get_cache_path( 'models.php' );
}
/**
* Determine if the application is cached.
*/
public function is_configuration_cached(): bool {
return is_file( $this->get_cached_config_path() );
}
/**
* Retrieve the cached configuration path.
*/
public function get_cached_config_path(): string {
return $this->get_bootstrap_path() . '/' . Environment::get_mixed( 'APP_CONFIG_CACHE', 'cache/config.php' )->string();
}
/**
* Determine if events are cached.
*/
public function is_events_cached(): bool {
return is_file( $this->get_cached_events_path() );
}
/**
* Retrieve the cached configuration path.
*/
public function get_cached_events_path(): string {
return $this->get_bootstrap_path() . '/' . Environment::get_mixed( 'APP_EVENTS_CACHE', 'cache/events.php' )->string();
}
/**
* Get the path to the application configuration files.
*/
public function get_config_path(): string {
return $this->get_base_path( 'config' );
}
/**
* Determine if the application has been bootstrapped before.
*/
public function has_been_bootstrapped(): bool {
return $this->has_been_bootstrapped;
}
/**
* Register the basic bindings into the container.
*
* @return void
*/
protected function register_base_bindings() {
static::set_instance( $this );
$this->instance( 'app', $this );
$this->instance( Container::class, $this );
$this->instance( \Mantle\Contracts\Container::class, $this );
$this->instance( static::class, $this );
$this->singleton(
Package_Manifest::class,
fn ( $app ) => new Package_Manifest( $this->get_base_path(), $this->get_cached_packages_path() ),
);
$this->singleton(
Model_Manifest::class,
fn ( $app ) => new Model_Manifest( $this->get_app_path(), $this->get_cached_models_path() ),
);
}
/**
* Register the core aliases.
*/
protected function register_core_aliases(): void {
$core_aliases = [
'app' => [ static::class, \Mantle\Contracts\Application::class ],
'config' => [ \Mantle\Config\Repository::class, \Mantle\Contracts\Config\Repository::class ],
'events' => [ \Mantle\Events\Dispatcher::class, \Mantle\Contracts\Events\Dispatcher::class ],
'files' => [ \Mantle\Filesystem\Filesystem::class ],
'filesystem' => [ \Mantle\Filesystem\Filesystem_Manager::class, \Mantle\Contracts\Filesystem\Filesystem_Manager::class ],
'log' => [ \Mantle\Log\Log_Manager::class, \Psr\Log\LoggerInterface::class ],
'queue' => [ \Mantle\Queue\Queue_Manager::class, \Mantle\Contracts\Queue\Queue_Manager::class ],
'queue.worker' => [ \Mantle\Queue\Worker::class ],
'queue.dispatcher' => [ \Mantle\Queue\Dispatcher::class, \Mantle\Contracts\Queue\Dispatcher::class ],
'redirect' => [ \Mantle\Http\Routing\Redirector::class ],
'request' => [ \Mantle\Http\Request::class, \Symfony\Component\HttpFoundation\Request::class ],
'router' => [ \Mantle\Http\Routing\Router::class, \Mantle\Contracts\Http\Routing\Router::class ],
'router.entity' => [ \Mantle\Http\Routing\Entity_Router::class, \Mantle\Contracts\Http\Routing\Entity_Router::class ],
'scheduler' => [ \Mantle\Scheduling\Schedule::class ],
'url' => [ \Mantle\Http\Routing\Url_Generator::class, \Mantle\Contracts\Http\Routing\Url_Generator::class ],
'view.loader' => [ \Mantle\Http\View\View_Finder::class, \Mantle\Contracts\Http\View\View_Finder::class ],
'view' => [ \Mantle\Http\View\Factory::class, \Mantle\Contracts\Http\View\Factory::class ],
];
foreach ( $core_aliases as $key => $aliases ) {
foreach ( $aliases as $alias ) {
$this->alias( $key, $alias );
}
}
}
/**
* Register the base services for the application.
*/
public function register_base_services(): void {
$this->load_environment_variables();
$this->load_base_configuration();
$this->load_facades();
}
/**
* Flush the container of all bindings and resolved instances.
*/
#[\Override]
public function flush(): void {
parent::flush();
$this->flush_callbacks();
}
/**
* Run the given array of bootstrap classes.
*
* Bootstrap classes should implement {@see \Mantle\Contracts\Bootstrapable}.
*
* @param array<mixed, class-string<Bootstrapable>> $bootstrappers Class names of packages to boot.
* @param \Mantle\Contracts\Kernel|null $kernel Kernel instance.
*/
public function bootstrap_with( array $bootstrappers, ?Kernel $kernel ): void {
$this->has_been_bootstrapped = true;
foreach ( $bootstrappers as $bootstrapper ) {
$this->class( $bootstrapper )->bootstrap( $this, $kernel );
}
}
/**
* Determine if the application has booted.
*/
public function is_booted(): bool {
return $this->booted;
}
/**
* Boot the application's service providers.
*/
public function boot(): static {
if ( $this->is_booted() ) {
return $this;
}
// Fire the 'booting' callbacks.
$this->fire_app_callbacks( $this->booting_callbacks );
foreach ( $this->service_providers as $service_provider ) {
$service_provider->boot_provider();
}
$this->booted = true;
// Fire the 'booted' callbacks.
$this->fire_app_callbacks( $this->booted_callbacks );
return $this;
}
/**
* Set and retrieve the environment file name.
*
* @param string $file File name to set.
*/
public function environment_file( ?string $file = null ): string {
if ( $file ) {
$this->environment_file = $file;
}
return $this->environment_file ?: '.env';
}
/**
* Set and retrieve the environment path to use.
*
* @param string $path Path to set, optional.
* @return string
*/
public function environment_path( ?string $path = null ): ?string {
if ( $path ) {
$this->environment_path = $path;
}
return $this->environment_path;
}
/**
* Get the Application's Environment
*/
public function environment(): string {
if ( ! empty( $this->environment ) ) {
return $this->environment;
}
return Environment::get_mixed( 'APP_ENV', function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : '' )->string();
}
/**
* Check if the Application's Environment matches a list.
*
* @param string ...$environments Environments to check.
*/
public function is_environment( ...$environments ): bool {
return in_array( $this->environment(), $environments, true );
}
/**
* Set the environment for the application.
*
* @param string $environment Environment to set.
*/
public function set_environment( string $environment ): static {
$this->environment = $environment;
return $this;
}
/**
* Get the application namespace.
*/
public function get_namespace(): string {
if ( ! empty( $this->namespace ) ) {
return $this->namespace;
}
if ( isset( $this['config'] ) && $this['config'] instanceof \Mantle\Contracts\Config\Repository ) {
$this->namespace = $this['config']->get_mixed( 'app.namespace' )->string();
}
// If the namespace is not set, attempt to infer it from the composer.json file.
if ( empty( $this->namespace ) && file_exists( $this->get_base_path( 'composer.json' ) ) ) {
$composer = json_decode( file_get_contents( $this->get_base_path( 'composer.json' ) ) ?: '', true );
$autoload = data_get( $composer, 'extra.wordpress-autoloader.autoload', [] );
if ( ! empty( $autoload ) && is_array( $autoload ) ) {
$this->namespace = str( (string) collect( $autoload )->keys()->first( default: '' ) )->rtrim( '\\' )->value();
}
}
if ( empty( $this->namespace ) ) {
return 'App';
}
return $this->namespace;
}
/**
* Alias to get_namespace().
*
* @throws RuntimeException Thrown on error determining namespace.
*/
public function namespace(): string {
return $this->get_namespace();
}
/**
* Check if the application is running in the console.
*/
public function is_running_in_console(): bool {
if ( $this->is_running_in_console_isolation() ) {
return true;
}
if ( null === $this->is_running_in_console ) {
$this->is_running_in_console = Environment::get( 'APP_RUNNING_IN_CONSOLE' ) || ( defined( 'WP_CLI' ) && WP_CLI && ! wp_doing_cron() );
}
return $this->is_running_in_console;
}
/**
* Check if the application is running in console isolation mode.
*/
public function is_running_in_console_isolation(): bool {
return defined( 'MANTLE_ISOLATION_MODE' ) && MANTLE_ISOLATION_MODE;
}
/**
* Throw an HttpException with the given data.
*
* @param int $code HTTP status code.
* @param string $message Response message.
* @param array<string, string> $headers Response headers.
*
* @throws NotFoundHttpException Thrown on 404 error.
* @throws HttpException Thrown on other HTTP error.
*/
public function abort( int $code, string $message = '', array $headers = [] ): void {
if ( 404 === $code ) {
throw new NotFoundHttpException( $message, null, 404, $headers );
}
throw new HttpException( $code, $message, null, $headers );
}
/**
* Terminate the application.
*/
public function terminate(): void {
$this->fire_app_callbacks( $this->terminating_callbacks );
}
}