|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\loaders; |
|
4
|
|
|
|
|
5
|
|
|
use EE_Error; |
|
6
|
|
|
use EE_Registry; |
|
7
|
|
|
use EventEspresso\core\exceptions\InvalidClassException; |
|
8
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
9
|
|
|
use EventEspresso\core\exceptions\InvalidIdentifierException; |
|
10
|
|
|
use EventEspresso\core\services\container\CoffeeMaker; |
|
11
|
|
|
use EventEspresso\core\services\container\CoffeeShop; |
|
12
|
|
|
use EventEspresso\core\services\container\exceptions\InstantiationException; |
|
13
|
|
|
use EventEspresso\core\services\container\exceptions\ServiceExistsException; |
|
14
|
|
|
use EventEspresso\core\services\container\exceptions\ServiceNotFoundException; |
|
15
|
|
|
use InvalidArgumentException; |
|
16
|
|
|
use OutOfBoundsException; |
|
17
|
|
|
use ReflectionException; |
|
18
|
|
|
|
|
19
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class CoreLoader |
|
25
|
|
|
* Currently uses EE_Registry for instantiating classes, |
|
26
|
|
|
* but will later be replaced by the CoffeeShop DI container |
|
27
|
|
|
* |
|
28
|
|
|
* @package Event Espresso |
|
29
|
|
|
* @author Brent Christensen |
|
30
|
|
|
* @since $VID:$ |
|
31
|
|
|
*/ |
|
32
|
|
|
class CoreLoader implements LoaderDecoratorInterface |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var EE_Registry|CoffeeShop $generator |
|
37
|
|
|
*/ |
|
38
|
|
|
private $generator; |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* CoreLoader constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param EE_Registry|CoffeeShop $generator |
|
46
|
|
|
* @throws InvalidArgumentException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($generator) |
|
49
|
|
|
{ |
|
50
|
|
|
if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) { |
|
51
|
|
|
throw new InvalidArgumentException( |
|
52
|
|
|
esc_html__( |
|
53
|
|
|
'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.', |
|
54
|
|
|
'event_espresso' |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
$this->generator = $generator; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Calls the appropriate loading method from the installed generator; |
|
65
|
|
|
* If EE_Registry is being used, then the additional parameters for the EE_Registry::create() method |
|
66
|
|
|
* can be added to the $arguments array and they will be extracted and passed to EE_Registry::create(), |
|
67
|
|
|
* but NOT to the class being instantiated. |
|
68
|
|
|
* This is done by adding the parameters to the $arguments array as follows: |
|
69
|
|
|
* array( |
|
70
|
|
|
* 'EE_Registry::create(from_db)' => true, // boolean value, default = false |
|
71
|
|
|
* 'EE_Registry::create(load_only)' => true, // boolean value, default = false |
|
72
|
|
|
* 'EE_Registry::create(addon)' => true, // boolean value, default = false |
|
73
|
|
|
* ) |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $fqcn |
|
76
|
|
|
* @param array $arguments |
|
77
|
|
|
* @param bool $shared |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
* @throws OutOfBoundsException |
|
80
|
|
|
* @throws ServiceExistsException |
|
81
|
|
|
* @throws InstantiationException |
|
82
|
|
|
* @throws InvalidIdentifierException |
|
83
|
|
|
* @throws InvalidDataTypeException |
|
84
|
|
|
* @throws InvalidClassException |
|
85
|
|
|
* @throws EE_Error |
|
86
|
|
|
* @throws ServiceNotFoundException |
|
87
|
|
|
* @throws ReflectionException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function load($fqcn, $arguments = array(), $shared = true) |
|
90
|
|
|
{ |
|
91
|
|
|
$shared = filter_var($shared, FILTER_VALIDATE_BOOLEAN); |
|
92
|
|
|
if($this->generator instanceof EE_Registry) { |
|
93
|
|
|
// check if additional EE_Registry::create() arguments have been passed |
|
94
|
|
|
// from_db |
|
95
|
|
|
$from_db = isset($arguments['EE_Registry::create(from_db)']) |
|
96
|
|
|
? filter_var($arguments['EE_Registry::create(from_db)'], FILTER_VALIDATE_BOOLEAN) |
|
97
|
|
|
: false; |
|
98
|
|
|
// load_only |
|
99
|
|
|
$load_only = isset($arguments['EE_Registry::create(load_only)']) |
|
100
|
|
|
? filter_var($arguments['EE_Registry::create(load_only)'], FILTER_VALIDATE_BOOLEAN) |
|
101
|
|
|
: false; |
|
102
|
|
|
// addon |
|
103
|
|
|
$addon = isset($arguments['EE_Registry::create(addon)']) |
|
104
|
|
|
? filter_var($arguments['EE_Registry::create(addon)'], FILTER_VALIDATE_BOOLEAN) |
|
105
|
|
|
: false; |
|
106
|
|
|
unset( |
|
107
|
|
|
$arguments['EE_Registry::create(from_db)'], |
|
108
|
|
|
$arguments['EE_Registry::create(load_only)'], |
|
109
|
|
|
$arguments['EE_Registry::create(addon)'] |
|
110
|
|
|
); |
|
111
|
|
|
// addons need to be cached on EE_Registry |
|
112
|
|
|
$shared = $addon ? true : $shared; |
|
113
|
|
|
return $this->generator->create( |
|
114
|
|
|
$fqcn, |
|
115
|
|
|
$arguments, |
|
116
|
|
|
$shared, |
|
117
|
|
|
$from_db, |
|
118
|
|
|
$load_only, |
|
119
|
|
|
$addon |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
return $this->generator->brew( |
|
123
|
|
|
$fqcn, |
|
124
|
|
|
$arguments, |
|
125
|
|
|
$shared ? CoffeeMaker::BREW_SHARED : CoffeeMaker::BREW_NEW |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* calls reset() on generator if method exists |
|
134
|
|
|
* |
|
135
|
|
|
* @throws EE_Error |
|
136
|
|
|
* @throws ReflectionException |
|
137
|
|
|
*/ |
|
138
|
|
|
public function reset() |
|
139
|
|
|
{ |
|
140
|
|
|
if (method_exists($this->generator, 'reset')) { |
|
141
|
|
|
$this->generator->reset(); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
// End of file CoreLoader.php |
|
147
|
|
|
// Location: core/services/loaders/CoreLoader.php |
|
148
|
|
|
|
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: