|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\loaders; |
|
4
|
|
|
|
|
5
|
|
|
use EE_Registry; |
|
6
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
7
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
|
8
|
|
|
use EventEspresso\core\services\collections\LooseCollection; |
|
9
|
|
|
use EventEspresso\core\services\container\CoffeeShop; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
|
|
12
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class LoaderFactory |
|
17
|
|
|
* All hard dependencies should be constructor injected wherever possible. |
|
18
|
|
|
* But other times, objects need to be created dynamically within another class |
|
19
|
|
|
* after some decision has been made, or relevant data has been collected. |
|
20
|
|
|
* If the constructors for the classes being generated have injected dependencies themselves, |
|
21
|
|
|
* then we want to use the Loader class for creating the classes as opposed to using the "new" keyword, |
|
22
|
|
|
* because using the Loader will ensure that all of the dependencies will get automatically injected. |
|
23
|
|
|
* So then the issue becomes HOW to get access to the Loader !?!? |
|
24
|
|
|
* First off, we do NOT want to be injecting the Loader all over the place, |
|
25
|
|
|
* because this is considered to be an anti pattern called "Service Location", |
|
26
|
|
|
* so instead we want to use Factory classes anywhere we would need to use the "new" keyword, |
|
27
|
|
|
* and then inject the factories we need into our other classes. |
|
28
|
|
|
* It is considered acceptable to inject the Loader into these factories. |
|
29
|
|
|
* But since the Loader would now be a hard dependency for these factories, |
|
30
|
|
|
* it too should be injected into the constructor just like any other hard dependency. |
|
31
|
|
|
* However, since our dependency tree is not yet complete at this moment, |
|
32
|
|
|
* at some point a class would need to use the "new" keyword to generate the Loader. |
|
33
|
|
|
* With most other classes this wouldn't be an issue, but since the Loader needs to be able to provide |
|
34
|
|
|
* shared instances of classes, we do NOT ever want more than one copy of the Loader floating around. |
|
35
|
|
|
* And that's the purpose of this class. |
|
36
|
|
|
* This class is a temporary measure for providing access to the ONE single instance of the Loader. |
|
37
|
|
|
* The Loader should still be injected into your factory class constructor |
|
38
|
|
|
* so that it can ultimately be provided via the dependency tree, |
|
39
|
|
|
* but it should not YET be required, and a class property should be created for holding the Loader. |
|
40
|
|
|
* ie: |
|
41
|
|
|
* |
|
42
|
|
|
* private $loader; |
|
43
|
|
|
* |
|
44
|
|
|
* public function __construct(LoaderInterface $loader = null) |
|
45
|
|
|
* { |
|
46
|
|
|
* $this->loader = $loader; |
|
47
|
|
|
* } |
|
48
|
|
|
* |
|
49
|
|
|
* The getter for the Loader should check the instance to see if injection was successful, |
|
50
|
|
|
* and if not, use THIS class to obtain the ONE single instance of the Loader. |
|
51
|
|
|
* ie: |
|
52
|
|
|
* |
|
53
|
|
|
* private function getLoader() |
|
54
|
|
|
* { |
|
55
|
|
|
* if (! $this->loader instanceof LoaderInterface) { |
|
56
|
|
|
* $this->loader = LoaderFactory::getLoader(); |
|
57
|
|
|
* } |
|
58
|
|
|
* return $this->loader; |
|
59
|
|
|
* } |
|
60
|
|
|
* |
|
61
|
|
|
* Then the methods in your factory class that generate their target classes, |
|
62
|
|
|
* can use this getter to obtain the Loader to use for generating objects. |
|
63
|
|
|
* ie: |
|
64
|
|
|
* |
|
65
|
|
|
* public function createSomeObject(array $arguments = array()) |
|
66
|
|
|
* { |
|
67
|
|
|
* return $this->loader->getNew('fully/qualified/ClassName', $arguments); |
|
68
|
|
|
* } |
|
69
|
|
|
* |
|
70
|
|
|
* Eventually, when our dependency tree is complete |
|
71
|
|
|
* and ALL class construction can be traced back to the DI container, |
|
72
|
|
|
* then any injected factories can obtain their instance of the Loader via their constructor, |
|
73
|
|
|
* and this LoaderFactory (and the need to have getLoader() methods that use it) |
|
74
|
|
|
* will be completely unnecessary and can be removed. |
|
75
|
|
|
* |
|
76
|
|
|
* @package Event Espresso |
|
77
|
|
|
* @author Brent Christensen |
|
78
|
|
|
* @since 4.9.44 |
|
79
|
|
|
*/ |
|
80
|
|
|
class LoaderFactory |
|
81
|
|
|
{ |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var LoaderInterface $loader ; |
|
85
|
|
|
*/ |
|
86
|
|
|
private static $loader; |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param EE_Registry|CoffeeShop $generator provided during very first instantiation in |
|
91
|
|
|
* BootstrapDependencyInjectionContainer::buildLoader() |
|
92
|
|
|
* otherwise can be left null |
|
93
|
|
|
* @param ClassInterfaceCache|null $class_cache also provided during first instantiation |
|
94
|
|
|
* @param ObjectIdentifier|null $object_identifier |
|
95
|
|
|
* @return LoaderInterface |
|
96
|
|
|
* @throws InvalidArgumentException |
|
97
|
|
|
* @throws InvalidDataTypeException |
|
98
|
|
|
* @throws InvalidInterfaceException |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function getLoader( |
|
101
|
|
|
$generator = null, |
|
102
|
|
|
ClassInterfaceCache $class_cache = null, |
|
103
|
|
|
ObjectIdentifier $object_identifier = null |
|
104
|
|
|
) { |
|
105
|
|
|
if ( |
|
106
|
|
|
! LoaderFactory::$loader instanceof LoaderInterface |
|
107
|
|
|
&& ($generator instanceof EE_Registry || $generator instanceof CoffeeShop) |
|
108
|
|
|
&& $class_cache instanceof ClassInterfaceCache |
|
109
|
|
|
&& $object_identifier instanceof ObjectIdentifier |
|
110
|
|
|
) { |
|
111
|
|
|
$core_loader = new CoreLoader($generator); |
|
112
|
|
|
LoaderFactory::$loader = new Loader( |
|
113
|
|
|
$core_loader, |
|
114
|
|
|
new CachingLoader( |
|
115
|
|
|
$core_loader, |
|
116
|
|
|
new LooseCollection(''), |
|
117
|
|
|
$object_identifier |
|
118
|
|
|
), |
|
119
|
|
|
$class_cache |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
return LoaderFactory::$loader; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.