1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CarbonFramework; |
4
|
|
|
|
5
|
|
|
use ReflectionException; |
6
|
|
|
use ReflectionMethod; |
7
|
|
|
use Exception; |
8
|
|
|
use Pimple\Container; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use CarbonFramework\Facades\Facade; |
11
|
|
|
use CarbonFramework\Support\AliasLoader; |
12
|
|
|
use CarbonFramework\ServiceProviders\Routing as RoutingServiceProvider; |
13
|
|
|
use CarbonFramework\ServiceProviders\Flash as FlashServiceProvider; |
14
|
|
|
use CarbonFramework\ServiceProviders\OldInput as OldInputServiceProvider; |
15
|
|
|
use CarbonFramework\ServiceProviders\Templating as TemplatingServiceProvider; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Main communication channel with the framework |
19
|
|
|
*/ |
20
|
|
|
class Framework { |
21
|
|
|
/** |
22
|
|
|
* Flag whether the framework has been booted |
23
|
|
|
* |
24
|
|
|
* @var boolean |
25
|
|
|
*/ |
26
|
|
|
protected static $booted = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* IoC container |
30
|
|
|
* |
31
|
|
|
* @var Container |
32
|
|
|
*/ |
33
|
|
|
protected static $container = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Return whether WordPress is in debug mode |
37
|
|
|
* |
38
|
|
|
* @return boolean |
39
|
|
|
*/ |
40
|
|
|
public static function debugging() { |
41
|
|
|
return ( defined( 'WP_DEBUG' ) && WP_DEBUG ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Return whether the framework has been booted |
46
|
|
|
* |
47
|
|
|
* @return boolean |
48
|
|
|
*/ |
49
|
|
|
public static function isBooted() { |
50
|
|
|
return static::$booted; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Throw an exception if the framework has not been booted |
55
|
|
|
* |
56
|
|
|
* @throws Exception |
57
|
|
|
* @return null |
58
|
|
|
*/ |
59
|
|
|
public static function verifyBoot() { |
60
|
|
|
if ( ! static::isBooted() ) { |
61
|
|
|
throw new Exception( get_called_class() . ' must be booted first.' ); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return the IoC container instance |
67
|
|
|
* |
68
|
|
|
* @return Container |
69
|
|
|
*/ |
70
|
|
|
public static function getContainer() { |
71
|
|
|
if ( static::$container === null ) { |
72
|
|
|
static::$container = new Container(); |
73
|
|
|
} |
74
|
|
|
return static::$container; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Boot the framework |
79
|
|
|
* WordPress's 'init' action is a good place to call this |
80
|
|
|
* |
81
|
|
|
* @param array $config |
82
|
|
|
* @throws Exception |
83
|
|
|
* @return null |
84
|
|
|
*/ |
85
|
|
|
public static function boot( $config ) { |
86
|
|
|
if ( static::isBooted() ) { |
87
|
|
|
throw new Exception( get_called_class() . ' already booted.' ); |
88
|
|
|
} |
89
|
|
|
static::$booted = true; |
90
|
|
|
|
91
|
|
|
$container = static::getContainer(); |
92
|
|
|
|
93
|
|
|
$container['framework.config'] = array_merge( [ |
94
|
|
|
'providers' => [], |
95
|
|
|
], $config ); |
96
|
|
|
|
97
|
|
|
$container['framework.service_providers'] = array_merge( [ |
98
|
|
|
RoutingServiceProvider::class, |
99
|
|
|
FlashServiceProvider::class, |
100
|
|
|
OldInputServiceProvider::class, |
101
|
|
|
TemplatingServiceProvider::class, |
102
|
|
|
], $container['framework.config']['providers'] ); |
103
|
|
|
|
104
|
|
|
Facade::setFacadeApplication( $container ); |
105
|
|
|
AliasLoader::getInstance()->register(); |
106
|
|
|
|
107
|
|
|
static::loadServiceProviders( $container ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Register and boot all service providers |
112
|
|
|
* |
113
|
|
|
* @param Container $container |
114
|
|
|
* @return null |
115
|
|
|
*/ |
116
|
|
|
protected static function loadServiceProviders( $container ) { |
117
|
|
|
$container['framework.service_providers'] = apply_filters( 'carbon_framework_service_providers', $container['framework.service_providers'] ); |
118
|
|
|
|
119
|
|
|
$service_providers = array_map( function( $service_provider ) { |
120
|
|
|
return new $service_provider(); |
121
|
|
|
}, $container['framework.service_providers'] ); |
122
|
|
|
|
123
|
|
|
static::registerServiceProviders( $service_providers, $container ); |
124
|
|
|
static::bootServiceProviders( $service_providers, $container ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Register all service providers |
129
|
|
|
* |
130
|
|
|
* @param Container $container |
131
|
|
|
* @return null |
132
|
|
|
*/ |
133
|
|
|
protected static function registerServiceProviders( $service_providers, $container ) { |
134
|
|
|
foreach ( $service_providers as $provider ) { |
135
|
|
|
$provider->register( $container ); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Boot all service providers |
141
|
|
|
* |
142
|
|
|
* @param Container $container |
143
|
|
|
* @return null |
144
|
|
|
*/ |
145
|
|
|
protected static function bootServiceProviders( $service_providers, $container ) { |
146
|
|
|
foreach ( $service_providers as $provider ) { |
147
|
|
|
$provider->boot( $container ); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Register a facade class |
153
|
|
|
* |
154
|
|
|
* @param string $alias |
155
|
|
|
* @param string $facade_class |
156
|
|
|
* @return null |
157
|
|
|
*/ |
158
|
|
|
public static function facade( $alias, $facade_class ) { |
159
|
|
|
AliasLoader::getInstance()->alias( $alias, $facade_class ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Resolve a dependency from the IoC container |
164
|
|
|
* |
165
|
|
|
* @param string $key |
166
|
|
|
* @return mixed|null |
167
|
|
|
*/ |
168
|
|
|
public static function resolve( $key ) { |
169
|
|
|
static::verifyBoot(); |
170
|
|
|
|
171
|
|
|
if ( ! isset( static::getContainer()[ $key ] ) ) { |
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return static::getContainer()[ $key ]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Create and return a class instance |
180
|
|
|
* |
181
|
|
|
* @param string $class |
182
|
|
|
* @return object |
183
|
|
|
*/ |
184
|
|
|
public static function instantiate( $class ) { |
185
|
|
|
static::verifyBoot(); |
186
|
|
|
|
187
|
|
|
$instance = static::resolve( $class ); |
188
|
|
|
|
189
|
|
|
if ( $instance === null ) { |
190
|
|
|
$instance = new $class(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $instance; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Send output based on a response object |
198
|
|
|
* |
199
|
|
|
* @param ResponseInterface $response |
200
|
|
|
* @return null |
201
|
|
|
*/ |
202
|
|
|
public static function respond( ResponseInterface $response ) { |
203
|
|
|
Response::respond( $response ); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|