1 | <?php |
||
21 | class Framework { |
||
22 | /** |
||
23 | * Flag whether the framework has been booted |
||
24 | * |
||
25 | * @var boolean |
||
26 | */ |
||
27 | protected static $booted = false; |
||
28 | |||
29 | /** |
||
30 | * IoC container |
||
31 | * |
||
32 | * @var Container |
||
33 | */ |
||
34 | protected static $container = null; |
||
35 | |||
36 | /** |
||
37 | * Array of framework service providers |
||
38 | * |
||
39 | * @var string[] |
||
40 | */ |
||
41 | protected static $service_proviers = [ |
||
42 | RoutingServiceProvider::class, |
||
43 | FlashServiceProvider::class, |
||
44 | OldInputServiceProvider::class, |
||
45 | TemplatingServiceProvider::class, |
||
46 | ControllersServiceProvider::class, |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * Return whether WordPress is in debug mode |
||
51 | * |
||
52 | * @return boolean |
||
53 | */ |
||
54 | 1 | public static function debugging() { |
|
57 | |||
58 | /** |
||
59 | * Return whether the framework has been booted |
||
60 | * |
||
61 | * @return boolean |
||
62 | */ |
||
63 | 1 | public static function isBooted() { |
|
66 | |||
67 | /** |
||
68 | * Throw an exception if the framework has not been booted |
||
69 | * |
||
70 | * @throws Exception |
||
71 | * @return null |
||
72 | */ |
||
73 | protected static function verifyBoot() { |
||
78 | |||
79 | /** |
||
80 | * Return the IoC container instance |
||
81 | * |
||
82 | * @return Container |
||
83 | */ |
||
84 | 2 | public static function getContainer() { |
|
85 | 2 | if ( static::$container === null ) { |
|
86 | static::$container = new Container(); |
||
87 | } |
||
88 | 2 | return static::$container; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Boot the framework |
||
93 | * WordPress's 'init' action is a good place to call this |
||
94 | * |
||
95 | * @param array $config |
||
96 | * @throws Exception |
||
97 | * @return null |
||
98 | */ |
||
99 | 1 | public static function boot( $config = [] ) { |
|
100 | 1 | if ( static::isBooted() ) { |
|
101 | 1 | throw new Exception( get_called_class() . ' already booted.' ); |
|
102 | } |
||
103 | static::$booted = true; |
||
104 | |||
105 | $container = static::getContainer(); |
||
106 | |||
107 | $container['framework.config'] = array_merge( [ |
||
108 | 'providers' => [], |
||
109 | ], $config ); |
||
110 | |||
111 | $container['framework.service_providers'] = array_merge( |
||
112 | static::$service_proviers, |
||
113 | $container['framework.config']['providers'] |
||
114 | ); |
||
115 | |||
116 | Facade::setFacadeApplication( $container ); |
||
117 | AliasLoader::getInstance()->register(); |
||
118 | |||
119 | static::loadServiceProviders( $container ); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Register and boot all service providers |
||
124 | * |
||
125 | * @param Container $container |
||
126 | * @return null |
||
127 | */ |
||
128 | protected static function loadServiceProviders( $container ) { |
||
129 | $container['framework.service_providers'] = apply_filters( |
||
130 | 'obsidian_service_providers', |
||
131 | $container['framework.service_providers'] |
||
132 | ); |
||
133 | |||
134 | $service_providers = array_map( function( $service_provider ) { |
||
135 | return new $service_provider(); |
||
136 | }, $container['framework.service_providers'] ); |
||
137 | |||
138 | static::registerServiceProviders( $service_providers, $container ); |
||
139 | static::bootServiceProviders( $service_providers, $container ); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Register all service providers |
||
144 | * |
||
145 | * @param Container $container |
||
146 | * @return null |
||
147 | */ |
||
148 | protected static function registerServiceProviders( $service_providers, $container ) { |
||
149 | foreach ( $service_providers as $provider ) { |
||
150 | $provider->register( $container ); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Boot all service providers |
||
156 | * |
||
157 | * @param Container $container |
||
158 | * @return null |
||
159 | */ |
||
160 | protected static function bootServiceProviders( $service_providers, $container ) { |
||
161 | foreach ( $service_providers as $provider ) { |
||
162 | $provider->boot( $container ); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Register a facade class |
||
168 | * |
||
169 | * @param string $alias |
||
170 | * @param string $facade_class |
||
171 | * @return null |
||
172 | */ |
||
173 | 1 | public static function facade( $alias, $facade_class ) { |
|
176 | |||
177 | /** |
||
178 | * Resolve a dependency from the IoC container |
||
179 | * |
||
180 | * @param string $key |
||
181 | * @return mixed|null |
||
182 | */ |
||
183 | 2 | public static function resolve( $key ) { |
|
192 | |||
193 | /** |
||
194 | * Create and return a class instance |
||
195 | * |
||
196 | * @param string $class |
||
197 | * @return object |
||
198 | */ |
||
199 | 2 | public static function instantiate( $class ) { |
|
200 | 2 | static::verifyBoot(); |
|
201 | |||
202 | 2 | $instance = static::resolve( $class ); |
|
203 | |||
204 | 2 | if ( $instance === null ) { |
|
205 | 1 | $instance = new $class(); |
|
206 | } |
||
207 | |||
208 | 2 | return $instance; |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * Send output based on a response object |
||
213 | * |
||
214 | * @codeCoverageIgnore |
||
215 | * @param ResponseInterface $response |
||
216 | * @return null |
||
217 | */ |
||
218 | public static function respond( ResponseInterface $response ) { |
||
221 | } |
||
222 |