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