Passed
Push — master ( 517cb3...2c2725 )
by Atanas
02:15
created

Framework::isBooted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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