1 | <?php |
||
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 | 1 | public static function debugging() { |
|
43 | |||
44 | /** |
||
45 | * Return whether the framework has been booted |
||
46 | * |
||
47 | * @return boolean |
||
48 | */ |
||
49 | 1 | public static function isBooted() { |
|
52 | |||
53 | /** |
||
54 | * Throw an exception if the framework has not been booted |
||
55 | * |
||
56 | * @throws Exception |
||
57 | * @return null |
||
58 | */ |
||
59 | protected static function verifyBoot() { |
||
64 | |||
65 | /** |
||
66 | * Return the IoC container instance |
||
67 | * |
||
68 | * @return Container |
||
69 | */ |
||
70 | 2 | public static function getContainer() { |
|
71 | 2 | if ( static::$container === null ) { |
|
72 | static::$container = new Container(); |
||
73 | } |
||
74 | 2 | 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 | 1 | public static function boot( $config = [] ) { |
|
86 | 1 | if ( static::isBooted() ) { |
|
87 | 1 | 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 ) { |
||
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 | 1 | public static function facade( $alias, $facade_class ) { |
|
161 | |||
162 | /** |
||
163 | * Resolve a dependency from the IoC container |
||
164 | * |
||
165 | * @param string $key |
||
166 | * @return mixed|null |
||
167 | */ |
||
168 | 2 | public static function resolve( $key ) { |
|
177 | |||
178 | /** |
||
179 | * Create and return a class instance |
||
180 | * |
||
181 | * @param string $class |
||
182 | * @return object |
||
183 | */ |
||
184 | 2 | public static function instantiate( $class ) { |
|
185 | 2 | static::verifyBoot(); |
|
186 | |||
187 | 2 | $instance = static::resolve( $class ); |
|
188 | |||
189 | 2 | if ( $instance === null ) { |
|
190 | 1 | $instance = new $class(); |
|
191 | 1 | } |
|
192 | |||
193 | 2 | return $instance; |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * Send output based on a response object |
||
198 | * |
||
199 | * @codeCoverageIgnore |
||
200 | * @param ResponseInterface $response |
||
201 | * @return null |
||
202 | */ |
||
203 | public static function respond( ResponseInterface $response ) { |
||
206 | } |
||
207 |