Completed
Push — milestone/2_0/react-ui ( 174588...64d64a )
by
unknown
13:43
created

Carbon_Fields::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields;
4
5
use Carbon_Fields\Pimple\Container as PimpleContainer;
6
use Carbon_Fields\Loader\Loader;
7
use Carbon_Fields\Container\Repository as ContainerRepository;
8
use Carbon_Fields\Toolset\Key_Toolset;
9
use Carbon_Fields\Toolset\WP_Toolset;
10
use Carbon_Fields\Service\Meta_Query_Service;
11
use Carbon_Fields\Service\Legacy_Storage_Service_v_1_5;
12
use Carbon_Fields\Service\REST_API_Service;
13
use Carbon_Fields\Libraries\Sidebar_Manager\Sidebar_Manager;
14
15
use Carbon_Fields\REST_API\Router as REST_API_Router;
16
use Carbon_Fields\REST_API\Decorator as REST_API_Decorator;
17
18
/**
19
 * Holds a static reference to the ioc container
20
 */
21
class Carbon_Fields {
22
23
	/**
24
	 * Flag if Carbon Fields has been booted
25
	 * 
26
	 * @var bool
27
	 */
28
	public $booted = false;
29
30
	/**
31
	 * Array of callables to call on boot
32
	 * 
33
	 * @var array<callable>
34
	 */
35
	public $boot_actions = array();
36
37
	/**
38
	 * Inversion of Control container instance
39
	 * 
40
	 * @var PimpleContainer
41
	 */
42
	protected $ioc = null;
43
44
	/**
45
	 * Singleton implementation
46
	 *
47
	 * @return Carbon_Fields\Carbon_Fields
48
	 */
49
	public static function instance() {
50
		static $instance = null;
51
		if ( $instance === null ) {
52
			$instance = new static();
53
		}
54
		return $instance;
55
	}
56
57
	/**
58
	 * Get default IoC container dependencies
59
	 * 
60
	 * @return PimpleContainer
61
	 */
62
	protected static function get_default_ioc() {
63
		$ioc = new PimpleContainer();
64
65
		$ioc['loader'] = function( $ioc ) {
66
			return new Loader( $ioc['sidebar_manager'], $ioc['container_repository'] );
67
		};
68
69
		$ioc['container_repository'] = function() {
70
			return new ContainerRepository();
71
		};
72
73
		$ioc['key_toolset'] = function() {
74
			return new Key_Toolset();
75
		};
76
77
		$ioc['wp_toolset'] = function() {
78
			return new WP_Toolset();
79
		};
80
81
		$ioc['sidebar_manager'] = function() {
82
			return new Sidebar_Manager();
83
		};
84
85
		$ioc['rest_api_router'] = function( $ioc ) {
86
			return new REST_API_Router( $ioc['container_repository'] );
87
		};
88
89
		$ioc['rest_api_decorator'] = function( $ioc ) {
90
			return new REST_API_Decorator( $ioc['container_repository'] );
91
		};
92
93
		/* Services */
94
		$ioc['meta_query_service'] = function( $ioc ) {
95
			return new Meta_Query_Service( $ioc['container_repository'], $ioc['key_toolset'] );
96
		};
97
98
		$ioc['legacy_storage_service'] = function( $ioc ) {
99
			return new Legacy_Storage_Service_v_1_5( $ioc['container_repository'], $ioc['key_toolset'] );
100
		};
101
102
		$ioc['rest_api_service'] = function( $ioc ) {
103
			return new REST_API_Service( $ioc['rest_api_router'], $ioc['rest_api_decorator'] );
104
		};
105
106
		\Carbon_Fields\Installer\Container_Condition_Installer::install( $ioc );
107
108
		return $ioc;
109
	}
110
111
	/**
112
	 * Resolve a dependency through IoC
113
	 *
114
	 * @param string $key
115
	 * @return mixed
116
	 */
117
	public static function resolve( $key ) {
118
		return static::instance()->ioc[ $key ];
119
	}
120
121
	/**
122
	 * Resolve a service through IoC
123
	 *
124
	 * @param string $service_name
125
	 * @return mixed
126
	 */
127
	public static function service( $service_name ) {
128
		return static::resolve( $service_name . '_service' );
129
	}
130
131
	/**
132
	 * Check if a dependency is registered
133
	 *
134
	 * @param string $key
135
	 * @return bool
136
	 */
137
	public static function has( $key ) {
138
		return isset( static::instance()->ioc[ $key ] );
139
	}
140
141
	/**
142
	 * Replace the ioc container for Carbon_Fields\Carbon_Fields
143
	 * 
144
	 * @param  PimpleContainer $ioc
145
	 */
146
	public function install( PimpleContainer $ioc ) {
147
		$this->ioc = $ioc;
148
	}
149
150
	/**
151
	 * Boot Carbon Fields with default IoC dependencies
152
	 */
153
	public static function boot() {
154
		if ( static::is_booted() ) {
155
			return;
156
		}
157
158
		if ( defined( __NAMESPACE__ . '\VERSION' ) ) {
159
			return; // Possibly attempting to load multiple versions of Carbon Fields; bail in favor of already loaded version
160
		}
161
162
		static::instance()->install( static::get_default_ioc() );
163
		static::resolve( 'loader' )->boot();
164
		static::instance()->booted = true;
165
166
		$boot_actions = static::instance()->boot_actions;
167
		foreach ( $boot_actions as $callable ) {
168
			call_user_func( $callable );
169
		}
170
	}
171
172
	/**
173
	 * Check if Carbon Fields has booted
174
	 */
175
	public static function is_booted() {
176
		return static::instance()->booted;
177
	}
178
179
	/**
180
	 * Throw exception if Carbon Fields has not been booted
181
	 */
182
	public static function verify_boot() {
183
		if ( ! static::is_booted() ) {
184
			throw new \Exception( 'You must call Carbon_Fields\Carbon_Fields::boot() in a suitable WordPress hook before using Carbon Fields.' );
185
		}
186
	}
187
188
	/**
189
	 * Add a callable to execute on boot
190
	 *
191
	 * @param callable $callable
192
	 */
193
	public static function on_boot( $callable ) {
194
		if ( ! is_callable( $callable ) ) {
195
			throw new \Exception( 'Invalid callable passed to Carbon_Fields\Carbon_Fields::on_boot().' );
196
		}
197
		static::instance()->boot_actions[] = $callable;
198
	}
199
}