1 | <?php |
||
25 | class PluginFactory |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * Configured plugins instances |
||
30 | * @var PluginsStorage |
||
31 | */ |
||
32 | private $instances = null; |
||
33 | |||
34 | /** |
||
35 | * Single plugins instances |
||
36 | * @var Storage\PluginStorage |
||
37 | */ |
||
38 | private $plugins = null; |
||
39 | |||
40 | /** |
||
41 | * EmbeDi instance |
||
42 | * @var EmbeDi |
||
43 | */ |
||
44 | private $di = null; |
||
45 | |||
46 | /** |
||
47 | * Static instances of plugin factories |
||
48 | * @var PluginFactory[] |
||
49 | */ |
||
50 | private static $_pf = []; |
||
51 | |||
52 | /** |
||
53 | * Class constructor with optional instanceid which is passed to EmbeDi |
||
54 | * @param string $instanceId |
||
55 | */ |
||
56 | public function __construct($instanceId = Gazebo::DefaultInstanceId) |
||
62 | |||
63 | /** |
||
64 | * Flyweight accessor for `PluginFactory` with optional instanceid. |
||
65 | * This will create only one runtime wide instance of `PluginFactory` for each `$instanceId`. |
||
66 | * @param string $instanceId |
||
67 | * @return PluginFactory Flyweight instance of PluginFactory |
||
68 | */ |
||
69 | public static function fly($instanceId = Gazebo::DefaultInstanceId) |
||
77 | |||
78 | /** |
||
79 | * Create plugin set from `$configuration` for `$object` |
||
80 | * optionally implementing one or more `$interfaces`. |
||
81 | * |
||
82 | * @param mixed[][] $configuration Configuration arrays |
||
83 | * @param string|object $object Object or class name |
||
84 | * @param null|string|string[] $interfaces Array or string of interface names or class names |
||
85 | * @return object[] Array of plugin instances |
||
86 | */ |
||
87 | public function create($configuration, $object, $interfaces = null) |
||
96 | |||
97 | /** |
||
98 | * Get instance of plugin set from `$config` for `$object` |
||
99 | * optionally implementing one or more `$interfaces`. |
||
100 | * |
||
101 | * This will create instances unique for each object and interfaces set. |
||
102 | * This will create only **one instance** of each plugin per config. |
||
103 | * |
||
104 | * @param mixed[][] $config |
||
105 | * @param string|object $object |
||
106 | * @param null|string|string[] $interfaces |
||
107 | * @return object[] Array of plugin instances |
||
108 | */ |
||
109 | public function instance($config, $object, $interfaces = null) |
||
110 | { |
||
111 | if (is_string($object)) |
||
112 | { |
||
113 | $key = $object; |
||
114 | } |
||
115 | else |
||
116 | { |
||
117 | $key = get_class($object); |
||
118 | } |
||
119 | if (null !== $interfaces) |
||
120 | { |
||
121 | if (!is_array($interfaces)) |
||
122 | { |
||
123 | $interfaces = [ |
||
124 | (string) $interfaces |
||
125 | ]; |
||
126 | } |
||
127 | $key .= '.' . implode('.', $interfaces); |
||
128 | } |
||
129 | $key .= $this->getKey($config); |
||
130 | if (!isset($this->instances[$key])) |
||
131 | { |
||
132 | $plugins = []; |
||
133 | foreach ($this->_getConfigs($config, $object, $interfaces) as $config) |
||
|
|||
134 | { |
||
135 | $plugins[] = $this->_instantiate($config, true); |
||
136 | } |
||
137 | $this->instances[$key] = $plugins; |
||
138 | } |
||
139 | return $this->instances[$key]; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Check if object or class implements one or more interfaces |
||
144 | * @param object|string $object |
||
145 | * @param null|string|string[] $interfaces Interfaces to check |
||
146 | * @return boolean |
||
147 | */ |
||
148 | private function _implements($object, $interfaces = null) |
||
179 | |||
180 | /** |
||
181 | * Get class name from configuration |
||
182 | * @param string|mixed[] $config |
||
183 | * @return string |
||
184 | */ |
||
185 | private function _getClassName($config) |
||
193 | |||
194 | /** |
||
195 | * Config generator |
||
196 | * |
||
197 | * @param mixed[][] $configuration Configuration arrays |
||
198 | * @param string|object $object Object or class name |
||
199 | * @param null|string|string[] $interfaces Array or string of interface names or class names |
||
200 | * @return mixed[] Array of plugin configs |
||
201 | * @yield mixed[] Array of plugin configs |
||
202 | */ |
||
203 | private function _getConfigs($configuration, $object, $interfaces = null) |
||
230 | |||
231 | /** |
||
232 | * Instantiate object based on configuration |
||
233 | * @param string|mixed[] $config |
||
234 | * @return object |
||
235 | */ |
||
236 | private function _instantiate($config, $fly = false) |
||
261 | |||
262 | private function getKey($config) |
||
280 | |||
281 | } |
||
282 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.