1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\loaders; |
4
|
|
|
|
5
|
|
|
use EE_Error; |
6
|
|
|
use EE_Registry; |
7
|
|
|
use EventEspresso\core\services\container\CoffeeShop; |
8
|
|
|
use EventEspresso\core\services\container\exceptions\ServiceNotFoundException; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class CoreLoader |
17
|
|
|
* Currently uses EE_Registry for instantiating classes, |
18
|
|
|
* but will later be replaced by the CoffeeShop DI container |
19
|
|
|
* |
20
|
|
|
* @package Event Espresso |
21
|
|
|
* @author Brent Christensen |
22
|
|
|
* @since $VID:$ |
23
|
|
|
*/ |
24
|
|
|
class CoreLoader implements LoaderDecoratorInterface |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var EE_Registry|CoffeeShop $generator |
29
|
|
|
*/ |
30
|
|
|
private $generator; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* CoreLoader constructor. |
36
|
|
|
* |
37
|
|
|
* @param EE_Registry|CoffeeShop $generator |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
*/ |
40
|
|
|
public function __construct($generator) |
41
|
|
|
{ |
42
|
|
|
if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) { |
43
|
|
|
throw new InvalidArgumentException( |
44
|
|
|
esc_html__( |
45
|
|
|
'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.', |
46
|
|
|
'event_espresso' |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
$this->generator = $generator; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $fqcn |
57
|
|
|
* @param array $arguments |
58
|
|
|
* @return mixed |
59
|
|
|
* @throws EE_Error |
60
|
|
|
* @throws ServiceNotFoundException |
61
|
|
|
*/ |
62
|
|
|
public function load($fqcn, $arguments = array()) |
63
|
|
|
{ |
64
|
|
|
return $this->generator instanceof EE_Registry |
65
|
|
|
? $this->generator->create($fqcn, $arguments) |
66
|
|
|
: $this->generator->brew($fqcn, $arguments); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* calls reset() on generator if method exists |
73
|
|
|
*/ |
74
|
|
|
public function reset() |
75
|
|
|
{ |
76
|
|
|
if (method_exists($this->generator, 'reset')) { |
77
|
|
|
$this->generator->reset(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
// End of file CoreLoader.php |
83
|
|
|
// Location: core/services/loaders/CoreLoader.php |
84
|
|
|
|
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: