Passed
Push — master ( 222b05...0241e8 )
by Atanas
02:00
created

Framework::loadServiceProviders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 13
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Obsidian;
4
5
use ReflectionException;
6
use ReflectionMethod;
7
use Exception;
8
use Pimple\Container;
9
use Psr\Http\Message\ResponseInterface;
10
use Obsidian\Support\Facade;
11
use Obsidian\Support\AliasLoader;
12
use Obsidian\Routing\RoutingServiceProvider;
13
use Obsidian\Flash\FlashServiceProvider;
14
use Obsidian\Input\OldInputServiceProvider;
15
use Obsidian\Templating\TemplatingServiceProvider;
16
use Obsidian\Controllers\ControllersServiceProvider;
17
18
/**
19
 * Main communication channel with the framework
20
 */
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() {
55 1
		return ( defined( 'WP_DEBUG' ) && WP_DEBUG );
56
	}
57
58
	/**
59
	 * Return whether the framework has been booted
60
	 *
61
	 * @return boolean
62
	 */
63 1
	public static function isBooted() {
64 1
		return static::$booted;
65
	}
66
67
	/**
68
	 * Throw an exception if the framework has not been booted
69
	 *
70
	 * @codeCoverageIgnore
71
	 * @throws Exception
72
	 * @return null
73
	 */
74
	protected static function verifyBoot() {
75
		if ( ! static::isBooted() ) {
76
			throw new Exception( get_called_class() . ' must be booted first.' );
77
		}
78
	}
79
80
	/**
81
	 * Return the IoC container instance
82
	 *
83
	 * @return Container
84
	 */
85 2
	public static function getContainer() {
86
		// @codeCoverageIgnoreStart
87
		if ( static::$container === null ) {
88
			static::$container = new Container();
89
		}
90
		// @codeCoverageIgnoreEnd
91 2
		return static::$container;
92
	}
93
94
	/**
95
	 * Boot the framework
96
	 * WordPress's 'after_setup_theme' action is a good place to call this
97
	 *
98
	 * @codeCoverageIgnore
99
	 * @param  array     $config
100
	 * @throws Exception
101
	 * @return null
102
	 */
103
	public static function boot( $config = [] ) {
104
		if ( static::isBooted() ) {
105
			throw new Exception( get_called_class() . ' already booted.' );
106
		}
107
		static::$booted = true;
108
109
		$container = static::getContainer();
110
111
		$container['framework.config'] = array_merge( [
112
			'providers' => [],
113
		], $config );
114
115
		$container['framework.service_providers'] = array_merge(
116
			static::$service_proviers,
117
			$container['framework.config']['providers']
118
		);
119
120
		Facade::setFacadeApplication( $container );
121
		AliasLoader::getInstance()->register();
122
123
		static::loadServiceProviders( $container );
124
	}
125
126
	/**
127
	 * Register and boot all service providers
128
	 *
129
	 * @codeCoverageIgnore
130
	 * @param  Container $container
131
	 * @return null
132
	 */
133
	protected static function loadServiceProviders( $container ) {
134
		$container['framework.service_providers'] = apply_filters(
135
			'obsidian.service_providers',
136
			$container['framework.service_providers']
137
		);
138
139
		$service_providers = array_map( function( $service_provider ) {
140
			return new $service_provider();
141
		}, $container['framework.service_providers'] );
142
143
		static::registerServiceProviders( $service_providers, $container );
144
		static::bootServiceProviders( $service_providers, $container );
145
	}
146
147
	/**
148
	 * Register all service providers
149
	 *
150
	 * @codeCoverageIgnore
151
	 * @param  Container $container
152
	 * @return null
153
	 */
154
	protected static function registerServiceProviders( $service_providers, $container ) {
155
		foreach ( $service_providers as $provider ) {
156
			$provider->register( $container );
157
		}
158
	}
159
160
	/**
161
	 * Boot all service providers
162
	 *
163
	 * @codeCoverageIgnore
164
	 * @param  Container $container
165
	 * @return null
166
	 */
167
	protected static function bootServiceProviders( $service_providers, $container ) {
168
		foreach ( $service_providers as $provider ) {
169
			$provider->boot( $container );
170
		}
171
	}
172
173
	/**
174
	 * Register a facade class
175
	 *
176
	 * @param  string $alias
177
	 * @param  string $facade_class
178
	 * @return null
179
	 */
180 1
	public static function facade( $alias, $facade_class ) {
181 1
		AliasLoader::getInstance()->alias( $alias, $facade_class );
182 1
	}
183
184
	/**
185
	 * Resolve a dependency from the IoC container
186
	 *
187
	 * @param  string   $key
188
	 * @return mixed|null
189
	 */
190 2
	public static function resolve( $key ) {
191 2
		static::verifyBoot();
192
193 2
		if ( ! isset( static::getContainer()[ $key ] ) ) {
194 1
			return null;
195
		}
196
197 1
		return static::getContainer()[ $key ];
198
	}
199
200
	/**
201
	 * Create and return a class instance
202
	 *
203
	 * @param  string $class
204
	 * @return object
205
	 */
206 2
	public static function instantiate( $class ) {
207 2
		static::verifyBoot();
208
209 2
		$instance = static::resolve( $class );
210
211 2
		if ( $instance === null ) {
212 1
			$instance = new $class();
213 1
		}
214
215 2
		return $instance;
216
	}
217
218
	/**
219
	 * Send output based on a response object
220
	 *
221
	 * @codeCoverageIgnore
222
	 * @param  ResponseInterface $response
223
	 * @return null
224
	 */
225
	public static function respond( ResponseInterface $response ) {
226
		Response::respond( $response );
227
	}
228
}
229