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\Key_Toolset\Key_Toolset; |
9
|
|
|
use \Carbon_Fields\Templater\Templater; |
10
|
|
|
use \Carbon_Fields\Service\Meta_Query_Service; |
11
|
|
|
use \Carbon_Fields\Service\Legacy_Storage_Service; |
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
|
|
|
/** |
20
|
|
|
* Holds a static reference to the ioc container |
21
|
|
|
*/ |
22
|
|
|
class App { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Flag if Carbon Fields has been booted |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
public $booted = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Inversion of Control container instance |
33
|
|
|
* |
34
|
|
|
* @var PimpleContainer |
35
|
|
|
*/ |
36
|
|
|
protected $ioc = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Singleton implementation |
40
|
|
|
* |
41
|
|
|
* @return App |
42
|
|
|
*/ |
43
|
|
|
public static function instance() { |
44
|
|
|
static $instance = null; |
45
|
|
|
if ( $instance === null ) { |
46
|
|
|
$instance = new static(); |
47
|
|
|
} |
48
|
|
|
return $instance; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get default IoC container dependencies |
53
|
|
|
* |
54
|
|
|
* @return PimpleContainer |
55
|
|
|
*/ |
56
|
|
|
protected static function get_default_ioc() { |
57
|
|
|
$ioc = new PimpleContainer(); |
58
|
|
|
|
59
|
|
|
$ioc['loader'] = function( $ioc ) { |
60
|
|
|
return new Loader( $ioc['templater'], $ioc['sidebar_manager'], $ioc['container_repository'], $ioc['legacy_storage_service'], $ioc['rest_api_service'] ); |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
$ioc['container_repository'] = function() { |
64
|
|
|
return new ContainerRepository(); |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
$ioc['key_toolset'] = function() { |
68
|
|
|
return new Key_Toolset(); |
69
|
|
|
}; |
70
|
|
|
|
71
|
|
|
$ioc['templater'] = function() { |
72
|
|
|
return new Templater(); |
73
|
|
|
}; |
74
|
|
|
|
75
|
|
|
$ioc['sidebar_manager'] = function() { |
76
|
|
|
return new Sidebar_Manager(); |
77
|
|
|
}; |
78
|
|
|
|
79
|
|
|
$ioc['rest_api_router'] = function( $ioc ) { |
80
|
|
|
return new REST_API_Router( $ioc['container_repository'] ); |
81
|
|
|
}; |
82
|
|
|
|
83
|
|
|
$ioc['rest_api_decorator'] = function( $ioc ) { |
84
|
|
|
return new REST_API_Decorator( $ioc['container_repository'] ); |
85
|
|
|
}; |
86
|
|
|
|
87
|
|
|
/* Services */ |
88
|
|
|
|
89
|
|
|
$ioc['meta_query_service'] = function( $ioc ) { |
90
|
|
|
return new Meta_Query_Service( $ioc['container_repository'], $ioc['key_toolset'] ); |
91
|
|
|
}; |
92
|
|
|
|
93
|
|
|
$ioc['legacy_storage_service'] = function( $ioc ) { |
94
|
|
|
return new Legacy_Storage_Service( $ioc['container_repository'], $ioc['key_toolset'] ); |
95
|
|
|
}; |
96
|
|
|
|
97
|
|
|
$ioc['rest_api_service'] = function( $ioc ) { |
98
|
|
|
return new REST_API_Service( $ioc['rest_api_router'], $ioc['rest_api_decorator'] ); |
99
|
|
|
}; |
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
|
|
|
* Replace the ioc container for the App |
126
|
|
|
* |
127
|
|
|
* @param PimpleContainer $ioc |
128
|
|
|
*/ |
129
|
|
|
public function install( PimpleContainer $ioc ) { |
130
|
|
|
$this->ioc = $ioc; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Boot Carbon Fields with default IoC dependencies |
135
|
|
|
*/ |
136
|
|
|
public static function boot() { |
137
|
|
|
if ( static::is_booted() ) { |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ( defined( __NAMESPACE__ . '\VERSION' ) ) { |
142
|
|
|
return; // Possibly attempting to load multiple versions of Carbon Fields; bail in favor of already loaded version |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
static::instance()->install( static::get_default_ioc() ); |
146
|
|
|
static::resolve( 'loader' )->boot(); |
147
|
|
|
static::instance()->booted = true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Check if Carbon Fields has booted |
152
|
|
|
*/ |
153
|
|
|
public static function is_booted() { |
154
|
|
|
return static::instance()->booted; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Throw exception if Carbon Fields has not been booted |
159
|
|
|
*/ |
160
|
|
|
public static function verify_boot() { |
161
|
|
|
if ( ! static::is_booted() ) { |
162
|
|
|
throw new \Exception( 'You must call \Carbon_Fields\App::boot() in a suitable WordPress hook before using Carbon Fields.' ); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |