@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | use EventEspresso\core\services\loaders\LoaderInterface; |
6 | 6 | |
7 | 7 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
8 | - exit('No direct script access allowed'); |
|
8 | + exit('No direct script access allowed'); |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | |
@@ -22,813 +22,813 @@ discard block |
||
22 | 22 | class EE_Dependency_Map |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * This means that the requested class dependency is not present in the dependency map |
|
27 | - */ |
|
28 | - const not_registered = 0; |
|
29 | - |
|
30 | - /** |
|
31 | - * This instructs class loaders to ALWAYS return a newly instantiated object for the requested class. |
|
32 | - */ |
|
33 | - const load_new_object = 1; |
|
34 | - |
|
35 | - /** |
|
36 | - * This instructs class loaders to return a previously instantiated and cached object for the requested class. |
|
37 | - * IF a previously instantiated object does not exist, a new one will be created and added to the cache. |
|
38 | - */ |
|
39 | - const load_from_cache = 2; |
|
40 | - |
|
41 | - /** |
|
42 | - * When registering a dependency, |
|
43 | - * this indicates to keep any existing dependencies that already exist, |
|
44 | - * and simply discard any new dependencies declared in the incoming data |
|
45 | - */ |
|
46 | - const KEEP_EXISTING_DEPENDENCIES = 0; |
|
47 | - |
|
48 | - /** |
|
49 | - * When registering a dependency, |
|
50 | - * this indicates to overwrite any existing dependencies that already exist using the incoming data |
|
51 | - */ |
|
52 | - const OVERWRITE_DEPENDENCIES = 1; |
|
53 | - |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * @type EE_Dependency_Map $_instance |
|
58 | - */ |
|
59 | - protected static $_instance; |
|
60 | - |
|
61 | - /** |
|
62 | - * @type EE_Request $request |
|
63 | - */ |
|
64 | - protected $_request; |
|
65 | - |
|
66 | - /** |
|
67 | - * @type EE_Response $response |
|
68 | - */ |
|
69 | - protected $_response; |
|
70 | - |
|
71 | - /** |
|
72 | - * @type LoaderInterface $loader |
|
73 | - */ |
|
74 | - protected $loader; |
|
75 | - |
|
76 | - /** |
|
77 | - * @type array $_dependency_map |
|
78 | - */ |
|
79 | - protected $_dependency_map = array(); |
|
80 | - |
|
81 | - /** |
|
82 | - * @type array $_class_loaders |
|
83 | - */ |
|
84 | - protected $_class_loaders = array(); |
|
85 | - |
|
86 | - /** |
|
87 | - * @type array $_aliases |
|
88 | - */ |
|
89 | - protected $_aliases = array(); |
|
90 | - |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * EE_Dependency_Map constructor. |
|
95 | - * |
|
96 | - * @param EE_Request $request |
|
97 | - * @param EE_Response $response |
|
98 | - */ |
|
99 | - protected function __construct(EE_Request $request, EE_Response $response) |
|
100 | - { |
|
101 | - $this->_request = $request; |
|
102 | - $this->_response = $response; |
|
103 | - add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
104 | - do_action('EE_Dependency_Map____construct'); |
|
105 | - } |
|
106 | - |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * @throws InvalidDataTypeException |
|
111 | - * @throws InvalidInterfaceException |
|
112 | - * @throws InvalidArgumentException |
|
113 | - */ |
|
114 | - public function initialize() |
|
115 | - { |
|
116 | - $this->_register_core_dependencies(); |
|
117 | - $this->_register_core_class_loaders(); |
|
118 | - $this->_register_core_aliases(); |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * @singleton method used to instantiate class object |
|
125 | - * @access public |
|
126 | - * @param EE_Request $request |
|
127 | - * @param EE_Response $response |
|
128 | - * @return EE_Dependency_Map |
|
129 | - */ |
|
130 | - public static function instance(EE_Request $request = null, EE_Response $response = null) |
|
131 | - { |
|
132 | - // check if class object is instantiated, and instantiated properly |
|
133 | - if (! self::$_instance instanceof EE_Dependency_Map) { |
|
134 | - self::$_instance = new EE_Dependency_Map($request, $response); |
|
135 | - } |
|
136 | - return self::$_instance; |
|
137 | - } |
|
138 | - |
|
139 | - |
|
140 | - |
|
141 | - /** |
|
142 | - * @param LoaderInterface $loader |
|
143 | - */ |
|
144 | - public function setLoader(LoaderInterface $loader) |
|
145 | - { |
|
146 | - $this->loader = $loader; |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * @param string $class |
|
153 | - * @param array $dependencies |
|
154 | - * @param int $overwrite |
|
155 | - * @return bool |
|
156 | - */ |
|
157 | - public static function register_dependencies( |
|
158 | - $class, |
|
159 | - array $dependencies, |
|
160 | - $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
161 | - ) { |
|
162 | - return self::$_instance->registerDependencies($class, $dependencies, $overwrite); |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * Assigns an array of class names and corresponding load sources (new or cached) |
|
169 | - * to the class specified by the first parameter. |
|
170 | - * IMPORTANT !!! |
|
171 | - * The order of elements in the incoming $dependencies array MUST match |
|
172 | - * the order of the constructor parameters for the class in question. |
|
173 | - * This is especially important when overriding any existing dependencies that are registered. |
|
174 | - * the third parameter controls whether any duplicate dependencies are overwritten or not. |
|
175 | - * |
|
176 | - * @param string $class |
|
177 | - * @param array $dependencies |
|
178 | - * @param int $overwrite |
|
179 | - * @return bool |
|
180 | - */ |
|
181 | - public function registerDependencies( |
|
182 | - $class, |
|
183 | - array $dependencies, |
|
184 | - $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
185 | - ) { |
|
186 | - $class = trim($class, '\\'); |
|
187 | - $registered = false; |
|
188 | - if (empty(self::$_instance->_dependency_map[ $class ])) { |
|
189 | - self::$_instance->_dependency_map[ $class ] = array(); |
|
190 | - } |
|
191 | - // we need to make sure that any aliases used when registering a dependency |
|
192 | - // get resolved to the correct class name |
|
193 | - foreach ((array)$dependencies as $dependency => $load_source) { |
|
194 | - $alias = self::$_instance->get_alias($dependency); |
|
195 | - if ( |
|
196 | - $overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES |
|
197 | - || ! isset(self::$_instance->_dependency_map[ $class ][ $alias ]) |
|
198 | - ) { |
|
199 | - unset($dependencies[$dependency]); |
|
200 | - $dependencies[$alias] = $load_source; |
|
201 | - $registered = true; |
|
202 | - } |
|
203 | - } |
|
204 | - // now add our two lists of dependencies together. |
|
205 | - // using Union (+=) favours the arrays in precedence from left to right, |
|
206 | - // so $dependencies is NOT overwritten because it is listed first |
|
207 | - // ie: with A = B + C, entries in B take precedence over duplicate entries in C |
|
208 | - // Union is way faster than array_merge() but should be used with caution... |
|
209 | - // especially with numerically indexed arrays |
|
210 | - $dependencies += self::$_instance->_dependency_map[ $class ]; |
|
211 | - // now we need to ensure that the resulting dependencies |
|
212 | - // array only has the entries that are required for the class |
|
213 | - // so first count how many dependencies were originally registered for the class |
|
214 | - $dependency_count = count(self::$_instance->_dependency_map[ $class ]); |
|
215 | - // if that count is non-zero (meaning dependencies were already registered) |
|
216 | - self::$_instance->_dependency_map[ $class ] = $dependency_count |
|
217 | - // then truncate the final array to match that count |
|
218 | - ? array_slice($dependencies, 0, $dependency_count) |
|
219 | - // otherwise just take the incoming array because nothing previously existed |
|
220 | - : $dependencies; |
|
221 | - return $registered; |
|
222 | - } |
|
223 | - |
|
224 | - |
|
225 | - |
|
226 | - /** |
|
227 | - * @param string $class_name |
|
228 | - * @param string $loader |
|
229 | - * @return bool |
|
230 | - * @throws DomainException |
|
231 | - */ |
|
232 | - public static function register_class_loader($class_name, $loader = 'load_core') |
|
233 | - { |
|
234 | - if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
235 | - throw new DomainException( |
|
236 | - esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso') |
|
237 | - ); |
|
238 | - } |
|
239 | - // check that loader is callable or method starts with "load_" and exists in EE_Registry |
|
240 | - if ( |
|
241 | - ! is_callable($loader) |
|
242 | - && ( |
|
243 | - strpos($loader, 'load_') !== 0 |
|
244 | - || ! method_exists('EE_Registry', $loader) |
|
245 | - ) |
|
246 | - ) { |
|
247 | - throw new DomainException( |
|
248 | - sprintf( |
|
249 | - esc_html__( |
|
250 | - '"%1$s" is not a valid loader method on EE_Registry.', |
|
251 | - 'event_espresso' |
|
252 | - ), |
|
253 | - $loader |
|
254 | - ) |
|
255 | - ); |
|
256 | - } |
|
257 | - $class_name = self::$_instance->get_alias($class_name); |
|
258 | - if (! isset(self::$_instance->_class_loaders[$class_name])) { |
|
259 | - self::$_instance->_class_loaders[$class_name] = $loader; |
|
260 | - return true; |
|
261 | - } |
|
262 | - return false; |
|
263 | - } |
|
264 | - |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * @return array |
|
269 | - */ |
|
270 | - public function dependency_map() |
|
271 | - { |
|
272 | - return $this->_dependency_map; |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - |
|
277 | - /** |
|
278 | - * returns TRUE if dependency map contains a listing for the provided class name |
|
279 | - * |
|
280 | - * @param string $class_name |
|
281 | - * @return boolean |
|
282 | - */ |
|
283 | - public function has($class_name = '') |
|
284 | - { |
|
285 | - // all legacy models have the same dependencies |
|
286 | - if (strpos($class_name, 'EEM_') === 0) { |
|
287 | - $class_name = 'LEGACY_MODELS'; |
|
288 | - } |
|
289 | - return isset($this->_dependency_map[$class_name]) ? true : false; |
|
290 | - } |
|
291 | - |
|
292 | - |
|
293 | - |
|
294 | - /** |
|
295 | - * returns TRUE if dependency map contains a listing for the provided class name AND dependency |
|
296 | - * |
|
297 | - * @param string $class_name |
|
298 | - * @param string $dependency |
|
299 | - * @return bool |
|
300 | - */ |
|
301 | - public function has_dependency_for_class($class_name = '', $dependency = '') |
|
302 | - { |
|
303 | - // all legacy models have the same dependencies |
|
304 | - if (strpos($class_name, 'EEM_') === 0) { |
|
305 | - $class_name = 'LEGACY_MODELS'; |
|
306 | - } |
|
307 | - $dependency = $this->get_alias($dependency); |
|
308 | - return isset($this->_dependency_map[$class_name], $this->_dependency_map[$class_name][$dependency]) |
|
309 | - ? true |
|
310 | - : false; |
|
311 | - } |
|
312 | - |
|
313 | - |
|
314 | - |
|
315 | - /** |
|
316 | - * returns loading strategy for whether a previously cached dependency should be loaded or a new instance returned |
|
317 | - * |
|
318 | - * @param string $class_name |
|
319 | - * @param string $dependency |
|
320 | - * @return int |
|
321 | - */ |
|
322 | - public function loading_strategy_for_class_dependency($class_name = '', $dependency = '') |
|
323 | - { |
|
324 | - // all legacy models have the same dependencies |
|
325 | - if (strpos($class_name, 'EEM_') === 0) { |
|
326 | - $class_name = 'LEGACY_MODELS'; |
|
327 | - } |
|
328 | - $dependency = $this->get_alias($dependency); |
|
329 | - return $this->has_dependency_for_class($class_name, $dependency) |
|
330 | - ? $this->_dependency_map[$class_name][$dependency] |
|
331 | - : EE_Dependency_Map::not_registered; |
|
332 | - } |
|
333 | - |
|
334 | - |
|
335 | - |
|
336 | - /** |
|
337 | - * @param string $class_name |
|
338 | - * @return string | Closure |
|
339 | - */ |
|
340 | - public function class_loader($class_name) |
|
341 | - { |
|
342 | - // all legacy models use load_model() |
|
343 | - if(strpos($class_name, 'EEM_') === 0){ |
|
344 | - return 'load_model'; |
|
345 | - } |
|
346 | - $class_name = $this->get_alias($class_name); |
|
347 | - return isset($this->_class_loaders[$class_name]) ? $this->_class_loaders[$class_name] : ''; |
|
348 | - } |
|
349 | - |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * @return array |
|
354 | - */ |
|
355 | - public function class_loaders() |
|
356 | - { |
|
357 | - return $this->_class_loaders; |
|
358 | - } |
|
359 | - |
|
360 | - |
|
361 | - |
|
362 | - /** |
|
363 | - * adds an alias for a classname |
|
364 | - * |
|
365 | - * @param string $class_name the class name that should be used (concrete class to replace interface) |
|
366 | - * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
367 | - * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
368 | - */ |
|
369 | - public function add_alias($class_name, $alias, $for_class = '') |
|
370 | - { |
|
371 | - if ($for_class !== '') { |
|
372 | - if (! isset($this->_aliases[$for_class])) { |
|
373 | - $this->_aliases[$for_class] = array(); |
|
374 | - } |
|
375 | - $this->_aliases[$for_class][$class_name] = $alias; |
|
376 | - } |
|
377 | - $this->_aliases[$class_name] = $alias; |
|
378 | - } |
|
379 | - |
|
380 | - |
|
381 | - |
|
382 | - /** |
|
383 | - * returns TRUE if the provided class name has an alias |
|
384 | - * |
|
385 | - * @param string $class_name |
|
386 | - * @param string $for_class |
|
387 | - * @return bool |
|
388 | - */ |
|
389 | - public function has_alias($class_name = '', $for_class = '') |
|
390 | - { |
|
391 | - return isset($this->_aliases[$for_class], $this->_aliases[$for_class][$class_name]) |
|
392 | - || ( |
|
393 | - isset($this->_aliases[$class_name]) |
|
394 | - && ! is_array($this->_aliases[$class_name]) |
|
395 | - ); |
|
396 | - } |
|
397 | - |
|
398 | - |
|
399 | - |
|
400 | - /** |
|
401 | - * returns alias for class name if one exists, otherwise returns the original classname |
|
402 | - * functions recursively, so that multiple aliases can be used to drill down to a classname |
|
403 | - * for example: |
|
404 | - * if the following two entries were added to the _aliases array: |
|
405 | - * array( |
|
406 | - * 'interface_alias' => 'some\namespace\interface' |
|
407 | - * 'some\namespace\interface' => 'some\namespace\classname' |
|
408 | - * ) |
|
409 | - * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
410 | - * to load an instance of 'some\namespace\classname' |
|
411 | - * |
|
412 | - * @param string $class_name |
|
413 | - * @param string $for_class |
|
414 | - * @return string |
|
415 | - */ |
|
416 | - public function get_alias($class_name = '', $for_class = '') |
|
417 | - { |
|
418 | - if (! $this->has_alias($class_name, $for_class)) { |
|
419 | - return $class_name; |
|
420 | - } |
|
421 | - if ($for_class !== '' && isset($this->_aliases[ $for_class ][ $class_name ])) { |
|
422 | - return $this->get_alias($this->_aliases[$for_class][$class_name], $for_class); |
|
423 | - } |
|
424 | - return $this->get_alias($this->_aliases[$class_name]); |
|
425 | - } |
|
426 | - |
|
427 | - |
|
428 | - |
|
429 | - /** |
|
430 | - * Registers the core dependencies and whether a previously instantiated object should be loaded from the cache, |
|
431 | - * if one exists, or whether a new object should be generated every time the requested class is loaded. |
|
432 | - * This is done by using the following class constants: |
|
433 | - * EE_Dependency_Map::load_from_cache - loads previously instantiated object |
|
434 | - * EE_Dependency_Map::load_new_object - generates a new object every time |
|
435 | - */ |
|
436 | - protected function _register_core_dependencies() |
|
437 | - { |
|
438 | - $this->_dependency_map = array( |
|
439 | - 'EE_Request_Handler' => array( |
|
440 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
441 | - ), |
|
442 | - 'EE_System' => array( |
|
443 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
444 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
445 | - 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
446 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
447 | - 'EE_Maintenance_Mode' => EE_Dependency_Map::load_from_cache, |
|
448 | - ), |
|
449 | - 'EE_Session' => array( |
|
450 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
451 | - 'EE_Encryption' => EE_Dependency_Map::load_from_cache, |
|
452 | - ), |
|
453 | - 'EE_Cart' => array( |
|
454 | - 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
455 | - ), |
|
456 | - 'EE_Front_Controller' => array( |
|
457 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
458 | - 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
459 | - 'EE_Module_Request_Router' => EE_Dependency_Map::load_from_cache, |
|
460 | - ), |
|
461 | - 'EE_Messenger_Collection_Loader' => array( |
|
462 | - 'EE_Messenger_Collection' => EE_Dependency_Map::load_new_object, |
|
463 | - ), |
|
464 | - 'EE_Message_Type_Collection_Loader' => array( |
|
465 | - 'EE_Message_Type_Collection' => EE_Dependency_Map::load_new_object, |
|
466 | - ), |
|
467 | - 'EE_Message_Resource_Manager' => array( |
|
468 | - 'EE_Messenger_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
469 | - 'EE_Message_Type_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
470 | - 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
471 | - ), |
|
472 | - 'EE_Message_Factory' => array( |
|
473 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
474 | - ), |
|
475 | - 'EE_messages' => array( |
|
476 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
477 | - ), |
|
478 | - 'EE_Messages_Generator' => array( |
|
479 | - 'EE_Messages_Queue' => EE_Dependency_Map::load_new_object, |
|
480 | - 'EE_Messages_Data_Handler_Collection' => EE_Dependency_Map::load_new_object, |
|
481 | - 'EE_Message_Template_Group_Collection' => EE_Dependency_Map::load_new_object, |
|
482 | - 'EEH_Parse_Shortcodes' => EE_Dependency_Map::load_from_cache, |
|
483 | - ), |
|
484 | - 'EE_Messages_Processor' => array( |
|
485 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
486 | - ), |
|
487 | - 'EE_Messages_Queue' => array( |
|
488 | - 'EE_Message_Repository' => EE_Dependency_Map::load_new_object, |
|
489 | - ), |
|
490 | - 'EE_Messages_Template_Defaults' => array( |
|
491 | - 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
492 | - 'EEM_Message_Template' => EE_Dependency_Map::load_from_cache, |
|
493 | - ), |
|
494 | - 'EE_Message_To_Generate_From_Request' => array( |
|
495 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
496 | - 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
497 | - ), |
|
498 | - 'EventEspresso\core\services\commands\CommandBus' => array( |
|
499 | - 'EventEspresso\core\services\commands\CommandHandlerManager' => EE_Dependency_Map::load_from_cache, |
|
500 | - ), |
|
501 | - 'EventEspresso\services\commands\CommandHandler' => array( |
|
502 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
503 | - 'CommandBusInterface' => EE_Dependency_Map::load_from_cache, |
|
504 | - ), |
|
505 | - 'EventEspresso\core\services\commands\CommandHandlerManager' => array( |
|
506 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
507 | - ), |
|
508 | - 'EventEspresso\core\services\commands\CompositeCommandHandler' => array( |
|
509 | - 'EventEspresso\core\services\commands\CommandBus' => EE_Dependency_Map::load_from_cache, |
|
510 | - 'EventEspresso\core\services\commands\CommandFactory' => EE_Dependency_Map::load_from_cache, |
|
511 | - ), |
|
512 | - 'EventEspresso\core\services\commands\CommandFactory' => array( |
|
513 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
514 | - ), |
|
515 | - 'EventEspresso\core\services\commands\middleware\CapChecker' => array( |
|
516 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
517 | - ), |
|
518 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => array( |
|
519 | - 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
520 | - ), |
|
521 | - 'EventEspresso\core\domain\services\capabilities\RegistrationsCapChecker' => array( |
|
522 | - 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
523 | - ), |
|
524 | - 'EventEspresso\core\services\commands\registration\CreateRegistrationCommandHandler' => array( |
|
525 | - 'EventEspresso\core\domain\services\registration\CreateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
526 | - ), |
|
527 | - 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommandHandler' => array( |
|
528 | - 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
529 | - ), |
|
530 | - 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommandHandler' => array( |
|
531 | - 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
532 | - ), |
|
533 | - 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler' => array( |
|
534 | - 'EventEspresso\core\domain\services\registration\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
535 | - ), |
|
536 | - 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler' => array( |
|
537 | - 'EventEspresso\core\domain\services\registration\UpdateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
538 | - ), |
|
539 | - 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler' => array( |
|
540 | - 'EventEspresso\core\domain\services\ticket\CreateTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
541 | - ), |
|
542 | - 'EventEspresso\core\services\commands\ticket\CancelTicketLineItemCommandHandler' => array( |
|
543 | - 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
544 | - ), |
|
545 | - 'EventEspresso\core\domain\services\registration\CancelRegistrationService' => array( |
|
546 | - 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
547 | - ), |
|
548 | - 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler' => array( |
|
549 | - 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
550 | - ), |
|
551 | - 'EventEspresso\core\services\database\TableManager' => array( |
|
552 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
553 | - ), |
|
554 | - 'EE_Data_Migration_Class_Base' => array( |
|
555 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
556 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
557 | - ), |
|
558 | - 'EE_DMS_Core_4_1_0' => array( |
|
559 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
560 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
561 | - ), |
|
562 | - 'EE_DMS_Core_4_2_0' => array( |
|
563 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
564 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
565 | - ), |
|
566 | - 'EE_DMS_Core_4_3_0' => array( |
|
567 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
568 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
569 | - ), |
|
570 | - 'EE_DMS_Core_4_4_0' => array( |
|
571 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
572 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
573 | - ), |
|
574 | - 'EE_DMS_Core_4_5_0' => array( |
|
575 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
576 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
577 | - ), |
|
578 | - 'EE_DMS_Core_4_6_0' => array( |
|
579 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
580 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
581 | - ), |
|
582 | - 'EE_DMS_Core_4_7_0' => array( |
|
583 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
584 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
585 | - ), |
|
586 | - 'EE_DMS_Core_4_8_0' => array( |
|
587 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
588 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
589 | - ), |
|
590 | - 'EE_DMS_Core_4_9_0' => array( |
|
591 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
592 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
593 | - ), |
|
594 | - 'EventEspresso\core\services\assets\Registry' => array( |
|
595 | - 'EE_Template_Config' => EE_Dependency_Map::load_from_cache, |
|
596 | - 'EE_Currency_Config' => EE_Dependency_Map::load_from_cache, |
|
597 | - ), |
|
598 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoCancelled' => array( |
|
599 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
600 | - ), |
|
601 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoCheckout' => array( |
|
602 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
603 | - ), |
|
604 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoEventAttendees' => array( |
|
605 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
606 | - ), |
|
607 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoEvents' => array( |
|
608 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
609 | - ), |
|
610 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoThankYou' => array( |
|
611 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
612 | - ), |
|
613 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoTicketSelector' => array( |
|
614 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
615 | - ), |
|
616 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoTxnPage' => array( |
|
617 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
618 | - ), |
|
619 | - 'EventEspresso\core\services\cache\BasicCacheManager' => array( |
|
620 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
621 | - ), |
|
622 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => array( |
|
623 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
624 | - ), |
|
625 | - 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => array( |
|
626 | - 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
627 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
628 | - ), |
|
629 | - 'EventEspresso\core\domain\values\EmailAddress' => array( |
|
630 | - null, |
|
631 | - 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => EE_Dependency_Map::load_from_cache, |
|
632 | - ), |
|
633 | - 'EventEspresso\core\services\orm\ModelFieldFactory' => array( |
|
634 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
635 | - ), |
|
636 | - 'LEGACY_MODELS' => array( |
|
637 | - null, |
|
638 | - 'EventEspresso\core\services\database\ModelFieldFactory' => EE_Dependency_Map::load_from_cache, |
|
639 | - ), |
|
640 | - 'EE_Module_Request_Router' => array( |
|
641 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
642 | - ), |
|
643 | - 'EE_Registration_Processor' => array( |
|
644 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
645 | - ), |
|
646 | - 'EventEspresso\core\services\currency\CurrencyFactory' => array( |
|
647 | - 'EEM_Country' => EE_Dependency_Map::load_from_cache, |
|
648 | - 'EE_Organization_Config' => EE_Dependency_Map::load_from_cache, |
|
649 | - ), |
|
650 | - 'EventEspresso\core\services\currency\MoneyFactory' => array( |
|
651 | - 'EventEspresso\core\services\currency\CurrencyFactory' => EE_Dependency_Map::load_from_cache, |
|
652 | - 'EventEspresso\core\services\currency\Calculator' => EE_Dependency_Map::load_from_cache, |
|
653 | - array(), |
|
654 | - ), |
|
655 | - ); |
|
656 | - } |
|
657 | - |
|
658 | - |
|
659 | - |
|
660 | - /** |
|
661 | - * Registers how core classes are loaded. |
|
662 | - * This can either be done by simply providing the name of one of the EE_Registry loader methods such as: |
|
663 | - * 'EE_Request_Handler' => 'load_core' |
|
664 | - * 'EE_Messages_Queue' => 'load_lib' |
|
665 | - * 'EEH_Debug_Tools' => 'load_helper' |
|
666 | - * or, if greater control is required, by providing a custom closure. For example: |
|
667 | - * 'Some_Class' => function () { |
|
668 | - * return new Some_Class(); |
|
669 | - * }, |
|
670 | - * This is required for instantiating dependencies |
|
671 | - * where an interface has been type hinted in a class constructor. For example: |
|
672 | - * 'Required_Interface' => function () { |
|
673 | - * return new A_Class_That_Implements_Required_Interface(); |
|
674 | - * }, |
|
675 | - * |
|
676 | - * @throws InvalidInterfaceException |
|
677 | - * @throws InvalidDataTypeException |
|
678 | - * @throws InvalidArgumentException |
|
679 | - */ |
|
680 | - protected function _register_core_class_loaders() |
|
681 | - { |
|
682 | - //for PHP5.3 compat, we need to register any properties called here in a variable because `$this` cannot |
|
683 | - //be used in a closure. |
|
684 | - $request = &$this->_request; |
|
685 | - $response = &$this->_response; |
|
686 | - // $loader = &$this->loader; |
|
687 | - $this->_class_loaders = array( |
|
688 | - //load_core |
|
689 | - 'EE_Capabilities' => 'load_core', |
|
690 | - 'EE_Encryption' => 'load_core', |
|
691 | - 'EE_Front_Controller' => 'load_core', |
|
692 | - 'EE_Module_Request_Router' => 'load_core', |
|
693 | - 'EE_Registry' => 'load_core', |
|
694 | - 'EE_Request' => function () use (&$request) { |
|
695 | - return $request; |
|
696 | - }, |
|
697 | - 'EE_Response' => function () use (&$response) { |
|
698 | - return $response; |
|
699 | - }, |
|
700 | - 'EE_Request_Handler' => 'load_core', |
|
701 | - 'EE_Session' => 'load_core', |
|
702 | - 'EE_Cron_Tasks' => 'load_core', |
|
703 | - 'EE_System' => 'load_core', |
|
704 | - 'EE_Maintenance_Mode' => 'load_core', |
|
705 | - 'EE_Register_CPTs' => 'load_core', |
|
706 | - 'EE_Admin' => 'load_core', |
|
707 | - //load_lib |
|
708 | - 'EE_Message_Resource_Manager' => 'load_lib', |
|
709 | - 'EE_Message_Type_Collection' => 'load_lib', |
|
710 | - 'EE_Message_Type_Collection_Loader' => 'load_lib', |
|
711 | - 'EE_Messenger_Collection' => 'load_lib', |
|
712 | - 'EE_Messenger_Collection_Loader' => 'load_lib', |
|
713 | - 'EE_Messages_Processor' => 'load_lib', |
|
714 | - 'EE_Message_Repository' => 'load_lib', |
|
715 | - 'EE_Messages_Queue' => 'load_lib', |
|
716 | - 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
|
717 | - 'EE_Message_Template_Group_Collection' => 'load_lib', |
|
718 | - 'EE_Payment_Method_Manager' => 'load_lib', |
|
719 | - 'EE_Messages_Generator' => function () { |
|
720 | - return EE_Registry::instance()->load_lib( |
|
721 | - 'Messages_Generator', |
|
722 | - array(), |
|
723 | - false, |
|
724 | - false |
|
725 | - ); |
|
726 | - }, |
|
727 | - 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
728 | - return EE_Registry::instance()->load_lib( |
|
729 | - 'Messages_Template_Defaults', |
|
730 | - $arguments, |
|
731 | - false, |
|
732 | - false |
|
733 | - ); |
|
734 | - }, |
|
735 | - //load_model |
|
736 | - // 'EEM_Attendee' => 'load_model', |
|
737 | - // 'EEM_Message_Template_Group' => 'load_model', |
|
738 | - // 'EEM_Message_Template' => 'load_model', |
|
739 | - //load_helper |
|
740 | - 'EEH_Parse_Shortcodes' => function () { |
|
741 | - if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
|
742 | - return new EEH_Parse_Shortcodes(); |
|
743 | - } |
|
744 | - return null; |
|
745 | - }, |
|
746 | - 'EE_Template_Config' => function () { |
|
747 | - return EE_Config::instance()->template_settings; |
|
748 | - }, |
|
749 | - 'EE_Currency_Config' => function () { |
|
750 | - return EE_Config::instance()->currency; |
|
751 | - }, |
|
752 | - 'EE_Registration_Config' => function () { |
|
753 | - return EE_Config::instance()->registration; |
|
754 | - }, |
|
755 | - 'EE_Organization_Config' => function () { |
|
756 | - return EE_Config::instance()->organization; |
|
757 | - }, |
|
758 | - 'EventEspresso\core\services\loaders\Loader' => function () { |
|
759 | - return LoaderFactory::getLoader(); |
|
760 | - }, |
|
761 | - ); |
|
762 | - } |
|
763 | - |
|
764 | - |
|
765 | - |
|
766 | - /** |
|
767 | - * can be used for supplying alternate names for classes, |
|
768 | - * or for connecting interface names to instantiable classes |
|
769 | - */ |
|
770 | - protected function _register_core_aliases() |
|
771 | - { |
|
772 | - $this->_aliases = array( |
|
773 | - 'CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBusInterface', |
|
774 | - 'EventEspresso\core\services\commands\CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBus', |
|
775 | - 'CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
776 | - 'EventEspresso\core\services\commands\CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManager', |
|
777 | - 'CapChecker' => 'EventEspresso\core\services\commands\middleware\CapChecker', |
|
778 | - 'AddActionHook' => 'EventEspresso\core\services\commands\middleware\AddActionHook', |
|
779 | - 'CapabilitiesChecker' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
780 | - 'CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface', |
|
781 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
782 | - 'CreateRegistrationService' => 'EventEspresso\core\domain\services\registration\CreateRegistrationService', |
|
783 | - 'CreateRegCodeCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegCodeCommand', |
|
784 | - 'CreateRegUrlLinkCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegUrlLinkCommand', |
|
785 | - 'CreateRegistrationCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegistrationCommand', |
|
786 | - 'CopyRegistrationDetailsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommand', |
|
787 | - 'CopyRegistrationPaymentsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommand', |
|
788 | - 'CancelRegistrationAndTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler', |
|
789 | - 'UpdateRegistrationAndTransactionAfterChangeCommandHandler' => 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler', |
|
790 | - 'CreateTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommand', |
|
791 | - 'CreateTransactionCommandHandler' => 'EventEspresso\core\services\commands\transaction\CreateTransactionCommandHandler', |
|
792 | - 'CreateAttendeeCommandHandler' => 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler', |
|
793 | - 'TableManager' => 'EventEspresso\core\services\database\TableManager', |
|
794 | - 'TableAnalysis' => 'EventEspresso\core\services\database\TableAnalysis', |
|
795 | - 'EspressoShortcode' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
796 | - 'ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
797 | - 'EventEspresso\core\services\shortcodes\ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
798 | - 'EventEspresso\core\services\cache\CacheStorageInterface' => 'EventEspresso\core\services\cache\TransientCacheStorage', |
|
799 | - 'LoaderInterface' => 'EventEspresso\core\services\loaders\LoaderInterface', |
|
800 | - 'EventEspresso\core\services\loaders\LoaderInterface' => 'EventEspresso\core\services\loaders\Loader', |
|
801 | - 'CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactoryInterface', |
|
802 | - 'EventEspresso\core\services\commands\CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactory', |
|
803 | - 'EventEspresso\core\domain\services\session\SessionIdentifierInterface' => 'EE_Session', |
|
804 | - 'EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface', |
|
805 | - 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidationService', |
|
806 | - 'NoticeConverterInterface' => 'EventEspresso\core\services\notices\NoticeConverterInterface', |
|
807 | - 'EventEspresso\core\services\notices\NoticeConverterInterface' => 'EventEspresso\core\services\notices\ConvertNoticesToEeErrors', |
|
808 | - 'NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainerInterface', |
|
809 | - 'EventEspresso\core\services\notices\NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainer', |
|
810 | - 'EventEspresso\core\services\currency\Calculator' => 'EventEspresso\core\services\currency\DefaultCalculator', |
|
811 | - ); |
|
812 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) { |
|
813 | - $this->_aliases['EventEspresso\core\services\notices\NoticeConverterInterface'] = 'EventEspresso\core\services\notices\ConvertNoticesToAdminNotices'; |
|
814 | - } |
|
815 | - } |
|
816 | - |
|
817 | - |
|
818 | - |
|
819 | - /** |
|
820 | - * This is used to reset the internal map and class_loaders to their original default state at the beginning of the |
|
821 | - * request Primarily used by unit tests. |
|
822 | - * |
|
823 | - * @throws InvalidDataTypeException |
|
824 | - * @throws InvalidInterfaceException |
|
825 | - * @throws InvalidArgumentException |
|
826 | - */ |
|
827 | - public function reset() |
|
828 | - { |
|
829 | - $this->_register_core_class_loaders(); |
|
830 | - $this->_register_core_dependencies(); |
|
831 | - } |
|
25 | + /** |
|
26 | + * This means that the requested class dependency is not present in the dependency map |
|
27 | + */ |
|
28 | + const not_registered = 0; |
|
29 | + |
|
30 | + /** |
|
31 | + * This instructs class loaders to ALWAYS return a newly instantiated object for the requested class. |
|
32 | + */ |
|
33 | + const load_new_object = 1; |
|
34 | + |
|
35 | + /** |
|
36 | + * This instructs class loaders to return a previously instantiated and cached object for the requested class. |
|
37 | + * IF a previously instantiated object does not exist, a new one will be created and added to the cache. |
|
38 | + */ |
|
39 | + const load_from_cache = 2; |
|
40 | + |
|
41 | + /** |
|
42 | + * When registering a dependency, |
|
43 | + * this indicates to keep any existing dependencies that already exist, |
|
44 | + * and simply discard any new dependencies declared in the incoming data |
|
45 | + */ |
|
46 | + const KEEP_EXISTING_DEPENDENCIES = 0; |
|
47 | + |
|
48 | + /** |
|
49 | + * When registering a dependency, |
|
50 | + * this indicates to overwrite any existing dependencies that already exist using the incoming data |
|
51 | + */ |
|
52 | + const OVERWRITE_DEPENDENCIES = 1; |
|
53 | + |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * @type EE_Dependency_Map $_instance |
|
58 | + */ |
|
59 | + protected static $_instance; |
|
60 | + |
|
61 | + /** |
|
62 | + * @type EE_Request $request |
|
63 | + */ |
|
64 | + protected $_request; |
|
65 | + |
|
66 | + /** |
|
67 | + * @type EE_Response $response |
|
68 | + */ |
|
69 | + protected $_response; |
|
70 | + |
|
71 | + /** |
|
72 | + * @type LoaderInterface $loader |
|
73 | + */ |
|
74 | + protected $loader; |
|
75 | + |
|
76 | + /** |
|
77 | + * @type array $_dependency_map |
|
78 | + */ |
|
79 | + protected $_dependency_map = array(); |
|
80 | + |
|
81 | + /** |
|
82 | + * @type array $_class_loaders |
|
83 | + */ |
|
84 | + protected $_class_loaders = array(); |
|
85 | + |
|
86 | + /** |
|
87 | + * @type array $_aliases |
|
88 | + */ |
|
89 | + protected $_aliases = array(); |
|
90 | + |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * EE_Dependency_Map constructor. |
|
95 | + * |
|
96 | + * @param EE_Request $request |
|
97 | + * @param EE_Response $response |
|
98 | + */ |
|
99 | + protected function __construct(EE_Request $request, EE_Response $response) |
|
100 | + { |
|
101 | + $this->_request = $request; |
|
102 | + $this->_response = $response; |
|
103 | + add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
104 | + do_action('EE_Dependency_Map____construct'); |
|
105 | + } |
|
106 | + |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * @throws InvalidDataTypeException |
|
111 | + * @throws InvalidInterfaceException |
|
112 | + * @throws InvalidArgumentException |
|
113 | + */ |
|
114 | + public function initialize() |
|
115 | + { |
|
116 | + $this->_register_core_dependencies(); |
|
117 | + $this->_register_core_class_loaders(); |
|
118 | + $this->_register_core_aliases(); |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * @singleton method used to instantiate class object |
|
125 | + * @access public |
|
126 | + * @param EE_Request $request |
|
127 | + * @param EE_Response $response |
|
128 | + * @return EE_Dependency_Map |
|
129 | + */ |
|
130 | + public static function instance(EE_Request $request = null, EE_Response $response = null) |
|
131 | + { |
|
132 | + // check if class object is instantiated, and instantiated properly |
|
133 | + if (! self::$_instance instanceof EE_Dependency_Map) { |
|
134 | + self::$_instance = new EE_Dependency_Map($request, $response); |
|
135 | + } |
|
136 | + return self::$_instance; |
|
137 | + } |
|
138 | + |
|
139 | + |
|
140 | + |
|
141 | + /** |
|
142 | + * @param LoaderInterface $loader |
|
143 | + */ |
|
144 | + public function setLoader(LoaderInterface $loader) |
|
145 | + { |
|
146 | + $this->loader = $loader; |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * @param string $class |
|
153 | + * @param array $dependencies |
|
154 | + * @param int $overwrite |
|
155 | + * @return bool |
|
156 | + */ |
|
157 | + public static function register_dependencies( |
|
158 | + $class, |
|
159 | + array $dependencies, |
|
160 | + $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
161 | + ) { |
|
162 | + return self::$_instance->registerDependencies($class, $dependencies, $overwrite); |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * Assigns an array of class names and corresponding load sources (new or cached) |
|
169 | + * to the class specified by the first parameter. |
|
170 | + * IMPORTANT !!! |
|
171 | + * The order of elements in the incoming $dependencies array MUST match |
|
172 | + * the order of the constructor parameters for the class in question. |
|
173 | + * This is especially important when overriding any existing dependencies that are registered. |
|
174 | + * the third parameter controls whether any duplicate dependencies are overwritten or not. |
|
175 | + * |
|
176 | + * @param string $class |
|
177 | + * @param array $dependencies |
|
178 | + * @param int $overwrite |
|
179 | + * @return bool |
|
180 | + */ |
|
181 | + public function registerDependencies( |
|
182 | + $class, |
|
183 | + array $dependencies, |
|
184 | + $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
185 | + ) { |
|
186 | + $class = trim($class, '\\'); |
|
187 | + $registered = false; |
|
188 | + if (empty(self::$_instance->_dependency_map[ $class ])) { |
|
189 | + self::$_instance->_dependency_map[ $class ] = array(); |
|
190 | + } |
|
191 | + // we need to make sure that any aliases used when registering a dependency |
|
192 | + // get resolved to the correct class name |
|
193 | + foreach ((array)$dependencies as $dependency => $load_source) { |
|
194 | + $alias = self::$_instance->get_alias($dependency); |
|
195 | + if ( |
|
196 | + $overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES |
|
197 | + || ! isset(self::$_instance->_dependency_map[ $class ][ $alias ]) |
|
198 | + ) { |
|
199 | + unset($dependencies[$dependency]); |
|
200 | + $dependencies[$alias] = $load_source; |
|
201 | + $registered = true; |
|
202 | + } |
|
203 | + } |
|
204 | + // now add our two lists of dependencies together. |
|
205 | + // using Union (+=) favours the arrays in precedence from left to right, |
|
206 | + // so $dependencies is NOT overwritten because it is listed first |
|
207 | + // ie: with A = B + C, entries in B take precedence over duplicate entries in C |
|
208 | + // Union is way faster than array_merge() but should be used with caution... |
|
209 | + // especially with numerically indexed arrays |
|
210 | + $dependencies += self::$_instance->_dependency_map[ $class ]; |
|
211 | + // now we need to ensure that the resulting dependencies |
|
212 | + // array only has the entries that are required for the class |
|
213 | + // so first count how many dependencies were originally registered for the class |
|
214 | + $dependency_count = count(self::$_instance->_dependency_map[ $class ]); |
|
215 | + // if that count is non-zero (meaning dependencies were already registered) |
|
216 | + self::$_instance->_dependency_map[ $class ] = $dependency_count |
|
217 | + // then truncate the final array to match that count |
|
218 | + ? array_slice($dependencies, 0, $dependency_count) |
|
219 | + // otherwise just take the incoming array because nothing previously existed |
|
220 | + : $dependencies; |
|
221 | + return $registered; |
|
222 | + } |
|
223 | + |
|
224 | + |
|
225 | + |
|
226 | + /** |
|
227 | + * @param string $class_name |
|
228 | + * @param string $loader |
|
229 | + * @return bool |
|
230 | + * @throws DomainException |
|
231 | + */ |
|
232 | + public static function register_class_loader($class_name, $loader = 'load_core') |
|
233 | + { |
|
234 | + if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
235 | + throw new DomainException( |
|
236 | + esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso') |
|
237 | + ); |
|
238 | + } |
|
239 | + // check that loader is callable or method starts with "load_" and exists in EE_Registry |
|
240 | + if ( |
|
241 | + ! is_callable($loader) |
|
242 | + && ( |
|
243 | + strpos($loader, 'load_') !== 0 |
|
244 | + || ! method_exists('EE_Registry', $loader) |
|
245 | + ) |
|
246 | + ) { |
|
247 | + throw new DomainException( |
|
248 | + sprintf( |
|
249 | + esc_html__( |
|
250 | + '"%1$s" is not a valid loader method on EE_Registry.', |
|
251 | + 'event_espresso' |
|
252 | + ), |
|
253 | + $loader |
|
254 | + ) |
|
255 | + ); |
|
256 | + } |
|
257 | + $class_name = self::$_instance->get_alias($class_name); |
|
258 | + if (! isset(self::$_instance->_class_loaders[$class_name])) { |
|
259 | + self::$_instance->_class_loaders[$class_name] = $loader; |
|
260 | + return true; |
|
261 | + } |
|
262 | + return false; |
|
263 | + } |
|
264 | + |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * @return array |
|
269 | + */ |
|
270 | + public function dependency_map() |
|
271 | + { |
|
272 | + return $this->_dependency_map; |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + |
|
277 | + /** |
|
278 | + * returns TRUE if dependency map contains a listing for the provided class name |
|
279 | + * |
|
280 | + * @param string $class_name |
|
281 | + * @return boolean |
|
282 | + */ |
|
283 | + public function has($class_name = '') |
|
284 | + { |
|
285 | + // all legacy models have the same dependencies |
|
286 | + if (strpos($class_name, 'EEM_') === 0) { |
|
287 | + $class_name = 'LEGACY_MODELS'; |
|
288 | + } |
|
289 | + return isset($this->_dependency_map[$class_name]) ? true : false; |
|
290 | + } |
|
291 | + |
|
292 | + |
|
293 | + |
|
294 | + /** |
|
295 | + * returns TRUE if dependency map contains a listing for the provided class name AND dependency |
|
296 | + * |
|
297 | + * @param string $class_name |
|
298 | + * @param string $dependency |
|
299 | + * @return bool |
|
300 | + */ |
|
301 | + public function has_dependency_for_class($class_name = '', $dependency = '') |
|
302 | + { |
|
303 | + // all legacy models have the same dependencies |
|
304 | + if (strpos($class_name, 'EEM_') === 0) { |
|
305 | + $class_name = 'LEGACY_MODELS'; |
|
306 | + } |
|
307 | + $dependency = $this->get_alias($dependency); |
|
308 | + return isset($this->_dependency_map[$class_name], $this->_dependency_map[$class_name][$dependency]) |
|
309 | + ? true |
|
310 | + : false; |
|
311 | + } |
|
312 | + |
|
313 | + |
|
314 | + |
|
315 | + /** |
|
316 | + * returns loading strategy for whether a previously cached dependency should be loaded or a new instance returned |
|
317 | + * |
|
318 | + * @param string $class_name |
|
319 | + * @param string $dependency |
|
320 | + * @return int |
|
321 | + */ |
|
322 | + public function loading_strategy_for_class_dependency($class_name = '', $dependency = '') |
|
323 | + { |
|
324 | + // all legacy models have the same dependencies |
|
325 | + if (strpos($class_name, 'EEM_') === 0) { |
|
326 | + $class_name = 'LEGACY_MODELS'; |
|
327 | + } |
|
328 | + $dependency = $this->get_alias($dependency); |
|
329 | + return $this->has_dependency_for_class($class_name, $dependency) |
|
330 | + ? $this->_dependency_map[$class_name][$dependency] |
|
331 | + : EE_Dependency_Map::not_registered; |
|
332 | + } |
|
333 | + |
|
334 | + |
|
335 | + |
|
336 | + /** |
|
337 | + * @param string $class_name |
|
338 | + * @return string | Closure |
|
339 | + */ |
|
340 | + public function class_loader($class_name) |
|
341 | + { |
|
342 | + // all legacy models use load_model() |
|
343 | + if(strpos($class_name, 'EEM_') === 0){ |
|
344 | + return 'load_model'; |
|
345 | + } |
|
346 | + $class_name = $this->get_alias($class_name); |
|
347 | + return isset($this->_class_loaders[$class_name]) ? $this->_class_loaders[$class_name] : ''; |
|
348 | + } |
|
349 | + |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * @return array |
|
354 | + */ |
|
355 | + public function class_loaders() |
|
356 | + { |
|
357 | + return $this->_class_loaders; |
|
358 | + } |
|
359 | + |
|
360 | + |
|
361 | + |
|
362 | + /** |
|
363 | + * adds an alias for a classname |
|
364 | + * |
|
365 | + * @param string $class_name the class name that should be used (concrete class to replace interface) |
|
366 | + * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
367 | + * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
368 | + */ |
|
369 | + public function add_alias($class_name, $alias, $for_class = '') |
|
370 | + { |
|
371 | + if ($for_class !== '') { |
|
372 | + if (! isset($this->_aliases[$for_class])) { |
|
373 | + $this->_aliases[$for_class] = array(); |
|
374 | + } |
|
375 | + $this->_aliases[$for_class][$class_name] = $alias; |
|
376 | + } |
|
377 | + $this->_aliases[$class_name] = $alias; |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + |
|
382 | + /** |
|
383 | + * returns TRUE if the provided class name has an alias |
|
384 | + * |
|
385 | + * @param string $class_name |
|
386 | + * @param string $for_class |
|
387 | + * @return bool |
|
388 | + */ |
|
389 | + public function has_alias($class_name = '', $for_class = '') |
|
390 | + { |
|
391 | + return isset($this->_aliases[$for_class], $this->_aliases[$for_class][$class_name]) |
|
392 | + || ( |
|
393 | + isset($this->_aliases[$class_name]) |
|
394 | + && ! is_array($this->_aliases[$class_name]) |
|
395 | + ); |
|
396 | + } |
|
397 | + |
|
398 | + |
|
399 | + |
|
400 | + /** |
|
401 | + * returns alias for class name if one exists, otherwise returns the original classname |
|
402 | + * functions recursively, so that multiple aliases can be used to drill down to a classname |
|
403 | + * for example: |
|
404 | + * if the following two entries were added to the _aliases array: |
|
405 | + * array( |
|
406 | + * 'interface_alias' => 'some\namespace\interface' |
|
407 | + * 'some\namespace\interface' => 'some\namespace\classname' |
|
408 | + * ) |
|
409 | + * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
410 | + * to load an instance of 'some\namespace\classname' |
|
411 | + * |
|
412 | + * @param string $class_name |
|
413 | + * @param string $for_class |
|
414 | + * @return string |
|
415 | + */ |
|
416 | + public function get_alias($class_name = '', $for_class = '') |
|
417 | + { |
|
418 | + if (! $this->has_alias($class_name, $for_class)) { |
|
419 | + return $class_name; |
|
420 | + } |
|
421 | + if ($for_class !== '' && isset($this->_aliases[ $for_class ][ $class_name ])) { |
|
422 | + return $this->get_alias($this->_aliases[$for_class][$class_name], $for_class); |
|
423 | + } |
|
424 | + return $this->get_alias($this->_aliases[$class_name]); |
|
425 | + } |
|
426 | + |
|
427 | + |
|
428 | + |
|
429 | + /** |
|
430 | + * Registers the core dependencies and whether a previously instantiated object should be loaded from the cache, |
|
431 | + * if one exists, or whether a new object should be generated every time the requested class is loaded. |
|
432 | + * This is done by using the following class constants: |
|
433 | + * EE_Dependency_Map::load_from_cache - loads previously instantiated object |
|
434 | + * EE_Dependency_Map::load_new_object - generates a new object every time |
|
435 | + */ |
|
436 | + protected function _register_core_dependencies() |
|
437 | + { |
|
438 | + $this->_dependency_map = array( |
|
439 | + 'EE_Request_Handler' => array( |
|
440 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
441 | + ), |
|
442 | + 'EE_System' => array( |
|
443 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
444 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
445 | + 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
446 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
447 | + 'EE_Maintenance_Mode' => EE_Dependency_Map::load_from_cache, |
|
448 | + ), |
|
449 | + 'EE_Session' => array( |
|
450 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
451 | + 'EE_Encryption' => EE_Dependency_Map::load_from_cache, |
|
452 | + ), |
|
453 | + 'EE_Cart' => array( |
|
454 | + 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
455 | + ), |
|
456 | + 'EE_Front_Controller' => array( |
|
457 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
458 | + 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
459 | + 'EE_Module_Request_Router' => EE_Dependency_Map::load_from_cache, |
|
460 | + ), |
|
461 | + 'EE_Messenger_Collection_Loader' => array( |
|
462 | + 'EE_Messenger_Collection' => EE_Dependency_Map::load_new_object, |
|
463 | + ), |
|
464 | + 'EE_Message_Type_Collection_Loader' => array( |
|
465 | + 'EE_Message_Type_Collection' => EE_Dependency_Map::load_new_object, |
|
466 | + ), |
|
467 | + 'EE_Message_Resource_Manager' => array( |
|
468 | + 'EE_Messenger_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
469 | + 'EE_Message_Type_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
470 | + 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
471 | + ), |
|
472 | + 'EE_Message_Factory' => array( |
|
473 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
474 | + ), |
|
475 | + 'EE_messages' => array( |
|
476 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
477 | + ), |
|
478 | + 'EE_Messages_Generator' => array( |
|
479 | + 'EE_Messages_Queue' => EE_Dependency_Map::load_new_object, |
|
480 | + 'EE_Messages_Data_Handler_Collection' => EE_Dependency_Map::load_new_object, |
|
481 | + 'EE_Message_Template_Group_Collection' => EE_Dependency_Map::load_new_object, |
|
482 | + 'EEH_Parse_Shortcodes' => EE_Dependency_Map::load_from_cache, |
|
483 | + ), |
|
484 | + 'EE_Messages_Processor' => array( |
|
485 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
486 | + ), |
|
487 | + 'EE_Messages_Queue' => array( |
|
488 | + 'EE_Message_Repository' => EE_Dependency_Map::load_new_object, |
|
489 | + ), |
|
490 | + 'EE_Messages_Template_Defaults' => array( |
|
491 | + 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
492 | + 'EEM_Message_Template' => EE_Dependency_Map::load_from_cache, |
|
493 | + ), |
|
494 | + 'EE_Message_To_Generate_From_Request' => array( |
|
495 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
496 | + 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
497 | + ), |
|
498 | + 'EventEspresso\core\services\commands\CommandBus' => array( |
|
499 | + 'EventEspresso\core\services\commands\CommandHandlerManager' => EE_Dependency_Map::load_from_cache, |
|
500 | + ), |
|
501 | + 'EventEspresso\services\commands\CommandHandler' => array( |
|
502 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
503 | + 'CommandBusInterface' => EE_Dependency_Map::load_from_cache, |
|
504 | + ), |
|
505 | + 'EventEspresso\core\services\commands\CommandHandlerManager' => array( |
|
506 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
507 | + ), |
|
508 | + 'EventEspresso\core\services\commands\CompositeCommandHandler' => array( |
|
509 | + 'EventEspresso\core\services\commands\CommandBus' => EE_Dependency_Map::load_from_cache, |
|
510 | + 'EventEspresso\core\services\commands\CommandFactory' => EE_Dependency_Map::load_from_cache, |
|
511 | + ), |
|
512 | + 'EventEspresso\core\services\commands\CommandFactory' => array( |
|
513 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
514 | + ), |
|
515 | + 'EventEspresso\core\services\commands\middleware\CapChecker' => array( |
|
516 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
517 | + ), |
|
518 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => array( |
|
519 | + 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
520 | + ), |
|
521 | + 'EventEspresso\core\domain\services\capabilities\RegistrationsCapChecker' => array( |
|
522 | + 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
523 | + ), |
|
524 | + 'EventEspresso\core\services\commands\registration\CreateRegistrationCommandHandler' => array( |
|
525 | + 'EventEspresso\core\domain\services\registration\CreateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
526 | + ), |
|
527 | + 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommandHandler' => array( |
|
528 | + 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
529 | + ), |
|
530 | + 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommandHandler' => array( |
|
531 | + 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
532 | + ), |
|
533 | + 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler' => array( |
|
534 | + 'EventEspresso\core\domain\services\registration\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
535 | + ), |
|
536 | + 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler' => array( |
|
537 | + 'EventEspresso\core\domain\services\registration\UpdateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
538 | + ), |
|
539 | + 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler' => array( |
|
540 | + 'EventEspresso\core\domain\services\ticket\CreateTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
541 | + ), |
|
542 | + 'EventEspresso\core\services\commands\ticket\CancelTicketLineItemCommandHandler' => array( |
|
543 | + 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
544 | + ), |
|
545 | + 'EventEspresso\core\domain\services\registration\CancelRegistrationService' => array( |
|
546 | + 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
547 | + ), |
|
548 | + 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler' => array( |
|
549 | + 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
550 | + ), |
|
551 | + 'EventEspresso\core\services\database\TableManager' => array( |
|
552 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
553 | + ), |
|
554 | + 'EE_Data_Migration_Class_Base' => array( |
|
555 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
556 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
557 | + ), |
|
558 | + 'EE_DMS_Core_4_1_0' => array( |
|
559 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
560 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
561 | + ), |
|
562 | + 'EE_DMS_Core_4_2_0' => array( |
|
563 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
564 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
565 | + ), |
|
566 | + 'EE_DMS_Core_4_3_0' => array( |
|
567 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
568 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
569 | + ), |
|
570 | + 'EE_DMS_Core_4_4_0' => array( |
|
571 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
572 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
573 | + ), |
|
574 | + 'EE_DMS_Core_4_5_0' => array( |
|
575 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
576 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
577 | + ), |
|
578 | + 'EE_DMS_Core_4_6_0' => array( |
|
579 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
580 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
581 | + ), |
|
582 | + 'EE_DMS_Core_4_7_0' => array( |
|
583 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
584 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
585 | + ), |
|
586 | + 'EE_DMS_Core_4_8_0' => array( |
|
587 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
588 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
589 | + ), |
|
590 | + 'EE_DMS_Core_4_9_0' => array( |
|
591 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
592 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
593 | + ), |
|
594 | + 'EventEspresso\core\services\assets\Registry' => array( |
|
595 | + 'EE_Template_Config' => EE_Dependency_Map::load_from_cache, |
|
596 | + 'EE_Currency_Config' => EE_Dependency_Map::load_from_cache, |
|
597 | + ), |
|
598 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoCancelled' => array( |
|
599 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
600 | + ), |
|
601 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoCheckout' => array( |
|
602 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
603 | + ), |
|
604 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoEventAttendees' => array( |
|
605 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
606 | + ), |
|
607 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoEvents' => array( |
|
608 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
609 | + ), |
|
610 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoThankYou' => array( |
|
611 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
612 | + ), |
|
613 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoTicketSelector' => array( |
|
614 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
615 | + ), |
|
616 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoTxnPage' => array( |
|
617 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
618 | + ), |
|
619 | + 'EventEspresso\core\services\cache\BasicCacheManager' => array( |
|
620 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
621 | + ), |
|
622 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => array( |
|
623 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
624 | + ), |
|
625 | + 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => array( |
|
626 | + 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
627 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
628 | + ), |
|
629 | + 'EventEspresso\core\domain\values\EmailAddress' => array( |
|
630 | + null, |
|
631 | + 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => EE_Dependency_Map::load_from_cache, |
|
632 | + ), |
|
633 | + 'EventEspresso\core\services\orm\ModelFieldFactory' => array( |
|
634 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
635 | + ), |
|
636 | + 'LEGACY_MODELS' => array( |
|
637 | + null, |
|
638 | + 'EventEspresso\core\services\database\ModelFieldFactory' => EE_Dependency_Map::load_from_cache, |
|
639 | + ), |
|
640 | + 'EE_Module_Request_Router' => array( |
|
641 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
642 | + ), |
|
643 | + 'EE_Registration_Processor' => array( |
|
644 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
645 | + ), |
|
646 | + 'EventEspresso\core\services\currency\CurrencyFactory' => array( |
|
647 | + 'EEM_Country' => EE_Dependency_Map::load_from_cache, |
|
648 | + 'EE_Organization_Config' => EE_Dependency_Map::load_from_cache, |
|
649 | + ), |
|
650 | + 'EventEspresso\core\services\currency\MoneyFactory' => array( |
|
651 | + 'EventEspresso\core\services\currency\CurrencyFactory' => EE_Dependency_Map::load_from_cache, |
|
652 | + 'EventEspresso\core\services\currency\Calculator' => EE_Dependency_Map::load_from_cache, |
|
653 | + array(), |
|
654 | + ), |
|
655 | + ); |
|
656 | + } |
|
657 | + |
|
658 | + |
|
659 | + |
|
660 | + /** |
|
661 | + * Registers how core classes are loaded. |
|
662 | + * This can either be done by simply providing the name of one of the EE_Registry loader methods such as: |
|
663 | + * 'EE_Request_Handler' => 'load_core' |
|
664 | + * 'EE_Messages_Queue' => 'load_lib' |
|
665 | + * 'EEH_Debug_Tools' => 'load_helper' |
|
666 | + * or, if greater control is required, by providing a custom closure. For example: |
|
667 | + * 'Some_Class' => function () { |
|
668 | + * return new Some_Class(); |
|
669 | + * }, |
|
670 | + * This is required for instantiating dependencies |
|
671 | + * where an interface has been type hinted in a class constructor. For example: |
|
672 | + * 'Required_Interface' => function () { |
|
673 | + * return new A_Class_That_Implements_Required_Interface(); |
|
674 | + * }, |
|
675 | + * |
|
676 | + * @throws InvalidInterfaceException |
|
677 | + * @throws InvalidDataTypeException |
|
678 | + * @throws InvalidArgumentException |
|
679 | + */ |
|
680 | + protected function _register_core_class_loaders() |
|
681 | + { |
|
682 | + //for PHP5.3 compat, we need to register any properties called here in a variable because `$this` cannot |
|
683 | + //be used in a closure. |
|
684 | + $request = &$this->_request; |
|
685 | + $response = &$this->_response; |
|
686 | + // $loader = &$this->loader; |
|
687 | + $this->_class_loaders = array( |
|
688 | + //load_core |
|
689 | + 'EE_Capabilities' => 'load_core', |
|
690 | + 'EE_Encryption' => 'load_core', |
|
691 | + 'EE_Front_Controller' => 'load_core', |
|
692 | + 'EE_Module_Request_Router' => 'load_core', |
|
693 | + 'EE_Registry' => 'load_core', |
|
694 | + 'EE_Request' => function () use (&$request) { |
|
695 | + return $request; |
|
696 | + }, |
|
697 | + 'EE_Response' => function () use (&$response) { |
|
698 | + return $response; |
|
699 | + }, |
|
700 | + 'EE_Request_Handler' => 'load_core', |
|
701 | + 'EE_Session' => 'load_core', |
|
702 | + 'EE_Cron_Tasks' => 'load_core', |
|
703 | + 'EE_System' => 'load_core', |
|
704 | + 'EE_Maintenance_Mode' => 'load_core', |
|
705 | + 'EE_Register_CPTs' => 'load_core', |
|
706 | + 'EE_Admin' => 'load_core', |
|
707 | + //load_lib |
|
708 | + 'EE_Message_Resource_Manager' => 'load_lib', |
|
709 | + 'EE_Message_Type_Collection' => 'load_lib', |
|
710 | + 'EE_Message_Type_Collection_Loader' => 'load_lib', |
|
711 | + 'EE_Messenger_Collection' => 'load_lib', |
|
712 | + 'EE_Messenger_Collection_Loader' => 'load_lib', |
|
713 | + 'EE_Messages_Processor' => 'load_lib', |
|
714 | + 'EE_Message_Repository' => 'load_lib', |
|
715 | + 'EE_Messages_Queue' => 'load_lib', |
|
716 | + 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
|
717 | + 'EE_Message_Template_Group_Collection' => 'load_lib', |
|
718 | + 'EE_Payment_Method_Manager' => 'load_lib', |
|
719 | + 'EE_Messages_Generator' => function () { |
|
720 | + return EE_Registry::instance()->load_lib( |
|
721 | + 'Messages_Generator', |
|
722 | + array(), |
|
723 | + false, |
|
724 | + false |
|
725 | + ); |
|
726 | + }, |
|
727 | + 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
728 | + return EE_Registry::instance()->load_lib( |
|
729 | + 'Messages_Template_Defaults', |
|
730 | + $arguments, |
|
731 | + false, |
|
732 | + false |
|
733 | + ); |
|
734 | + }, |
|
735 | + //load_model |
|
736 | + // 'EEM_Attendee' => 'load_model', |
|
737 | + // 'EEM_Message_Template_Group' => 'load_model', |
|
738 | + // 'EEM_Message_Template' => 'load_model', |
|
739 | + //load_helper |
|
740 | + 'EEH_Parse_Shortcodes' => function () { |
|
741 | + if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
|
742 | + return new EEH_Parse_Shortcodes(); |
|
743 | + } |
|
744 | + return null; |
|
745 | + }, |
|
746 | + 'EE_Template_Config' => function () { |
|
747 | + return EE_Config::instance()->template_settings; |
|
748 | + }, |
|
749 | + 'EE_Currency_Config' => function () { |
|
750 | + return EE_Config::instance()->currency; |
|
751 | + }, |
|
752 | + 'EE_Registration_Config' => function () { |
|
753 | + return EE_Config::instance()->registration; |
|
754 | + }, |
|
755 | + 'EE_Organization_Config' => function () { |
|
756 | + return EE_Config::instance()->organization; |
|
757 | + }, |
|
758 | + 'EventEspresso\core\services\loaders\Loader' => function () { |
|
759 | + return LoaderFactory::getLoader(); |
|
760 | + }, |
|
761 | + ); |
|
762 | + } |
|
763 | + |
|
764 | + |
|
765 | + |
|
766 | + /** |
|
767 | + * can be used for supplying alternate names for classes, |
|
768 | + * or for connecting interface names to instantiable classes |
|
769 | + */ |
|
770 | + protected function _register_core_aliases() |
|
771 | + { |
|
772 | + $this->_aliases = array( |
|
773 | + 'CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBusInterface', |
|
774 | + 'EventEspresso\core\services\commands\CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBus', |
|
775 | + 'CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
776 | + 'EventEspresso\core\services\commands\CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManager', |
|
777 | + 'CapChecker' => 'EventEspresso\core\services\commands\middleware\CapChecker', |
|
778 | + 'AddActionHook' => 'EventEspresso\core\services\commands\middleware\AddActionHook', |
|
779 | + 'CapabilitiesChecker' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
780 | + 'CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface', |
|
781 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
782 | + 'CreateRegistrationService' => 'EventEspresso\core\domain\services\registration\CreateRegistrationService', |
|
783 | + 'CreateRegCodeCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegCodeCommand', |
|
784 | + 'CreateRegUrlLinkCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegUrlLinkCommand', |
|
785 | + 'CreateRegistrationCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegistrationCommand', |
|
786 | + 'CopyRegistrationDetailsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommand', |
|
787 | + 'CopyRegistrationPaymentsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommand', |
|
788 | + 'CancelRegistrationAndTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler', |
|
789 | + 'UpdateRegistrationAndTransactionAfterChangeCommandHandler' => 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler', |
|
790 | + 'CreateTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommand', |
|
791 | + 'CreateTransactionCommandHandler' => 'EventEspresso\core\services\commands\transaction\CreateTransactionCommandHandler', |
|
792 | + 'CreateAttendeeCommandHandler' => 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler', |
|
793 | + 'TableManager' => 'EventEspresso\core\services\database\TableManager', |
|
794 | + 'TableAnalysis' => 'EventEspresso\core\services\database\TableAnalysis', |
|
795 | + 'EspressoShortcode' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
796 | + 'ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
797 | + 'EventEspresso\core\services\shortcodes\ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
798 | + 'EventEspresso\core\services\cache\CacheStorageInterface' => 'EventEspresso\core\services\cache\TransientCacheStorage', |
|
799 | + 'LoaderInterface' => 'EventEspresso\core\services\loaders\LoaderInterface', |
|
800 | + 'EventEspresso\core\services\loaders\LoaderInterface' => 'EventEspresso\core\services\loaders\Loader', |
|
801 | + 'CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactoryInterface', |
|
802 | + 'EventEspresso\core\services\commands\CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactory', |
|
803 | + 'EventEspresso\core\domain\services\session\SessionIdentifierInterface' => 'EE_Session', |
|
804 | + 'EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface', |
|
805 | + 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidationService', |
|
806 | + 'NoticeConverterInterface' => 'EventEspresso\core\services\notices\NoticeConverterInterface', |
|
807 | + 'EventEspresso\core\services\notices\NoticeConverterInterface' => 'EventEspresso\core\services\notices\ConvertNoticesToEeErrors', |
|
808 | + 'NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainerInterface', |
|
809 | + 'EventEspresso\core\services\notices\NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainer', |
|
810 | + 'EventEspresso\core\services\currency\Calculator' => 'EventEspresso\core\services\currency\DefaultCalculator', |
|
811 | + ); |
|
812 | + if (! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) { |
|
813 | + $this->_aliases['EventEspresso\core\services\notices\NoticeConverterInterface'] = 'EventEspresso\core\services\notices\ConvertNoticesToAdminNotices'; |
|
814 | + } |
|
815 | + } |
|
816 | + |
|
817 | + |
|
818 | + |
|
819 | + /** |
|
820 | + * This is used to reset the internal map and class_loaders to their original default state at the beginning of the |
|
821 | + * request Primarily used by unit tests. |
|
822 | + * |
|
823 | + * @throws InvalidDataTypeException |
|
824 | + * @throws InvalidInterfaceException |
|
825 | + * @throws InvalidArgumentException |
|
826 | + */ |
|
827 | + public function reset() |
|
828 | + { |
|
829 | + $this->_register_core_class_loaders(); |
|
830 | + $this->_register_core_dependencies(); |
|
831 | + } |
|
832 | 832 | |
833 | 833 | |
834 | 834 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | use EventEspresso\core\services\loaders\LoaderFactory; |
5 | 5 | use EventEspresso\core\services\loaders\LoaderInterface; |
6 | 6 | |
7 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
7 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
8 | 8 | exit('No direct script access allowed'); |
9 | 9 | } |
10 | 10 | |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | public static function instance(EE_Request $request = null, EE_Response $response = null) |
131 | 131 | { |
132 | 132 | // check if class object is instantiated, and instantiated properly |
133 | - if (! self::$_instance instanceof EE_Dependency_Map) { |
|
133 | + if ( ! self::$_instance instanceof EE_Dependency_Map) { |
|
134 | 134 | self::$_instance = new EE_Dependency_Map($request, $response); |
135 | 135 | } |
136 | 136 | return self::$_instance; |
@@ -185,16 +185,16 @@ discard block |
||
185 | 185 | ) { |
186 | 186 | $class = trim($class, '\\'); |
187 | 187 | $registered = false; |
188 | - if (empty(self::$_instance->_dependency_map[ $class ])) { |
|
189 | - self::$_instance->_dependency_map[ $class ] = array(); |
|
188 | + if (empty(self::$_instance->_dependency_map[$class])) { |
|
189 | + self::$_instance->_dependency_map[$class] = array(); |
|
190 | 190 | } |
191 | 191 | // we need to make sure that any aliases used when registering a dependency |
192 | 192 | // get resolved to the correct class name |
193 | - foreach ((array)$dependencies as $dependency => $load_source) { |
|
193 | + foreach ((array) $dependencies as $dependency => $load_source) { |
|
194 | 194 | $alias = self::$_instance->get_alias($dependency); |
195 | 195 | if ( |
196 | 196 | $overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES |
197 | - || ! isset(self::$_instance->_dependency_map[ $class ][ $alias ]) |
|
197 | + || ! isset(self::$_instance->_dependency_map[$class][$alias]) |
|
198 | 198 | ) { |
199 | 199 | unset($dependencies[$dependency]); |
200 | 200 | $dependencies[$alias] = $load_source; |
@@ -207,13 +207,13 @@ discard block |
||
207 | 207 | // ie: with A = B + C, entries in B take precedence over duplicate entries in C |
208 | 208 | // Union is way faster than array_merge() but should be used with caution... |
209 | 209 | // especially with numerically indexed arrays |
210 | - $dependencies += self::$_instance->_dependency_map[ $class ]; |
|
210 | + $dependencies += self::$_instance->_dependency_map[$class]; |
|
211 | 211 | // now we need to ensure that the resulting dependencies |
212 | 212 | // array only has the entries that are required for the class |
213 | 213 | // so first count how many dependencies were originally registered for the class |
214 | - $dependency_count = count(self::$_instance->_dependency_map[ $class ]); |
|
214 | + $dependency_count = count(self::$_instance->_dependency_map[$class]); |
|
215 | 215 | // if that count is non-zero (meaning dependencies were already registered) |
216 | - self::$_instance->_dependency_map[ $class ] = $dependency_count |
|
216 | + self::$_instance->_dependency_map[$class] = $dependency_count |
|
217 | 217 | // then truncate the final array to match that count |
218 | 218 | ? array_slice($dependencies, 0, $dependency_count) |
219 | 219 | // otherwise just take the incoming array because nothing previously existed |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | */ |
232 | 232 | public static function register_class_loader($class_name, $loader = 'load_core') |
233 | 233 | { |
234 | - if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
234 | + if ( ! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
235 | 235 | throw new DomainException( |
236 | 236 | esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso') |
237 | 237 | ); |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | ); |
256 | 256 | } |
257 | 257 | $class_name = self::$_instance->get_alias($class_name); |
258 | - if (! isset(self::$_instance->_class_loaders[$class_name])) { |
|
258 | + if ( ! isset(self::$_instance->_class_loaders[$class_name])) { |
|
259 | 259 | self::$_instance->_class_loaders[$class_name] = $loader; |
260 | 260 | return true; |
261 | 261 | } |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | public function class_loader($class_name) |
341 | 341 | { |
342 | 342 | // all legacy models use load_model() |
343 | - if(strpos($class_name, 'EEM_') === 0){ |
|
343 | + if (strpos($class_name, 'EEM_') === 0) { |
|
344 | 344 | return 'load_model'; |
345 | 345 | } |
346 | 346 | $class_name = $this->get_alias($class_name); |
@@ -369,7 +369,7 @@ discard block |
||
369 | 369 | public function add_alias($class_name, $alias, $for_class = '') |
370 | 370 | { |
371 | 371 | if ($for_class !== '') { |
372 | - if (! isset($this->_aliases[$for_class])) { |
|
372 | + if ( ! isset($this->_aliases[$for_class])) { |
|
373 | 373 | $this->_aliases[$for_class] = array(); |
374 | 374 | } |
375 | 375 | $this->_aliases[$for_class][$class_name] = $alias; |
@@ -415,10 +415,10 @@ discard block |
||
415 | 415 | */ |
416 | 416 | public function get_alias($class_name = '', $for_class = '') |
417 | 417 | { |
418 | - if (! $this->has_alias($class_name, $for_class)) { |
|
418 | + if ( ! $this->has_alias($class_name, $for_class)) { |
|
419 | 419 | return $class_name; |
420 | 420 | } |
421 | - if ($for_class !== '' && isset($this->_aliases[ $for_class ][ $class_name ])) { |
|
421 | + if ($for_class !== '' && isset($this->_aliases[$for_class][$class_name])) { |
|
422 | 422 | return $this->get_alias($this->_aliases[$for_class][$class_name], $for_class); |
423 | 423 | } |
424 | 424 | return $this->get_alias($this->_aliases[$class_name]); |
@@ -691,10 +691,10 @@ discard block |
||
691 | 691 | 'EE_Front_Controller' => 'load_core', |
692 | 692 | 'EE_Module_Request_Router' => 'load_core', |
693 | 693 | 'EE_Registry' => 'load_core', |
694 | - 'EE_Request' => function () use (&$request) { |
|
694 | + 'EE_Request' => function() use (&$request) { |
|
695 | 695 | return $request; |
696 | 696 | }, |
697 | - 'EE_Response' => function () use (&$response) { |
|
697 | + 'EE_Response' => function() use (&$response) { |
|
698 | 698 | return $response; |
699 | 699 | }, |
700 | 700 | 'EE_Request_Handler' => 'load_core', |
@@ -716,7 +716,7 @@ discard block |
||
716 | 716 | 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
717 | 717 | 'EE_Message_Template_Group_Collection' => 'load_lib', |
718 | 718 | 'EE_Payment_Method_Manager' => 'load_lib', |
719 | - 'EE_Messages_Generator' => function () { |
|
719 | + 'EE_Messages_Generator' => function() { |
|
720 | 720 | return EE_Registry::instance()->load_lib( |
721 | 721 | 'Messages_Generator', |
722 | 722 | array(), |
@@ -724,7 +724,7 @@ discard block |
||
724 | 724 | false |
725 | 725 | ); |
726 | 726 | }, |
727 | - 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
727 | + 'EE_Messages_Template_Defaults' => function($arguments = array()) { |
|
728 | 728 | return EE_Registry::instance()->load_lib( |
729 | 729 | 'Messages_Template_Defaults', |
730 | 730 | $arguments, |
@@ -737,25 +737,25 @@ discard block |
||
737 | 737 | // 'EEM_Message_Template_Group' => 'load_model', |
738 | 738 | // 'EEM_Message_Template' => 'load_model', |
739 | 739 | //load_helper |
740 | - 'EEH_Parse_Shortcodes' => function () { |
|
740 | + 'EEH_Parse_Shortcodes' => function() { |
|
741 | 741 | if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
742 | 742 | return new EEH_Parse_Shortcodes(); |
743 | 743 | } |
744 | 744 | return null; |
745 | 745 | }, |
746 | - 'EE_Template_Config' => function () { |
|
746 | + 'EE_Template_Config' => function() { |
|
747 | 747 | return EE_Config::instance()->template_settings; |
748 | 748 | }, |
749 | - 'EE_Currency_Config' => function () { |
|
749 | + 'EE_Currency_Config' => function() { |
|
750 | 750 | return EE_Config::instance()->currency; |
751 | 751 | }, |
752 | - 'EE_Registration_Config' => function () { |
|
752 | + 'EE_Registration_Config' => function() { |
|
753 | 753 | return EE_Config::instance()->registration; |
754 | 754 | }, |
755 | - 'EE_Organization_Config' => function () { |
|
755 | + 'EE_Organization_Config' => function() { |
|
756 | 756 | return EE_Config::instance()->organization; |
757 | 757 | }, |
758 | - 'EventEspresso\core\services\loaders\Loader' => function () { |
|
758 | + 'EventEspresso\core\services\loaders\Loader' => function() { |
|
759 | 759 | return LoaderFactory::getLoader(); |
760 | 760 | }, |
761 | 761 | ); |
@@ -809,7 +809,7 @@ discard block |
||
809 | 809 | 'EventEspresso\core\services\notices\NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainer', |
810 | 810 | 'EventEspresso\core\services\currency\Calculator' => 'EventEspresso\core\services\currency\DefaultCalculator', |
811 | 811 | ); |
812 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) { |
|
812 | + if ( ! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) { |
|
813 | 813 | $this->_aliases['EventEspresso\core\services\notices\NoticeConverterInterface'] = 'EventEspresso\core\services\notices\ConvertNoticesToAdminNotices'; |
814 | 814 | } |
815 | 815 | } |
@@ -22,337 +22,337 @@ |
||
22 | 22 | class Money |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * number of decimal places to be added to currencies for internal computations, |
|
27 | - * but removed before any output or formatting is applied. |
|
28 | - * This allows us to avoid rounding errors during calculations. |
|
29 | - */ |
|
30 | - const EXTRA_PRECISION = 3; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var int $amount |
|
34 | - */ |
|
35 | - private $amount; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var Currency $currency |
|
39 | - */ |
|
40 | - private $currency; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var Calculator $calculator |
|
44 | - */ |
|
45 | - protected $calculator; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var MoneyFormatter[] $formatters |
|
49 | - */ |
|
50 | - protected $formatters; |
|
51 | - |
|
52 | - |
|
53 | - |
|
54 | - /** |
|
55 | - * Money constructor. |
|
56 | - * |
|
57 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
58 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
59 | - * @param Currency $currency |
|
60 | - * @param Calculator $calculator |
|
61 | - * @param MoneyFormatter[] $formatters |
|
62 | - * @throws InvalidDataTypeException |
|
63 | - */ |
|
64 | - public function __construct($amount, Currency $currency, Calculator $calculator, array $formatters) |
|
65 | - { |
|
66 | - $this->currency = $currency; |
|
67 | - $this->amount = (string) $this->parseAmount($amount); |
|
68 | - $this->calculator = $calculator; |
|
69 | - $this->formatters = $formatters; |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * @return Calculator |
|
76 | - */ |
|
77 | - protected function calculator() |
|
78 | - { |
|
79 | - return $this->calculator; |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * @return MoneyFormatter[] |
|
86 | - */ |
|
87 | - protected function formatters() |
|
88 | - { |
|
89 | - return $this->formatters; |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
96 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
97 | - * @return float|int|number|string |
|
98 | - * @throws InvalidDataTypeException |
|
99 | - */ |
|
100 | - private function parseAmount($amount) |
|
101 | - { |
|
102 | - $type = gettype($amount); |
|
103 | - switch ($type) { |
|
104 | - case 'integer' : |
|
105 | - case 'double' : |
|
106 | - case 'string' : |
|
107 | - break; |
|
108 | - default : |
|
109 | - throw new InvalidDataTypeException( |
|
110 | - '$amount', |
|
111 | - $amount, |
|
112 | - 'integer (or float or string)' |
|
113 | - ); |
|
114 | - } |
|
115 | - if ($this->currency->decimalMark() !== '.') { |
|
116 | - // remove thousands separator and replace decimal place with standard decimal. |
|
117 | - $amount = str_replace( |
|
118 | - array( |
|
119 | - $this->currency->thousands(), |
|
120 | - $this->currency->decimalMark(), |
|
121 | - ), |
|
122 | - array( |
|
123 | - '', |
|
124 | - '.', |
|
125 | - ), |
|
126 | - $amount |
|
127 | - ); |
|
128 | - } |
|
129 | - // remove any non numeric values but leave the decimal |
|
130 | - $amount = (float) preg_replace('/([^0-9\\.])/', '', $amount); |
|
131 | - // shift the decimal position by the number of decimal places used internally |
|
132 | - // ex: 12.5 for a currency using 2 decimal places, would become 1250 |
|
133 | - // then if our extra internal precision was 3, it would become 1250000 |
|
134 | - $amount *= pow(10, $this->precision()); |
|
135 | - // then round up the remaining value if there is still a fractional amount left |
|
136 | - $amount = round($amount); |
|
137 | - return $amount; |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * adds or subtracts additional decimal places based on the value of the Money::EXTRA_PRECISION constant |
|
144 | - * |
|
145 | - * @param bool $positive |
|
146 | - * @return int |
|
147 | - */ |
|
148 | - private function precision($positive = true) |
|
149 | - { |
|
150 | - $sign = $positive ? 1 : -1; |
|
151 | - return ((int) $this->currency->decimalPlaces() + Money::EXTRA_PRECISION) * $sign; |
|
152 | - } |
|
153 | - |
|
154 | - |
|
155 | - |
|
156 | - /** |
|
157 | - * Returns the money amount as an unformatted string |
|
158 | - * IF YOU REQUIRE A FORMATTED STRING, THEN USE Money::format() |
|
159 | - * |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - public function amount() |
|
163 | - { |
|
164 | - // shift the decimal position BACK by the number of decimal places used internally |
|
165 | - // ex: 1250 for a currency using 2 decimal places, would become 12.50 |
|
166 | - $amount = (string) $this->amount * pow(10, $this->precision(false)); |
|
167 | - // then shave off our extra internal precision using the number of decimal places for the currency |
|
168 | - $amount = round($amount, $this->currency->decimalPlaces()); |
|
169 | - return $amount; |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * Returns the money SUBUNITS amount as an INTEGER |
|
176 | - * |
|
177 | - * @return integer |
|
178 | - */ |
|
179 | - public function amountInSubunits() |
|
180 | - { |
|
181 | - // shift the decimal position BACK |
|
182 | - // by the number of decimal places used internally by the extra internal precision |
|
183 | - // ex: if our extra internal precision was 3, |
|
184 | - // then 1250000 would become 1250 |
|
185 | - $amount = (string) $this->amount * pow(10, Money::EXTRA_PRECISION * -1); |
|
186 | - // then shave off anything after the decimal |
|
187 | - $amount = round($amount); |
|
188 | - return $amount; |
|
189 | - } |
|
190 | - |
|
191 | - |
|
192 | - |
|
193 | - /** |
|
194 | - * applies formatting based on the specified formatting level |
|
195 | - * corresponding to one of the constants on \EventEspresso\core\services\currency\MoneyFormatter |
|
196 | - * |
|
197 | - * @param int $formatting_level |
|
198 | - * @return string |
|
199 | - */ |
|
200 | - public function format($formatting_level = MoneyFormatter::ADD_THOUSANDS) |
|
201 | - { |
|
202 | - $formatted_amount = $this->amount(); |
|
203 | - $formatters = $this->formatters(); |
|
204 | - // if we are applying thousands formatting... |
|
205 | - if ($formatting_level >= MoneyFormatter::ADD_THOUSANDS) { |
|
206 | - // then let's remove decimal formatting since it's included in thousands formatting |
|
207 | - unset($formatters[ MoneyFormatter::DECIMAL_ONLY ]); |
|
208 | - } |
|
209 | - for ($x = 1; $x <= $formatting_level; $x++) { |
|
210 | - if (isset($formatters[ $x ]) && $formatters[ $x ] instanceof MoneyFormatter) { |
|
211 | - $formatted_amount = $formatters[ $x ]->format($formatted_amount, $this->currency); |
|
212 | - } |
|
213 | - } |
|
214 | - return $formatted_amount; |
|
215 | - } |
|
216 | - |
|
217 | - |
|
218 | - |
|
219 | - /** |
|
220 | - * Returns the Currency object for this money |
|
221 | - * |
|
222 | - * @return Currency |
|
223 | - */ |
|
224 | - public function currency() |
|
225 | - { |
|
226 | - return $this->currency; |
|
227 | - } |
|
228 | - |
|
229 | - |
|
230 | - |
|
231 | - /** |
|
232 | - * adds the supplied Money amount to this Money amount |
|
233 | - * and returns a new Money object |
|
234 | - * |
|
235 | - * @param Money $other |
|
236 | - * @return Money |
|
237 | - * @throws InvalidArgumentException |
|
238 | - */ |
|
239 | - public function add(Money $other) |
|
240 | - { |
|
241 | - $this->verifySameCurrency($other->currency()); |
|
242 | - return new Money( |
|
243 | - $this->calculator()->add( |
|
244 | - $this->amount(), |
|
245 | - $other->amount() |
|
246 | - ), |
|
247 | - $this->currency(), |
|
248 | - $this->calculator(), |
|
249 | - $this->formatters() |
|
250 | - ); |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - |
|
255 | - /** |
|
256 | - * subtracts the supplied Money amount from this Money amount |
|
257 | - * and returns a new Money object |
|
258 | - * |
|
259 | - * @param Money $other |
|
260 | - * @return Money |
|
261 | - * @throws InvalidArgumentException |
|
262 | - */ |
|
263 | - public function subtract(Money $other) |
|
264 | - { |
|
265 | - $this->verifySameCurrency($other->currency()); |
|
266 | - return new Money( |
|
267 | - $this->calculator()->subtract( |
|
268 | - $this->amount(), |
|
269 | - $other->amount() |
|
270 | - ), |
|
271 | - $this->currency(), |
|
272 | - $this->calculator(), |
|
273 | - $this->formatters() |
|
274 | - ); |
|
275 | - } |
|
276 | - |
|
277 | - |
|
278 | - |
|
279 | - /** |
|
280 | - * multiplies this Money amount by the supplied $multiplier |
|
281 | - * and returns a new Money object |
|
282 | - * |
|
283 | - * @param float|int|string $multiplier |
|
284 | - * @param int $rounding_mode |
|
285 | - * @return Money |
|
286 | - * @throws InvalidDataTypeException |
|
287 | - */ |
|
288 | - public function multiply($multiplier, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
289 | - { |
|
290 | - return new Money( |
|
291 | - $this->calculator()->multiply( |
|
292 | - $this->amount(), |
|
293 | - $multiplier, |
|
294 | - $this->precision(), |
|
295 | - $rounding_mode |
|
296 | - ), |
|
297 | - $this->currency(), |
|
298 | - $this->calculator(), |
|
299 | - $this->formatters() |
|
300 | - ); |
|
301 | - } |
|
302 | - |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * divides this Money amount by the supplied $divisor |
|
307 | - * and returns a new Money object |
|
308 | - * |
|
309 | - * @param float|int|string $divisor |
|
310 | - * @param int $rounding_mode |
|
311 | - * @return Money |
|
312 | - * @throws InvalidDataTypeException |
|
313 | - */ |
|
314 | - public function divide($divisor, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
315 | - { |
|
316 | - return new Money( |
|
317 | - $this->calculator()->divide( |
|
318 | - $this->amount(), |
|
319 | - $divisor, |
|
320 | - $this->precision(), |
|
321 | - $rounding_mode |
|
322 | - ), |
|
323 | - $this->currency(), |
|
324 | - $this->calculator(), |
|
325 | - $this->formatters() |
|
326 | - ); |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - |
|
331 | - /** |
|
332 | - * @param Currency $other_currency |
|
333 | - * @throws InvalidArgumentException |
|
334 | - */ |
|
335 | - public function verifySameCurrency(Currency $other_currency) |
|
336 | - { |
|
337 | - if ($this->currency()->equals($other_currency) !== true) { |
|
338 | - throw new InvalidArgumentException( |
|
339 | - esc_html__( |
|
340 | - 'Currencies must be the same in order to add or subtract their values.', |
|
341 | - 'event_espresso' |
|
342 | - ) |
|
343 | - ); |
|
344 | - } |
|
345 | - } |
|
346 | - |
|
347 | - |
|
348 | - |
|
349 | - /** |
|
350 | - * @return string |
|
351 | - */ |
|
352 | - public function __toString() |
|
353 | - { |
|
354 | - return $this->format(MoneyFormatter::DECIMAL_ONLY); |
|
355 | - } |
|
25 | + /** |
|
26 | + * number of decimal places to be added to currencies for internal computations, |
|
27 | + * but removed before any output or formatting is applied. |
|
28 | + * This allows us to avoid rounding errors during calculations. |
|
29 | + */ |
|
30 | + const EXTRA_PRECISION = 3; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var int $amount |
|
34 | + */ |
|
35 | + private $amount; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var Currency $currency |
|
39 | + */ |
|
40 | + private $currency; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var Calculator $calculator |
|
44 | + */ |
|
45 | + protected $calculator; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var MoneyFormatter[] $formatters |
|
49 | + */ |
|
50 | + protected $formatters; |
|
51 | + |
|
52 | + |
|
53 | + |
|
54 | + /** |
|
55 | + * Money constructor. |
|
56 | + * |
|
57 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
58 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
59 | + * @param Currency $currency |
|
60 | + * @param Calculator $calculator |
|
61 | + * @param MoneyFormatter[] $formatters |
|
62 | + * @throws InvalidDataTypeException |
|
63 | + */ |
|
64 | + public function __construct($amount, Currency $currency, Calculator $calculator, array $formatters) |
|
65 | + { |
|
66 | + $this->currency = $currency; |
|
67 | + $this->amount = (string) $this->parseAmount($amount); |
|
68 | + $this->calculator = $calculator; |
|
69 | + $this->formatters = $formatters; |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * @return Calculator |
|
76 | + */ |
|
77 | + protected function calculator() |
|
78 | + { |
|
79 | + return $this->calculator; |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * @return MoneyFormatter[] |
|
86 | + */ |
|
87 | + protected function formatters() |
|
88 | + { |
|
89 | + return $this->formatters; |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
96 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
97 | + * @return float|int|number|string |
|
98 | + * @throws InvalidDataTypeException |
|
99 | + */ |
|
100 | + private function parseAmount($amount) |
|
101 | + { |
|
102 | + $type = gettype($amount); |
|
103 | + switch ($type) { |
|
104 | + case 'integer' : |
|
105 | + case 'double' : |
|
106 | + case 'string' : |
|
107 | + break; |
|
108 | + default : |
|
109 | + throw new InvalidDataTypeException( |
|
110 | + '$amount', |
|
111 | + $amount, |
|
112 | + 'integer (or float or string)' |
|
113 | + ); |
|
114 | + } |
|
115 | + if ($this->currency->decimalMark() !== '.') { |
|
116 | + // remove thousands separator and replace decimal place with standard decimal. |
|
117 | + $amount = str_replace( |
|
118 | + array( |
|
119 | + $this->currency->thousands(), |
|
120 | + $this->currency->decimalMark(), |
|
121 | + ), |
|
122 | + array( |
|
123 | + '', |
|
124 | + '.', |
|
125 | + ), |
|
126 | + $amount |
|
127 | + ); |
|
128 | + } |
|
129 | + // remove any non numeric values but leave the decimal |
|
130 | + $amount = (float) preg_replace('/([^0-9\\.])/', '', $amount); |
|
131 | + // shift the decimal position by the number of decimal places used internally |
|
132 | + // ex: 12.5 for a currency using 2 decimal places, would become 1250 |
|
133 | + // then if our extra internal precision was 3, it would become 1250000 |
|
134 | + $amount *= pow(10, $this->precision()); |
|
135 | + // then round up the remaining value if there is still a fractional amount left |
|
136 | + $amount = round($amount); |
|
137 | + return $amount; |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * adds or subtracts additional decimal places based on the value of the Money::EXTRA_PRECISION constant |
|
144 | + * |
|
145 | + * @param bool $positive |
|
146 | + * @return int |
|
147 | + */ |
|
148 | + private function precision($positive = true) |
|
149 | + { |
|
150 | + $sign = $positive ? 1 : -1; |
|
151 | + return ((int) $this->currency->decimalPlaces() + Money::EXTRA_PRECISION) * $sign; |
|
152 | + } |
|
153 | + |
|
154 | + |
|
155 | + |
|
156 | + /** |
|
157 | + * Returns the money amount as an unformatted string |
|
158 | + * IF YOU REQUIRE A FORMATTED STRING, THEN USE Money::format() |
|
159 | + * |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + public function amount() |
|
163 | + { |
|
164 | + // shift the decimal position BACK by the number of decimal places used internally |
|
165 | + // ex: 1250 for a currency using 2 decimal places, would become 12.50 |
|
166 | + $amount = (string) $this->amount * pow(10, $this->precision(false)); |
|
167 | + // then shave off our extra internal precision using the number of decimal places for the currency |
|
168 | + $amount = round($amount, $this->currency->decimalPlaces()); |
|
169 | + return $amount; |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * Returns the money SUBUNITS amount as an INTEGER |
|
176 | + * |
|
177 | + * @return integer |
|
178 | + */ |
|
179 | + public function amountInSubunits() |
|
180 | + { |
|
181 | + // shift the decimal position BACK |
|
182 | + // by the number of decimal places used internally by the extra internal precision |
|
183 | + // ex: if our extra internal precision was 3, |
|
184 | + // then 1250000 would become 1250 |
|
185 | + $amount = (string) $this->amount * pow(10, Money::EXTRA_PRECISION * -1); |
|
186 | + // then shave off anything after the decimal |
|
187 | + $amount = round($amount); |
|
188 | + return $amount; |
|
189 | + } |
|
190 | + |
|
191 | + |
|
192 | + |
|
193 | + /** |
|
194 | + * applies formatting based on the specified formatting level |
|
195 | + * corresponding to one of the constants on \EventEspresso\core\services\currency\MoneyFormatter |
|
196 | + * |
|
197 | + * @param int $formatting_level |
|
198 | + * @return string |
|
199 | + */ |
|
200 | + public function format($formatting_level = MoneyFormatter::ADD_THOUSANDS) |
|
201 | + { |
|
202 | + $formatted_amount = $this->amount(); |
|
203 | + $formatters = $this->formatters(); |
|
204 | + // if we are applying thousands formatting... |
|
205 | + if ($formatting_level >= MoneyFormatter::ADD_THOUSANDS) { |
|
206 | + // then let's remove decimal formatting since it's included in thousands formatting |
|
207 | + unset($formatters[ MoneyFormatter::DECIMAL_ONLY ]); |
|
208 | + } |
|
209 | + for ($x = 1; $x <= $formatting_level; $x++) { |
|
210 | + if (isset($formatters[ $x ]) && $formatters[ $x ] instanceof MoneyFormatter) { |
|
211 | + $formatted_amount = $formatters[ $x ]->format($formatted_amount, $this->currency); |
|
212 | + } |
|
213 | + } |
|
214 | + return $formatted_amount; |
|
215 | + } |
|
216 | + |
|
217 | + |
|
218 | + |
|
219 | + /** |
|
220 | + * Returns the Currency object for this money |
|
221 | + * |
|
222 | + * @return Currency |
|
223 | + */ |
|
224 | + public function currency() |
|
225 | + { |
|
226 | + return $this->currency; |
|
227 | + } |
|
228 | + |
|
229 | + |
|
230 | + |
|
231 | + /** |
|
232 | + * adds the supplied Money amount to this Money amount |
|
233 | + * and returns a new Money object |
|
234 | + * |
|
235 | + * @param Money $other |
|
236 | + * @return Money |
|
237 | + * @throws InvalidArgumentException |
|
238 | + */ |
|
239 | + public function add(Money $other) |
|
240 | + { |
|
241 | + $this->verifySameCurrency($other->currency()); |
|
242 | + return new Money( |
|
243 | + $this->calculator()->add( |
|
244 | + $this->amount(), |
|
245 | + $other->amount() |
|
246 | + ), |
|
247 | + $this->currency(), |
|
248 | + $this->calculator(), |
|
249 | + $this->formatters() |
|
250 | + ); |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + |
|
255 | + /** |
|
256 | + * subtracts the supplied Money amount from this Money amount |
|
257 | + * and returns a new Money object |
|
258 | + * |
|
259 | + * @param Money $other |
|
260 | + * @return Money |
|
261 | + * @throws InvalidArgumentException |
|
262 | + */ |
|
263 | + public function subtract(Money $other) |
|
264 | + { |
|
265 | + $this->verifySameCurrency($other->currency()); |
|
266 | + return new Money( |
|
267 | + $this->calculator()->subtract( |
|
268 | + $this->amount(), |
|
269 | + $other->amount() |
|
270 | + ), |
|
271 | + $this->currency(), |
|
272 | + $this->calculator(), |
|
273 | + $this->formatters() |
|
274 | + ); |
|
275 | + } |
|
276 | + |
|
277 | + |
|
278 | + |
|
279 | + /** |
|
280 | + * multiplies this Money amount by the supplied $multiplier |
|
281 | + * and returns a new Money object |
|
282 | + * |
|
283 | + * @param float|int|string $multiplier |
|
284 | + * @param int $rounding_mode |
|
285 | + * @return Money |
|
286 | + * @throws InvalidDataTypeException |
|
287 | + */ |
|
288 | + public function multiply($multiplier, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
289 | + { |
|
290 | + return new Money( |
|
291 | + $this->calculator()->multiply( |
|
292 | + $this->amount(), |
|
293 | + $multiplier, |
|
294 | + $this->precision(), |
|
295 | + $rounding_mode |
|
296 | + ), |
|
297 | + $this->currency(), |
|
298 | + $this->calculator(), |
|
299 | + $this->formatters() |
|
300 | + ); |
|
301 | + } |
|
302 | + |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * divides this Money amount by the supplied $divisor |
|
307 | + * and returns a new Money object |
|
308 | + * |
|
309 | + * @param float|int|string $divisor |
|
310 | + * @param int $rounding_mode |
|
311 | + * @return Money |
|
312 | + * @throws InvalidDataTypeException |
|
313 | + */ |
|
314 | + public function divide($divisor, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
315 | + { |
|
316 | + return new Money( |
|
317 | + $this->calculator()->divide( |
|
318 | + $this->amount(), |
|
319 | + $divisor, |
|
320 | + $this->precision(), |
|
321 | + $rounding_mode |
|
322 | + ), |
|
323 | + $this->currency(), |
|
324 | + $this->calculator(), |
|
325 | + $this->formatters() |
|
326 | + ); |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + |
|
331 | + /** |
|
332 | + * @param Currency $other_currency |
|
333 | + * @throws InvalidArgumentException |
|
334 | + */ |
|
335 | + public function verifySameCurrency(Currency $other_currency) |
|
336 | + { |
|
337 | + if ($this->currency()->equals($other_currency) !== true) { |
|
338 | + throw new InvalidArgumentException( |
|
339 | + esc_html__( |
|
340 | + 'Currencies must be the same in order to add or subtract their values.', |
|
341 | + 'event_espresso' |
|
342 | + ) |
|
343 | + ); |
|
344 | + } |
|
345 | + } |
|
346 | + |
|
347 | + |
|
348 | + |
|
349 | + /** |
|
350 | + * @return string |
|
351 | + */ |
|
352 | + public function __toString() |
|
353 | + { |
|
354 | + return $this->format(MoneyFormatter::DECIMAL_ONLY); |
|
355 | + } |
|
356 | 356 | |
357 | 357 | |
358 | 358 |
@@ -27,139 +27,139 @@ |
||
27 | 27 | class CurrencyFactory |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * @var EEM_Country $country_model |
|
32 | - */ |
|
33 | - protected $country_model; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var EE_Country[] $countries |
|
37 | - */ |
|
38 | - protected $countries_by_iso_code; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var EE_Country[] $countries |
|
42 | - */ |
|
43 | - protected $countries_by_currency; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var EE_Organization_Config $organization_config |
|
47 | - */ |
|
48 | - protected $organization_config; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var string $site_country_iso |
|
52 | - */ |
|
53 | - protected $site_country_iso; |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * CurrencyFactory constructor. |
|
58 | - * |
|
59 | - * @param EEM_Country $country_model |
|
60 | - * @param EE_Organization_Config $organization_config |
|
61 | - */ |
|
62 | - public function __construct(EEM_Country $country_model, EE_Organization_Config $organization_config) |
|
63 | - { |
|
64 | - $this->country_model = $country_model; |
|
65 | - $this->organization_config = $organization_config; |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * returns a Currency object for the supplied country code |
|
71 | - * |
|
72 | - * @param string $CNT_ISO |
|
73 | - * @return Currency |
|
74 | - * @throws InvalidDataTypeException |
|
75 | - * @throws InvalidInterfaceException |
|
76 | - * @throws EE_Error |
|
77 | - * @throws InvalidArgumentException |
|
78 | - */ |
|
79 | - public function createFromCountryCode($CNT_ISO = null) |
|
80 | - { |
|
81 | - $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : $this->organization_config->CNT_ISO; |
|
82 | - if(isset($this->countries_by_iso_code[ $CNT_ISO])) { |
|
83 | - $country = $this->countries_by_iso_code[ $CNT_ISO ]; |
|
84 | - } else { |
|
85 | - /** @var EE_Country $country */ |
|
86 | - $country = $this->country_model->get_one_by_ID($CNT_ISO); |
|
87 | - if (! $country instanceof EE_Country) { |
|
88 | - throw new InvalidArgumentException( |
|
89 | - sprintf( |
|
90 | - esc_html__( |
|
91 | - 'A valid country could not be found for the "%1$s" country code;', |
|
92 | - 'event_espresso' |
|
93 | - ), |
|
94 | - $CNT_ISO |
|
95 | - ) |
|
96 | - ); |
|
97 | - } |
|
98 | - $this->countries_by_iso_code[ $CNT_ISO ] = $country; |
|
99 | - $this->countries_by_currency[ $country->currency_code() ] = $country; |
|
100 | - } |
|
101 | - return new Currency( |
|
102 | - $country->currency_code(), |
|
103 | - new Label( |
|
104 | - $country->currency_name_single(), |
|
105 | - $country->currency_name_plural() |
|
106 | - ), |
|
107 | - $country->currency_sign(), |
|
108 | - $country->currency_sign_before(), |
|
109 | - $country->currency_decimal_places(), |
|
110 | - $country->currency_decimal_mark(), |
|
111 | - $country->currency_thousands_separator() |
|
112 | - ); |
|
113 | - } |
|
114 | - |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * returns a Currency object for the supplied currency code |
|
119 | - * PLZ NOTE: variations may exist between how different countries display the same currency, |
|
120 | - * so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results |
|
121 | - * |
|
122 | - * @param string $code |
|
123 | - * @return Currency |
|
124 | - * @throws InvalidInterfaceException |
|
125 | - * @throws InvalidDataTypeException |
|
126 | - * @throws InvalidArgumentException |
|
127 | - * @throws EE_Error |
|
128 | - */ |
|
129 | - public function createFromCode($code) |
|
130 | - { |
|
131 | - if (isset($this->countries_by_currency[ $code ])) { |
|
132 | - $country = $this->countries_by_currency[ $code ]; |
|
133 | - } else { |
|
134 | - /** @var EE_Country $country */ |
|
135 | - $country = $this->country_model->get_one(array(array('CNT_cur_code' => $code))); |
|
136 | - if (! $country instanceof EE_Country) { |
|
137 | - throw new InvalidArgumentException( |
|
138 | - sprintf( |
|
139 | - esc_html__( |
|
140 | - 'A valid currency could not be found for the "%1$s" currency code;', |
|
141 | - 'event_espresso' |
|
142 | - ), |
|
143 | - $code |
|
144 | - ) |
|
145 | - ); |
|
146 | - } |
|
147 | - $this->countries_by_iso_code[ $country->ID() ] = $country; |
|
148 | - $this->countries_by_currency[ $code ] = $country; |
|
149 | - } |
|
150 | - return new Currency( |
|
151 | - $country->currency_code(), |
|
152 | - new Label( |
|
153 | - $country->currency_name_single(), |
|
154 | - $country->currency_name_plural() |
|
155 | - ), |
|
156 | - $country->currency_sign(), |
|
157 | - $country->currency_sign_before(), |
|
158 | - $country->currency_decimal_places(), |
|
159 | - $country->currency_decimal_mark(), |
|
160 | - $country->currency_thousands_separator() |
|
161 | - ); |
|
162 | - } |
|
30 | + /** |
|
31 | + * @var EEM_Country $country_model |
|
32 | + */ |
|
33 | + protected $country_model; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var EE_Country[] $countries |
|
37 | + */ |
|
38 | + protected $countries_by_iso_code; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var EE_Country[] $countries |
|
42 | + */ |
|
43 | + protected $countries_by_currency; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var EE_Organization_Config $organization_config |
|
47 | + */ |
|
48 | + protected $organization_config; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var string $site_country_iso |
|
52 | + */ |
|
53 | + protected $site_country_iso; |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * CurrencyFactory constructor. |
|
58 | + * |
|
59 | + * @param EEM_Country $country_model |
|
60 | + * @param EE_Organization_Config $organization_config |
|
61 | + */ |
|
62 | + public function __construct(EEM_Country $country_model, EE_Organization_Config $organization_config) |
|
63 | + { |
|
64 | + $this->country_model = $country_model; |
|
65 | + $this->organization_config = $organization_config; |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * returns a Currency object for the supplied country code |
|
71 | + * |
|
72 | + * @param string $CNT_ISO |
|
73 | + * @return Currency |
|
74 | + * @throws InvalidDataTypeException |
|
75 | + * @throws InvalidInterfaceException |
|
76 | + * @throws EE_Error |
|
77 | + * @throws InvalidArgumentException |
|
78 | + */ |
|
79 | + public function createFromCountryCode($CNT_ISO = null) |
|
80 | + { |
|
81 | + $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : $this->organization_config->CNT_ISO; |
|
82 | + if(isset($this->countries_by_iso_code[ $CNT_ISO])) { |
|
83 | + $country = $this->countries_by_iso_code[ $CNT_ISO ]; |
|
84 | + } else { |
|
85 | + /** @var EE_Country $country */ |
|
86 | + $country = $this->country_model->get_one_by_ID($CNT_ISO); |
|
87 | + if (! $country instanceof EE_Country) { |
|
88 | + throw new InvalidArgumentException( |
|
89 | + sprintf( |
|
90 | + esc_html__( |
|
91 | + 'A valid country could not be found for the "%1$s" country code;', |
|
92 | + 'event_espresso' |
|
93 | + ), |
|
94 | + $CNT_ISO |
|
95 | + ) |
|
96 | + ); |
|
97 | + } |
|
98 | + $this->countries_by_iso_code[ $CNT_ISO ] = $country; |
|
99 | + $this->countries_by_currency[ $country->currency_code() ] = $country; |
|
100 | + } |
|
101 | + return new Currency( |
|
102 | + $country->currency_code(), |
|
103 | + new Label( |
|
104 | + $country->currency_name_single(), |
|
105 | + $country->currency_name_plural() |
|
106 | + ), |
|
107 | + $country->currency_sign(), |
|
108 | + $country->currency_sign_before(), |
|
109 | + $country->currency_decimal_places(), |
|
110 | + $country->currency_decimal_mark(), |
|
111 | + $country->currency_thousands_separator() |
|
112 | + ); |
|
113 | + } |
|
114 | + |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * returns a Currency object for the supplied currency code |
|
119 | + * PLZ NOTE: variations may exist between how different countries display the same currency, |
|
120 | + * so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results |
|
121 | + * |
|
122 | + * @param string $code |
|
123 | + * @return Currency |
|
124 | + * @throws InvalidInterfaceException |
|
125 | + * @throws InvalidDataTypeException |
|
126 | + * @throws InvalidArgumentException |
|
127 | + * @throws EE_Error |
|
128 | + */ |
|
129 | + public function createFromCode($code) |
|
130 | + { |
|
131 | + if (isset($this->countries_by_currency[ $code ])) { |
|
132 | + $country = $this->countries_by_currency[ $code ]; |
|
133 | + } else { |
|
134 | + /** @var EE_Country $country */ |
|
135 | + $country = $this->country_model->get_one(array(array('CNT_cur_code' => $code))); |
|
136 | + if (! $country instanceof EE_Country) { |
|
137 | + throw new InvalidArgumentException( |
|
138 | + sprintf( |
|
139 | + esc_html__( |
|
140 | + 'A valid currency could not be found for the "%1$s" currency code;', |
|
141 | + 'event_espresso' |
|
142 | + ), |
|
143 | + $code |
|
144 | + ) |
|
145 | + ); |
|
146 | + } |
|
147 | + $this->countries_by_iso_code[ $country->ID() ] = $country; |
|
148 | + $this->countries_by_currency[ $code ] = $country; |
|
149 | + } |
|
150 | + return new Currency( |
|
151 | + $country->currency_code(), |
|
152 | + new Label( |
|
153 | + $country->currency_name_single(), |
|
154 | + $country->currency_name_plural() |
|
155 | + ), |
|
156 | + $country->currency_sign(), |
|
157 | + $country->currency_sign_before(), |
|
158 | + $country->currency_decimal_places(), |
|
159 | + $country->currency_decimal_mark(), |
|
160 | + $country->currency_thousands_separator() |
|
161 | + ); |
|
162 | + } |
|
163 | 163 | |
164 | 164 | |
165 | 165 |
@@ -79,12 +79,12 @@ discard block |
||
79 | 79 | public function createFromCountryCode($CNT_ISO = null) |
80 | 80 | { |
81 | 81 | $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : $this->organization_config->CNT_ISO; |
82 | - if(isset($this->countries_by_iso_code[ $CNT_ISO])) { |
|
83 | - $country = $this->countries_by_iso_code[ $CNT_ISO ]; |
|
82 | + if (isset($this->countries_by_iso_code[$CNT_ISO])) { |
|
83 | + $country = $this->countries_by_iso_code[$CNT_ISO]; |
|
84 | 84 | } else { |
85 | 85 | /** @var EE_Country $country */ |
86 | 86 | $country = $this->country_model->get_one_by_ID($CNT_ISO); |
87 | - if (! $country instanceof EE_Country) { |
|
87 | + if ( ! $country instanceof EE_Country) { |
|
88 | 88 | throw new InvalidArgumentException( |
89 | 89 | sprintf( |
90 | 90 | esc_html__( |
@@ -95,8 +95,8 @@ discard block |
||
95 | 95 | ) |
96 | 96 | ); |
97 | 97 | } |
98 | - $this->countries_by_iso_code[ $CNT_ISO ] = $country; |
|
99 | - $this->countries_by_currency[ $country->currency_code() ] = $country; |
|
98 | + $this->countries_by_iso_code[$CNT_ISO] = $country; |
|
99 | + $this->countries_by_currency[$country->currency_code()] = $country; |
|
100 | 100 | } |
101 | 101 | return new Currency( |
102 | 102 | $country->currency_code(), |
@@ -128,12 +128,12 @@ discard block |
||
128 | 128 | */ |
129 | 129 | public function createFromCode($code) |
130 | 130 | { |
131 | - if (isset($this->countries_by_currency[ $code ])) { |
|
132 | - $country = $this->countries_by_currency[ $code ]; |
|
131 | + if (isset($this->countries_by_currency[$code])) { |
|
132 | + $country = $this->countries_by_currency[$code]; |
|
133 | 133 | } else { |
134 | 134 | /** @var EE_Country $country */ |
135 | 135 | $country = $this->country_model->get_one(array(array('CNT_cur_code' => $code))); |
136 | - if (! $country instanceof EE_Country) { |
|
136 | + if ( ! $country instanceof EE_Country) { |
|
137 | 137 | throw new InvalidArgumentException( |
138 | 138 | sprintf( |
139 | 139 | esc_html__( |
@@ -144,8 +144,8 @@ discard block |
||
144 | 144 | ) |
145 | 145 | ); |
146 | 146 | } |
147 | - $this->countries_by_iso_code[ $country->ID() ] = $country; |
|
148 | - $this->countries_by_currency[ $code ] = $country; |
|
147 | + $this->countries_by_iso_code[$country->ID()] = $country; |
|
148 | + $this->countries_by_currency[$code] = $country; |
|
149 | 149 | } |
150 | 150 | return new Currency( |
151 | 151 | $country->currency_code(), |
@@ -23,213 +23,213 @@ |
||
23 | 23 | class MoneyFactory |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * @var CurrencyFactory $currency_factory |
|
28 | - */ |
|
29 | - protected $currency_factory; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var Calculator $calculator |
|
33 | - */ |
|
34 | - protected $calculator; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var MoneyFormatter[] $formatters |
|
38 | - */ |
|
39 | - protected $formatters; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * CreateMoney constructor. |
|
44 | - * |
|
45 | - * @param CurrencyFactory $currency_factory |
|
46 | - * @param Calculator $calculator |
|
47 | - * @param MoneyFormatter[] $formatters |
|
48 | - */ |
|
49 | - public function __construct( |
|
50 | - CurrencyFactory $currency_factory, |
|
51 | - Calculator $calculator = null, |
|
52 | - array $formatters = array() |
|
53 | - ) { |
|
54 | - $this->calculator = $calculator; |
|
55 | - $this->formatters = $formatters; |
|
56 | - $this->currency_factory = $currency_factory; |
|
57 | - } |
|
58 | - |
|
59 | - |
|
60 | - |
|
61 | - /** |
|
62 | - * factory method that returns a Money object using amount specified in the currency's subunits |
|
63 | - * example: for $12.50 USD use CreateMoney::fromSubUnits(1250, 'USD') |
|
64 | - * |
|
65 | - * @param int $subunits_amount money amount IN THE SUBUNITS FOR THE CURRENCY ie: cents |
|
66 | - * example: $12.50 USD would equate to a subunits amount of 1250 |
|
67 | - * @param string $currency_code |
|
68 | - * @return Money |
|
69 | - * @throws EE_Error |
|
70 | - * @throws InvalidArgumentException |
|
71 | - * @throws InvalidDataTypeException |
|
72 | - * @throws InvalidInterfaceException |
|
73 | - */ |
|
74 | - public function createFromSubUnits($subunits_amount, $currency_code = '') |
|
75 | - { |
|
76 | - $currency = $this->currency_factory->createFromCode($currency_code); |
|
77 | - return new Money( |
|
78 | - // shift decimal BACK by number of places for currency |
|
79 | - $subunits_amount * pow(10, $currency->decimalPlaces() * -1), |
|
80 | - $currency, |
|
81 | - $this->calculator(), |
|
82 | - $this->formatters() |
|
83 | - ); |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * factory method that returns a Money object using the currency corresponding to the site's country |
|
90 | - * example: CreateMoney::forSite(12.5) |
|
91 | - * |
|
92 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
93 | - * example: $12.5 USD would equate to a standard amount of 12.50 |
|
94 | - * @return Money |
|
95 | - * @throws EE_Error |
|
96 | - * @throws InvalidArgumentException |
|
97 | - * @throws InvalidDataTypeException |
|
98 | - * @throws InvalidInterfaceException |
|
99 | - */ |
|
100 | - public function createForSite($amount) |
|
101 | - { |
|
102 | - return new Money( |
|
103 | - $amount, |
|
104 | - $this->currency_factory->createFromCountryCode(), |
|
105 | - $this->calculator(), |
|
106 | - $this->formatters() |
|
107 | - ); |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * factory method that returns a Money object using the currency as specified by the supplied ISO country code |
|
114 | - * example: CreateMoney::forCountry(12.5,'US') |
|
115 | - * |
|
116 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
117 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
118 | - * @param string $CNT_ISO |
|
119 | - * @return Money |
|
120 | - * @throws EE_Error |
|
121 | - * @throws InvalidArgumentException |
|
122 | - * @throws InvalidDataTypeException |
|
123 | - * @throws InvalidInterfaceException |
|
124 | - */ |
|
125 | - public function createForCountry($amount, $CNT_ISO) |
|
126 | - { |
|
127 | - return new Money( |
|
128 | - $amount, |
|
129 | - $this->currency_factory->createFromCountryCode($CNT_ISO), |
|
130 | - $this->calculator(), |
|
131 | - $this->formatters() |
|
132 | - ); |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - |
|
137 | - /** |
|
138 | - * factory method that returns a Money object using the currency as specified by the supplied currency code |
|
139 | - * example: CreateMoney::forCurrency(12.5, 'USD') |
|
140 | - * |
|
141 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
142 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
143 | - * @param string $currency_code |
|
144 | - * @return Money |
|
145 | - * @throws EE_Error |
|
146 | - * @throws InvalidArgumentException |
|
147 | - * @throws InvalidDataTypeException |
|
148 | - * @throws InvalidInterfaceException |
|
149 | - */ |
|
150 | - public function createForCurrency($amount, $currency_code) |
|
151 | - { |
|
152 | - return new Money( |
|
153 | - $amount, |
|
154 | - $this->currency_factory->createFromCode($currency_code), |
|
155 | - $this->calculator(), |
|
156 | - $this->formatters() |
|
157 | - ); |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - |
|
162 | - |
|
163 | - /** |
|
164 | - * @return Calculator |
|
165 | - */ |
|
166 | - public function calculator() |
|
167 | - { |
|
168 | - $this->initializeCalculators(); |
|
169 | - return $this->calculator; |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * loops through a filterable array of Calculator services |
|
176 | - * and selects the first one that is supported by the current server |
|
177 | - */ |
|
178 | - protected function initializeCalculators() |
|
179 | - { |
|
180 | - if ($this->calculator instanceof Calculator) { |
|
181 | - return; |
|
182 | - } |
|
183 | - $calculators = apply_filters( |
|
184 | - 'FHEE__EventEspresso_core_services_currency_MoneyFactory__initializeCalculators__Calculators_array', |
|
185 | - array( |
|
186 | - '\EventEspresso\core\services\currency\DefaultCalculator', |
|
187 | - ) |
|
188 | - ); |
|
189 | - foreach ($calculators as $calculator) { |
|
190 | - if (! class_exists($calculator)) { |
|
191 | - continue; |
|
192 | - } |
|
193 | - $calculator = new $calculator(); |
|
194 | - if ($calculator instanceof Calculator && $calculator->isSupported()) { |
|
195 | - $this->calculator = $calculator; |
|
196 | - break; |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - |
|
202 | - |
|
203 | - /** |
|
204 | - * @return MoneyFormatter[] |
|
205 | - */ |
|
206 | - public function formatters() |
|
207 | - { |
|
208 | - $this->initializeFormatters(); |
|
209 | - return $this->formatters; |
|
210 | - } |
|
211 | - |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * initializes a filterable array of MoneyFormatter services |
|
216 | - */ |
|
217 | - protected function initializeFormatters() |
|
218 | - { |
|
219 | - if (! empty($this->formatters)) { |
|
220 | - return; |
|
221 | - } |
|
222 | - $this->formatters = apply_filters( |
|
223 | - 'FHEE__EventEspresso_core_services_currency_MoneyFactory__initializeFormatters__MoneyFormatters_array', |
|
224 | - array( |
|
225 | - 1 => new DecimalMoneyFormatter(), |
|
226 | - 2 => new ThousandsMoneyFormatter(), |
|
227 | - 3 => new CurrencySignMoneyFormatter(), |
|
228 | - 4 => new CurrencyCodeMoneyFormatter(), |
|
229 | - 5 => new InternationalMoneyFormatter(), |
|
230 | - ) |
|
231 | - ); |
|
232 | - } |
|
26 | + /** |
|
27 | + * @var CurrencyFactory $currency_factory |
|
28 | + */ |
|
29 | + protected $currency_factory; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var Calculator $calculator |
|
33 | + */ |
|
34 | + protected $calculator; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var MoneyFormatter[] $formatters |
|
38 | + */ |
|
39 | + protected $formatters; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * CreateMoney constructor. |
|
44 | + * |
|
45 | + * @param CurrencyFactory $currency_factory |
|
46 | + * @param Calculator $calculator |
|
47 | + * @param MoneyFormatter[] $formatters |
|
48 | + */ |
|
49 | + public function __construct( |
|
50 | + CurrencyFactory $currency_factory, |
|
51 | + Calculator $calculator = null, |
|
52 | + array $formatters = array() |
|
53 | + ) { |
|
54 | + $this->calculator = $calculator; |
|
55 | + $this->formatters = $formatters; |
|
56 | + $this->currency_factory = $currency_factory; |
|
57 | + } |
|
58 | + |
|
59 | + |
|
60 | + |
|
61 | + /** |
|
62 | + * factory method that returns a Money object using amount specified in the currency's subunits |
|
63 | + * example: for $12.50 USD use CreateMoney::fromSubUnits(1250, 'USD') |
|
64 | + * |
|
65 | + * @param int $subunits_amount money amount IN THE SUBUNITS FOR THE CURRENCY ie: cents |
|
66 | + * example: $12.50 USD would equate to a subunits amount of 1250 |
|
67 | + * @param string $currency_code |
|
68 | + * @return Money |
|
69 | + * @throws EE_Error |
|
70 | + * @throws InvalidArgumentException |
|
71 | + * @throws InvalidDataTypeException |
|
72 | + * @throws InvalidInterfaceException |
|
73 | + */ |
|
74 | + public function createFromSubUnits($subunits_amount, $currency_code = '') |
|
75 | + { |
|
76 | + $currency = $this->currency_factory->createFromCode($currency_code); |
|
77 | + return new Money( |
|
78 | + // shift decimal BACK by number of places for currency |
|
79 | + $subunits_amount * pow(10, $currency->decimalPlaces() * -1), |
|
80 | + $currency, |
|
81 | + $this->calculator(), |
|
82 | + $this->formatters() |
|
83 | + ); |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * factory method that returns a Money object using the currency corresponding to the site's country |
|
90 | + * example: CreateMoney::forSite(12.5) |
|
91 | + * |
|
92 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
93 | + * example: $12.5 USD would equate to a standard amount of 12.50 |
|
94 | + * @return Money |
|
95 | + * @throws EE_Error |
|
96 | + * @throws InvalidArgumentException |
|
97 | + * @throws InvalidDataTypeException |
|
98 | + * @throws InvalidInterfaceException |
|
99 | + */ |
|
100 | + public function createForSite($amount) |
|
101 | + { |
|
102 | + return new Money( |
|
103 | + $amount, |
|
104 | + $this->currency_factory->createFromCountryCode(), |
|
105 | + $this->calculator(), |
|
106 | + $this->formatters() |
|
107 | + ); |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * factory method that returns a Money object using the currency as specified by the supplied ISO country code |
|
114 | + * example: CreateMoney::forCountry(12.5,'US') |
|
115 | + * |
|
116 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
117 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
118 | + * @param string $CNT_ISO |
|
119 | + * @return Money |
|
120 | + * @throws EE_Error |
|
121 | + * @throws InvalidArgumentException |
|
122 | + * @throws InvalidDataTypeException |
|
123 | + * @throws InvalidInterfaceException |
|
124 | + */ |
|
125 | + public function createForCountry($amount, $CNT_ISO) |
|
126 | + { |
|
127 | + return new Money( |
|
128 | + $amount, |
|
129 | + $this->currency_factory->createFromCountryCode($CNT_ISO), |
|
130 | + $this->calculator(), |
|
131 | + $this->formatters() |
|
132 | + ); |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + |
|
137 | + /** |
|
138 | + * factory method that returns a Money object using the currency as specified by the supplied currency code |
|
139 | + * example: CreateMoney::forCurrency(12.5, 'USD') |
|
140 | + * |
|
141 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
142 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
143 | + * @param string $currency_code |
|
144 | + * @return Money |
|
145 | + * @throws EE_Error |
|
146 | + * @throws InvalidArgumentException |
|
147 | + * @throws InvalidDataTypeException |
|
148 | + * @throws InvalidInterfaceException |
|
149 | + */ |
|
150 | + public function createForCurrency($amount, $currency_code) |
|
151 | + { |
|
152 | + return new Money( |
|
153 | + $amount, |
|
154 | + $this->currency_factory->createFromCode($currency_code), |
|
155 | + $this->calculator(), |
|
156 | + $this->formatters() |
|
157 | + ); |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + |
|
162 | + |
|
163 | + /** |
|
164 | + * @return Calculator |
|
165 | + */ |
|
166 | + public function calculator() |
|
167 | + { |
|
168 | + $this->initializeCalculators(); |
|
169 | + return $this->calculator; |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * loops through a filterable array of Calculator services |
|
176 | + * and selects the first one that is supported by the current server |
|
177 | + */ |
|
178 | + protected function initializeCalculators() |
|
179 | + { |
|
180 | + if ($this->calculator instanceof Calculator) { |
|
181 | + return; |
|
182 | + } |
|
183 | + $calculators = apply_filters( |
|
184 | + 'FHEE__EventEspresso_core_services_currency_MoneyFactory__initializeCalculators__Calculators_array', |
|
185 | + array( |
|
186 | + '\EventEspresso\core\services\currency\DefaultCalculator', |
|
187 | + ) |
|
188 | + ); |
|
189 | + foreach ($calculators as $calculator) { |
|
190 | + if (! class_exists($calculator)) { |
|
191 | + continue; |
|
192 | + } |
|
193 | + $calculator = new $calculator(); |
|
194 | + if ($calculator instanceof Calculator && $calculator->isSupported()) { |
|
195 | + $this->calculator = $calculator; |
|
196 | + break; |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + |
|
202 | + |
|
203 | + /** |
|
204 | + * @return MoneyFormatter[] |
|
205 | + */ |
|
206 | + public function formatters() |
|
207 | + { |
|
208 | + $this->initializeFormatters(); |
|
209 | + return $this->formatters; |
|
210 | + } |
|
211 | + |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * initializes a filterable array of MoneyFormatter services |
|
216 | + */ |
|
217 | + protected function initializeFormatters() |
|
218 | + { |
|
219 | + if (! empty($this->formatters)) { |
|
220 | + return; |
|
221 | + } |
|
222 | + $this->formatters = apply_filters( |
|
223 | + 'FHEE__EventEspresso_core_services_currency_MoneyFactory__initializeFormatters__MoneyFormatters_array', |
|
224 | + array( |
|
225 | + 1 => new DecimalMoneyFormatter(), |
|
226 | + 2 => new ThousandsMoneyFormatter(), |
|
227 | + 3 => new CurrencySignMoneyFormatter(), |
|
228 | + 4 => new CurrencyCodeMoneyFormatter(), |
|
229 | + 5 => new InternationalMoneyFormatter(), |
|
230 | + ) |
|
231 | + ); |
|
232 | + } |
|
233 | 233 | |
234 | 234 | |
235 | 235 | } |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | ) |
188 | 188 | ); |
189 | 189 | foreach ($calculators as $calculator) { |
190 | - if (! class_exists($calculator)) { |
|
190 | + if ( ! class_exists($calculator)) { |
|
191 | 191 | continue; |
192 | 192 | } |
193 | 193 | $calculator = new $calculator(); |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | */ |
217 | 217 | protected function initializeFormatters() |
218 | 218 | { |
219 | - if (! empty($this->formatters)) { |
|
219 | + if ( ! empty($this->formatters)) { |
|
220 | 220 | return; |
221 | 221 | } |
222 | 222 | $this->formatters = apply_filters( |