Completed
Push — master ( da5ca0...e55bc1 )
by Atanas
02:05
created

Framework::respond()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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