Passed
Push — master ( fa63c5...708113 )
by
unknown
01:57
created

Framework   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 38.46%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 188
rs 10
c 2
b 0
f 0
ccs 25
cts 65
cp 0.3846
wmc 20
lcom 1
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A debugging() 0 3 2
A isBooted() 0 3 1
A verifyBoot() 0 5 2
A getContainer() 0 6 2
B boot() 0 25 2
A loadServiceProviders() 0 10 1
A registerServiceProviders() 0 5 2
A bootServiceProviders() 0 5 2
A facade() 0 3 1
A resolve() 0 9 2
A instantiate() 0 11 2
A respond() 0 3 1
1
<?php
2
3
namespace CarbonFramework;
4
5
use ReflectionException;
6
use ReflectionMethod;
7
use Exception;
8
use Pimple\Container;
9
use Psr\Http\Message\ResponseInterface;
10
use CarbonFramework\Support\Facade;
11
use CarbonFramework\Support\AliasLoader;
12
use CarbonFramework\Routing\RoutingServiceProvider;
13
use CarbonFramework\Flash\FlashServiceProvider;
14
use CarbonFramework\Input\OldInputServiceProvider;
15
use CarbonFramework\Templating\TemplatingServiceProvider;
16
use CarbonFramework\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
	 * Return whether WordPress is in debug mode
38
	 *
39
	 * @return boolean
40
	 */
41 1
	public static function debugging() {
42 1
		return ( defined( 'WP_DEBUG' ) && WP_DEBUG );
43
	}
44
45
	/**
46
	 * Return whether the framework has been booted
47
	 *
48
	 * @return boolean
49
	 */
50 1
	public static function isBooted() {
51 1
		return static::$booted;
52
	}
53
54
	/**
55
	 * Throw an exception if the framework has not been booted
56
	 *
57
	 * @throws Exception
58
	 * @return null
59
	 */
60
	protected static function verifyBoot() {
61
		if ( ! static::isBooted() ) {
62
			throw new Exception( get_called_class() . ' must be booted first.' );
63
		}
64
	}
65
66
	/**
67
	 * Return the IoC container instance
68
	 *
69
	 * @return Container
70
	 */
71 2
	public static function getContainer() {
72 2
		if ( static::$container === null ) {
73
			static::$container = new Container();
74
		}
75 2
		return static::$container;
76
	}
77
78
	/**
79
	 * Boot the framework
80
	 * WordPress's 'init' action is a good place to call this
81
	 *
82
	 * @param  array     $config
83
	 * @throws Exception
84
	 * @return null
85
	 */
86 1
	public static function boot( $config = [] ) {
87 1
		if ( static::isBooted() ) {
88 1
			throw new Exception( get_called_class() . ' already booted.' );
89
		}
90
		static::$booted = true;
91
92
		$container = static::getContainer();
93
94
		$container['framework.config'] = array_merge( [
95
			'providers' => [],
96
		], $config );
97
98
		$container['framework.service_providers'] = array_merge( [
99
			RoutingServiceProvider::class,
100
			FlashServiceProvider::class,
101
			OldInputServiceProvider::class,
102
			TemplatingServiceProvider::class,
103
			ControllersServiceProvider::class,
104
		], $container['framework.config']['providers'] );
105
106
		Facade::setFacadeApplication( $container );
107
		AliasLoader::getInstance()->register();
108
109
		static::loadServiceProviders( $container );
110
	}
111
112
	/**
113
	 * Register and boot all service providers
114
	 *
115
	 * @param  Container $container
116
	 * @return null
117
	 */
118
	protected static function loadServiceProviders( $container ) {
119
		$container['framework.service_providers'] = apply_filters( 'carbon_framework_service_providers', $container['framework.service_providers'] );
120
121
		$service_providers = array_map( function( $service_provider ) {
122
			return new $service_provider();
123
		}, $container['framework.service_providers'] );
124
125
		static::registerServiceProviders( $service_providers, $container );
126
		static::bootServiceProviders( $service_providers, $container );
127
	}
128
129
	/**
130
	 * Register all service providers
131
	 *
132
	 * @param  Container $container
133
	 * @return null
134
	 */
135
	protected static function registerServiceProviders( $service_providers, $container ) {
136
		foreach ( $service_providers as $provider ) {
137
			$provider->register( $container );
138
		}
139
	}
140
141
	/**
142
	 * Boot all service providers
143
	 *
144
	 * @param  Container $container
145
	 * @return null
146
	 */
147
	protected static function bootServiceProviders( $service_providers, $container ) {
148
		foreach ( $service_providers as $provider ) {
149
			$provider->boot( $container );
150
		}
151
	}
152
153
	/**
154
	 * Register a facade class
155
	 *
156
	 * @param  string $alias
157
	 * @param  string $facade_class
158
	 * @return null
159
	 */
160 1
	public static function facade( $alias, $facade_class ) {
161 1
		AliasLoader::getInstance()->alias( $alias, $facade_class );
162 1
	}
163
164
	/**
165
	 * Resolve a dependency from the IoC container
166
	 *
167
	 * @param  string   $key
168
	 * @return mixed|null
169
	 */
170 2
	public static function resolve( $key ) {
171 2
		static::verifyBoot();
172
173 2
		if ( ! isset( static::getContainer()[ $key ] ) ) {
174 1
			return null;
175
		}
176
177 1
		return static::getContainer()[ $key ];
178
	}
179
180
	/**
181
	 * Create and return a class instance
182
	 *
183
	 * @param  string $class
184
	 * @return object
185
	 */
186 2
	public static function instantiate( $class ) {
187 2
		static::verifyBoot();
188
189 2
		$instance = static::resolve( $class );
190
191 2
		if ( $instance === null ) {
192 1
			$instance = new $class();
193 1
		}
194
195 2
		return $instance;
196
	}
197
198
	/**
199
	 * Send output based on a response object
200
	 *
201
	 * @codeCoverageIgnore
202
	 * @param  ResponseInterface $response
203
	 * @return null
204
	 */
205
	public static function respond( ResponseInterface $response ) {
206
		Response::respond( $response );
207
	}
208
}
209