1 | <?php |
||
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 ) { |
||
123 | |||
124 | /** |
||
125 | * Check if a dependency is registered |
||
126 | * |
||
127 | * @param string $key |
||
128 | * @return bool |
||
129 | */ |
||
130 | public static function has( $key ) { |
||
133 | |||
134 | /** |
||
135 | * Replace the ioc container for the App |
||
136 | * |
||
137 | * @param PimpleContainer $ioc |
||
138 | */ |
||
139 | public function install( PimpleContainer $ioc ) { |
||
142 | |||
143 | /** |
||
144 | * Boot Carbon Fields with default IoC dependencies |
||
145 | */ |
||
146 | public static function boot() { |
||
159 | |||
160 | /** |
||
161 | * Check if Carbon Fields has booted |
||
162 | */ |
||
163 | public static function is_booted() { |
||
166 | |||
167 | /** |
||
168 | * Throw exception if Carbon Fields has not been booted |
||
169 | */ |
||
170 | public static function verify_boot() { |
||
175 | } |