Failed Conditions
Branch refactor/kernels (cc9370)
by Atanas
02:23
created

Application::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
ccs 0
cts 0
cp 0
c 0
b 0
f 0
cc 1
crap 2
rs 10
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Application;
11
12
use Pimple\Container;
13
use Psr\Http\Message\ResponseInterface;
14
use WPEmerge\Controllers\ControllersServiceProvider;
15
use WPEmerge\Csrf\CsrfServiceProvider;
16
use WPEmerge\Exceptions\ConfigurationException;
17
use WPEmerge\Exceptions\ExceptionsServiceProvider;
18
use WPEmerge\Flash\FlashServiceProvider;
19
use WPEmerge\Input\OldInputServiceProvider;
20
use WPEmerge\Kernels\KernelsServiceProvider;
21
use WPEmerge\Requests\RequestsServiceProvider;
22
use WPEmerge\Responses\ResponsesServiceProvider;
23
use WPEmerge\Routing\RoutingServiceProvider;
24
use WPEmerge\Support\AliasLoader;
25
use WPEmerge\View\ViewServiceProvider;
26
27
/**
28
 * Main communication channel with the application.
29
 */
30
class Application {
31
	/**
32
	 * Flag whether the application has been bootstrapped.
33
	 *
34
	 * @var boolean
35
	 */
36
	protected $bootstrapped = false;
37
38
	/**
39
	 * IoC container.
40
	 *
41
	 * @var Container
42
	 */
43
	protected $container = null;
44
45
	/**
46
	 * Array of application service providers.
47
	 *
48
	 * @var string[]
49
	 */
50
	protected $service_providers = [
51
		KernelsServiceProvider::class,
52
		ExceptionsServiceProvider::class,
53
		RequestsServiceProvider::class,
54
		ResponsesServiceProvider::class,
55
		RoutingServiceProvider::class,
56
		ViewServiceProvider::class,
57
		ControllersServiceProvider::class,
58
		CsrfServiceProvider::class,
59
		FlashServiceProvider::class,
60
		OldInputServiceProvider::class,
61
	];
62
63
	/**
64
	 * Constructor.
65
	 *
66
	 * @param Container $container
67
	 */
68 1
	public function __construct( Container $container ) {
69 1
		$this->container = $container;
70
71 1
		$config = isset( $container[ WPEMERGE_CONFIG_KEY ] ) ? $container[ WPEMERGE_CONFIG_KEY ] : [];
72 1
		$config = array_merge( [
73 1
			'providers' => [],
74 1
		], $config );
75 1
		$container[ WPEMERGE_CONFIG_KEY ] = $config;
76 1
	}
77
78
	/**
79
	 * Get whether WordPress is in debug mode.
80
	 *
81
	 * @return boolean
82
	 */
83 1
	public function debugging() {
84 1
		$debugging = ( defined( 'WP_DEBUG' ) && WP_DEBUG );
85 1
		$debugging = apply_filters( 'wpemerge.debug', $debugging );
86 1
		return $debugging;
87
	}
88
89
	/**
90
	 * Get whether the application has been bootstrapped.
91
	 *
92
	 * @return boolean
93
	 */
94 1
	public function isBootstrapped() {
95 1
		return $this->bootstrapped;
96
	}
97
98
	/**
99
	 * Throw an exception if the application has not been bootstrapped.
100
	 *
101
	 * @throws ConfigurationException
102
	 * @return void
103
	 */
104 5
	protected function verifyBootstrap() {
105 5
		if ( ! $this->isBootstrapped() ) {
106 1
			throw new ConfigurationException( static::class . ' must be bootstrapped first.' );
107
		}
108 4
	}
109
110
	/**
111
	 * Get the IoC container instance.
112
	 *
113
	 * @return Container
114
	 */
115 1
	public function getContainer() {
116 1
		return $this->container;
117
	}
118
119
	/**
120
	 * Bootstrap the application.
121
	 * WordPress' 'after_setup_theme' action is a good place to call this.
122
	 *
123
	 * @throws ConfigurationException
124
	 * @param  array $config
125
	 * @return void
126
	 */
127 3
	public function bootstrap( $config = [] ) {
128 3
		if ( $this->isBootstrapped() ) {
129 1
			throw new ConfigurationException( static::class . ' already bootstrapped.' );
130
		}
131
132 3
		$container = $this->getContainer();
133 3
		$this->loadConfig( $container, $config );
134 3
		$this->loadServiceProviders( $container );
135
136 3
		require_once WPEMERGE_DIR . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'wordpress.php';
137
		/*require_once WPEMERGE_DIR . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'wordpress-admin.php';*/
138
139 3
		$this->bootstrapped = true;
140 3
	}
141
142
	/**
143
	 * Load config into the service container.
144
	 *
145
	 * @codeCoverageIgnore
146
	 * @param  Container $container
147
	 * @param  array     $config
148
	 * @return void
149
	 */
150
	protected function loadConfig( Container $container, $config ) {
151
		$container[ WPEMERGE_CONFIG_KEY ] = array_merge(
152
			$container[ WPEMERGE_CONFIG_KEY ],
153
			$config
154
		);
155
	}
156
157
	/**
158
	 * Register and bootstrap all service providers.
159
	 *
160
	 * @codeCoverageIgnore
161
	 * @param  Container $container
162
	 * @return void
163
	 */
164
	protected function loadServiceProviders( Container $container ) {
165
		$container[ WPEMERGE_SERVICE_PROVIDERS_KEY ] = array_merge(
166
			$this->service_providers,
167
			$container[ WPEMERGE_CONFIG_KEY ]['providers']
168
		);
169
170
		$service_providers = array_map( function ( $service_provider ) {
171
			return new $service_provider();
172
		}, $container[ WPEMERGE_SERVICE_PROVIDERS_KEY ] );
173
174
		$this->registerServiceProviders( $service_providers, $container );
175
		$this->bootstrapServiceProviders( $service_providers, $container );
176
	}
177
178
	/**
179
	 * Register all service providers.
180
	 *
181
	 * @param  \WPEmerge\ServiceProviders\ServiceProviderInterface[] $service_providers
182
	 * @param  Container                                             $container
183
	 * @return void
184
	 */
185 1
	protected function registerServiceProviders( $service_providers, Container $container ) {
186 1
		foreach ( $service_providers as $provider ) {
187 1
			$provider->register( $container );
188
		}
189 1
	}
190
191
	/**
192
	 * Bootstrap all service providers.
193
	 *
194
	 * @param  \WPEmerge\ServiceProviders\ServiceProviderInterface[] $service_providers
195
	 * @param  Container                                             $container
196
	 * @return void
197
	 */
198 1
	protected function bootstrapServiceProviders( $service_providers, Container $container ) {
199 1
		foreach ( $service_providers as $provider ) {
200 1
			$provider->bootstrap( $container );
201
		}
202 1
	}
203
204
	/**
205
	 * Register a facade class.
206
	 *
207
	 * @param  string $alias
208
	 * @param  string $facade_class
209
	 * @return void
210
	 */
211 1
	public function facade( $alias, $facade_class ) {
212 1
		AliasLoader::getInstance()->alias( $alias, $facade_class );
213 1
	}
214
215
	/**
216
	 * Resolve a dependency from the IoC container.
217
	 *
218
	 * @param  string     $key
219
	 * @return mixed|null
220
	 */
221 2
	public function resolve( $key ) {
222 2
		$this->verifyBootstrap();
223
224 2
		if ( ! isset( $this->getContainer()[ $key ] ) ) {
225 1
			return null;
226
		}
227
228 1
		return $this->getContainer()[ $key ];
229
	}
230
231
	/**
232
	 * Create and return a class instance.
233
	 *
234
	 * @throws ClassNotFoundException
235
	 * @param  string $class
236
	 * @return object
237
	 */
238 3
	public function instantiate( $class ) {
239 3
		$this->verifyBootstrap();
240
241 3
		$instance = $this->resolve( $class );
242
243 3
		if ( $instance === null ) {
244 2
			if ( ! class_exists( $class ) ) {
245 1
				throw new ClassNotFoundException( 'Class not found: ' . $class );
246
			}
247
248 1
			$instance = new $class();
249
		}
250
251 2
		return $instance;
252
	}
253
254
	/**
255
	 * Send output based on a response object.
256
	 *
257
	 * @codeCoverageIgnore
258
	 * @param  ResponseInterface $response
259
	 * @return void
260
	 */
261
	public function respond( ResponseInterface $response ) {
262
		$this->resolve( WPEMERGE_RESPONSE_SERVICE_KEY )->respond( $response );
263
	}
264
}
265