1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WPEmerge |
4
|
|
|
* @author Atanas Angelov <[email protected]> |
5
|
|
|
* @copyright 2017-2019 Atanas Angelov |
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
7
|
|
|
* @link https://wpemerge.com/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WPEmerge\Application; |
11
|
|
|
|
12
|
|
|
use Closure; |
13
|
|
|
use Pimple\Container; |
14
|
|
|
use WPEmerge\Exceptions\ConfigurationException; |
15
|
|
|
use WPEmerge\Requests\Request; |
16
|
|
|
use WPEmerge\Support\Arr; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The core WP Emerge component representing an application. |
20
|
|
|
*/ |
21
|
|
|
class Application { |
22
|
|
|
use HasAliasesTrait; |
23
|
|
|
use LoadsServiceProvidersTrait; |
24
|
|
|
use HasContainerTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Flag whether to intercept and render configuration exceptions. |
28
|
|
|
* |
29
|
|
|
* @var boolean |
30
|
|
|
*/ |
31
|
|
|
protected $render_config_exceptions = true; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Flag whether the application has been bootstrapped. |
35
|
|
|
* |
36
|
|
|
* @var boolean |
37
|
|
|
*/ |
38
|
|
|
protected $bootstrapped = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Make a new application instance. |
42
|
|
|
* |
43
|
|
|
* @codeCoverageIgnore |
44
|
|
|
* @return static |
45
|
|
|
*/ |
46
|
|
|
public static function make() { |
47
|
|
|
return new static( new Container() ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor. |
52
|
|
|
* |
53
|
|
|
* @param Container $container |
54
|
|
|
* @param boolean $render_config_exceptions |
55
|
|
|
*/ |
56
|
1 |
|
public function __construct( Container $container, $render_config_exceptions = true ) { |
57
|
1 |
|
$this->setContainer( $container ); |
58
|
1 |
|
$this->container()[ WPEMERGE_APPLICATION_KEY ] = $this; |
59
|
1 |
|
$this->render_config_exceptions = $render_config_exceptions; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get whether the application has been bootstrapped. |
64
|
|
|
* |
65
|
|
|
* @return boolean |
66
|
|
|
*/ |
67
|
1 |
|
public function isBootstrapped() { |
68
|
1 |
|
return $this->bootstrapped; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Bootstrap the application. |
73
|
|
|
* |
74
|
|
|
* @param array $config |
75
|
|
|
* @param boolean $run |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
4 |
|
public function bootstrap( $config = [], $run = true ) { |
79
|
4 |
|
if ( $this->isBootstrapped() ) { |
80
|
1 |
|
throw new ConfigurationException( static::class . ' already bootstrapped.' ); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
$this->bootstrapped = true; |
84
|
|
|
|
85
|
4 |
|
$container = $this->container(); |
86
|
4 |
|
$this->loadConfig( $container, $config ); |
87
|
4 |
|
$this->loadServiceProviders( $container ); |
88
|
|
|
|
89
|
|
|
$this->renderConfigExceptions( function () use ( $run ) { |
90
|
4 |
|
$this->loadRoutes(); |
91
|
|
|
|
92
|
4 |
|
if ( $run ) { |
93
|
1 |
|
$kernel = $this->resolve( WPEMERGE_WORDPRESS_HTTP_KERNEL_KEY ); |
94
|
1 |
|
$kernel->bootstrap(); |
95
|
|
|
} |
96
|
4 |
|
} ); |
97
|
4 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Load config into the service container. |
101
|
|
|
* |
102
|
|
|
* @codeCoverageIgnore |
103
|
|
|
* @param Container $container |
104
|
|
|
* @param array $config |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
protected function loadConfig( Container $container, $config ) { |
108
|
|
|
$container[ WPEMERGE_CONFIG_KEY ] = $config; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Load route definition files depending on the current request. |
113
|
|
|
* |
114
|
|
|
* @codeCoverageIgnore |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function loadRoutes() { |
118
|
|
|
if ( wp_doing_ajax() ) { |
119
|
|
|
$this->loadRoutesGroup( 'ajax' ); |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ( is_admin() ) { |
124
|
|
|
$this->loadRoutesGroup( 'admin' ); |
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->loadRoutesGroup( 'web' ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Load a route group applying default attributes, if any. |
133
|
|
|
* |
134
|
|
|
* @codeCoverageIgnore |
135
|
|
|
* @param string $group |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
protected function loadRoutesGroup( $group ) { |
139
|
|
|
$config = $this->resolve( WPEMERGE_CONFIG_KEY ); |
140
|
|
|
$file = Arr::get( $config, 'routes.' . $group . '.definitions', '' ); |
141
|
|
|
$attributes = Arr::get( $config, 'routes.' . $group . '.attributes', [] ); |
142
|
|
|
|
143
|
|
|
if ( empty( $file ) ) { |
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$middleware = Arr::get( $attributes, 'middleware', [] ); |
148
|
|
|
|
149
|
|
|
if ( ! in_array( $group, $middleware, true ) ) { |
150
|
|
|
$middleware = array_merge( [$group], $middleware ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$attributes['middleware'] = $middleware; |
154
|
|
|
|
155
|
|
|
$blueprint = $this->resolve( WPEMERGE_ROUTING_ROUTE_BLUEPRINT_KEY ); |
156
|
|
|
$blueprint->attributes( $attributes )->group( $file ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Catch any configuration exceptions and short-circuit to an error page. |
161
|
|
|
* |
162
|
|
|
* @codeCoverageIgnore |
163
|
|
|
* @param Closure $action |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
public function renderConfigExceptions( Closure $action ) { |
167
|
|
|
try { |
168
|
|
|
$action(); |
169
|
|
|
} catch ( ConfigurationException $exception ) { |
170
|
|
|
if ( ! $this->render_config_exceptions ) { |
171
|
|
|
throw $exception; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$request = Request::fromGlobals(); |
175
|
|
|
$handler = $this->resolve( WPEMERGE_EXCEPTIONS_CONFIGURATION_ERROR_HANDLER_KEY ); |
176
|
|
|
|
177
|
|
|
add_filter( 'wpemerge.pretty_errors.apply_admin_styles', '__return_false' ); |
178
|
|
|
|
179
|
|
|
$response_service = $this->resolve( WPEMERGE_RESPONSE_SERVICE_KEY ); |
180
|
|
|
$response_service->respond( $handler->getResponse( $request, $exception ) ); |
181
|
|
|
|
182
|
|
|
wp_die(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|