Completed
Push — test/legacy-storage-service ( 13c02e...9939d7 )
by
unknown
02:29
created

App::resolve()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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\Template_Service;
11
use Carbon_Fields\Service\Meta_Query_Service;
12
use Carbon_Fields\Service\Legacy_Storage_Service;
13
use Carbon_Fields\Service\REST_API_Service;
14
use Carbon_Fields\Libraries\Sidebar_Manager\Sidebar_Manager;
15
16
use Carbon_Fields\REST_API\Router as REST_API_Router;
17
use Carbon_Fields\REST_API\Decorator as REST_API_Decorator;
18
19
20
/**
21
 * Holds a static reference to the ioc container
22
 */
23
class App {
24
25
	/**
26
	 * Flag if Carbon Fields has been booted
27
	 * 
28
	 * @var bool
29
	 */
30
	public $booted = false;
31
32
	/**
33
	 * Inversion of Control container instance
34
	 * 
35
	 * @var PimpleContainer
36
	 */
37
	protected $ioc = null;
38
39
	/**
40
	 * Singleton implementation
41
	 *
42
	 * @return App
43
	 */
44
	public static function instance() {
45
		static $instance = null;
46
		if ( $instance === null ) {
47
			$instance = new static();
48
		}
49
		return $instance;
50
	}
51
52
	/**
53
	 * Get default IoC container dependencies
54
	 * 
55
	 * @return PimpleContainer
56
	 */
57
	protected static function get_default_ioc() {
58
		$ioc = new PimpleContainer();
59
60
		$ioc['loader'] = function( $ioc ) {
61
			return new Loader( $ioc['template_service'], $ioc['sidebar_manager'], $ioc['container_repository'], $ioc['legacy_storage_service'], $ioc['rest_api_service'] );
62
		};
63
64
		$ioc['container_repository'] = function() {
65
			return new ContainerRepository();
66
		};
67
68
		$ioc['key_toolset'] = function() {
69
			return new Key_Toolset();
70
		};
71
72
		$ioc['wp_toolset'] = function() {
73
			return new WP_Toolset();
74
		};
75
76
		$ioc['template_service'] = function() {
77
			return new Template_Service();
78
		};
79
80
		$ioc['sidebar_manager'] = function() {
81
			return new Sidebar_Manager();
82
		};
83
84
		$ioc['rest_api_router'] = function( $ioc ) {
85
			return new REST_API_Router( $ioc['container_repository'] );
86
		};
87
88
		$ioc['rest_api_decorator'] = function( $ioc ) {
89
			return new REST_API_Decorator( $ioc['container_repository'] );
90
		};
91
92
		/* Services */
93
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( $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
		return $ioc;
107
	}
108
109
	/**
110
	 * Resolve a dependency through IoC
111
	 *
112
	 * @param string $key
113
	 * @return mixed
114
	 */
115
	public static function resolve( $key ) {
116
		return static::instance()->ioc[ $key ];
117
	}
118
119
	/**
120
	 * Resolve a service through IoC
121
	 *
122
	 * @param string $service_name
123
	 * @return mixed
124
	 */
125
	public static function service( $service_name ) {
126
		return static::resolve( $service_name . '_service' );
127
	}
128
129
	/**
130
	 * Replace the ioc container for the App
131
	 * 
132
	 * @param  PimpleContainer $ioc
133
	 */
134
	public function install( PimpleContainer $ioc ) {
135
		$this->ioc = $ioc;
136
	}
137
138
	/**
139
	 * Boot Carbon Fields with default IoC dependencies
140
	 */
141
	public static function boot() {
142
		if ( static::is_booted() ) {
143
			return;
144
		}
145
146
		if ( defined( __NAMESPACE__ . '\VERSION' ) ) {
147
			return; // Possibly attempting to load multiple versions of Carbon Fields; bail in favor of already loaded version
148
		}
149
150
		static::instance()->install( static::get_default_ioc() );
151
		static::resolve( 'loader' )->boot();
152
		static::instance()->booted = true;
153
	}
154
155
	/**
156
	 * Check if Carbon Fields has booted
157
	 */
158
	public static function is_booted() {
159
		return static::instance()->booted;
160
	}
161
162
	/**
163
	 * Throw exception if Carbon Fields has not been booted
164
	 */
165
	public static function verify_boot() {
166
		if ( ! static::is_booted() ) {
167
			throw new \Exception( 'You must call Carbon_Fields\App::boot() in a suitable WordPress hook before using Carbon Fields.' );
168
		}
169
	}
170
}