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