1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\loaders; |
4
|
|
|
|
5
|
|
|
use EE_Registry; |
6
|
|
|
use EventEspresso\core\services\container\CoffeeShop; |
7
|
|
|
use EventEspresso\core\services\container\exceptions\ServiceNotFoundException; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class CoreLoader |
16
|
|
|
* Currently uses EE_Registry for instantiating classes, |
17
|
|
|
* but will later be replaced by the CoffeeShop DI container |
18
|
|
|
* |
19
|
|
|
* @package Event Espresso |
20
|
|
|
* @author Brent Christensen |
21
|
|
|
* @since $VID:$ |
22
|
|
|
*/ |
23
|
|
|
class CoreLoader implements LoaderDecoratorInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var EE_Registry|CoffeeShop $generator |
28
|
|
|
*/ |
29
|
|
|
private $generator; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* CoreLoader constructor. |
35
|
|
|
* |
36
|
|
|
* @param EE_Registry|CoffeeShop $generator |
37
|
|
|
* @throws InvalidArgumentException |
38
|
|
|
*/ |
39
|
|
|
public function __construct($generator) |
40
|
|
|
{ |
41
|
|
|
if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) { |
42
|
|
|
throw new InvalidArgumentException( |
43
|
|
|
esc_html__( |
44
|
|
|
'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.', |
45
|
|
|
'event_espresso' |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
$this->generator = $generator; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $fqcn |
56
|
|
|
* @param array $arguments |
57
|
|
|
* @return mixed |
58
|
|
|
* @throws ServiceNotFoundException |
59
|
|
|
*/ |
60
|
|
|
public function load($fqcn, $arguments = array()) |
61
|
|
|
{ |
62
|
|
|
$object = $this->generator instanceof EE_Registry |
63
|
|
|
? $this->generator->create($fqcn, $arguments) |
64
|
|
|
: $this->generator->brew($fqcn, $arguments); |
65
|
|
|
// if we did NOT receive an instance of the requested object from EE_Registry |
66
|
|
|
if(! $object instanceof $fqcn && $this->generator instanceof EE_Registry) { |
67
|
|
|
// then we need to try some of these other loading methods |
68
|
|
|
$alternate_loaders = array( |
69
|
|
|
'load_core', |
70
|
|
|
'load_class', |
71
|
|
|
'load_model', |
72
|
|
|
'load_helper', |
73
|
|
|
'load_lib', |
74
|
|
|
'load_model_class', |
75
|
|
|
'load_service', |
76
|
|
|
'load_dms', |
77
|
|
|
); |
78
|
|
|
foreach ($alternate_loaders as $alternate_loader) { |
79
|
|
|
$object = $this->generator->{$alternate_loader}($fqcn, $arguments); |
80
|
|
|
// but then break out of the loop as soon as we find what we are looking for |
81
|
|
|
if ($object instanceof $fqcn) { |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
return $object; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* calls reset() on generator if method exists |
93
|
|
|
*/ |
94
|
|
|
public function reset() |
95
|
|
|
{ |
96
|
|
|
if (method_exists($this->generator, 'reset')) { |
97
|
|
|
$this->generator->reset(); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
// End of file CoreLoader.php |
103
|
|
|
// Location: core/services/loaders/CoreLoader.php |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: