Passed
Push — master ( 2f1c34...4bc1b6 )
by Atanas
02:03
created

Framework::getContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 5
cp 0.6
crap 2.2559
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
	 * @throws Exception
71
	 * @return null
72
	 */
73
	protected static function verifyBoot() {
74
		if ( ! static::isBooted() ) {
75
			throw new Exception( get_called_class() . ' must be booted first.' );
76
		}
77
	}
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 ) {
174 1
		AliasLoader::getInstance()->alias( $alias, $facade_class );
175 1
	}
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 ) {
184 2
		static::verifyBoot();
185
186 2
		if ( ! isset( static::getContainer()[ $key ] ) ) {
187 1
			return null;
188
		}
189
190 1
		return static::getContainer()[ $key ];
191
	}
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 1
		}
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 ) {
219
		Response::respond( $response );
220
	}
221
}
222