11
11
use Psr \Log \LoggerInterface ;
12
12
use Magento \Framework \App \ObjectManager ;
13
13
14
+ /**
15
+ * Class AbstractFactory
16
+ */
14
17
abstract class AbstractFactory implements \Magento \Framework \ObjectManager \FactoryInterface
15
18
{
16
19
/**
@@ -49,10 +52,10 @@ abstract class AbstractFactory implements \Magento\Framework\ObjectManager\Facto
49
52
protected $ creationStack = [];
50
53
51
54
/**
52
- * @param \Magento\Framework\ObjectManager\ConfigInterface $config
53
- * @param ObjectManagerInterface $objectManager
55
+ * @param \Magento\Framework\ObjectManager\ConfigInterface $config
56
+ * @param ObjectManagerInterface $objectManager
54
57
* @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions
55
- * @param array $globalArguments
58
+ * @param array $globalArguments
56
59
*/
57
60
public function __construct (
58
61
\Magento \Framework \ObjectManager \ConfigInterface $ config ,
@@ -91,6 +94,8 @@ public function setArguments($arguments)
91
94
}
92
95
93
96
/**
97
+ * Get definitions
98
+ *
94
99
* @return \Magento\Framework\ObjectManager\DefinitionInterface
95
100
*/
96
101
public function getDefinitions ()
@@ -105,7 +110,7 @@ public function getDefinitions()
105
110
* Create object
106
111
*
107
112
* @param string $type
108
- * @param array $args
113
+ * @param array $args
109
114
*
110
115
* @return object
111
116
* @throws RuntimeException
@@ -115,7 +120,9 @@ protected function createObject($type, $args)
115
120
try {
116
121
return new $ type (...array_values ($ args ));
117
122
} catch (\TypeError $ exception ) {
118
- /** @var LoggerInterface $logger */
123
+ /**
124
+ * @var LoggerInterface $logger
125
+ */
119
126
$ logger = ObjectManager::getInstance ()->get (LoggerInterface::class);
120
127
$ logger ->critical (
121
128
sprintf ('Type Error occurred when creating object: %s, %s ' , $ type , $ exception ->getMessage ())
@@ -130,9 +137,9 @@ protected function createObject($type, $args)
130
137
/**
131
138
* Resolve an argument
132
139
*
133
- * @param array & $argument
140
+ * @param array $argument
134
141
* @param string $paramType
135
- * @param mixed $paramDefault
142
+ * @param mixed $paramDefault
136
143
* @param string $paramName
137
144
* @param string $requestedType
138
145
*
@@ -214,8 +221,8 @@ protected function parseArray(&$array)
214
221
* Resolve constructor arguments
215
222
*
216
223
* @param string $requestedType
217
- * @param array $parameters
218
- * @param array $arguments
224
+ * @param array $parameters
225
+ * @param array $arguments
219
226
*
220
227
* @return array
221
228
*
@@ -226,27 +233,44 @@ protected function resolveArgumentsInRuntime($requestedType, array $parameters,
226
233
{
227
234
$ resolvedArguments = [];
228
235
foreach ($ parameters as $ parameter ) {
229
- list ($ paramName , $ paramType , $ paramRequired , $ paramDefault ) = $ parameter ;
230
- $ argument = null ;
231
- if (!empty ($ arguments ) && (isset ($ arguments [$ paramName ]) || array_key_exists ($ paramName , $ arguments ))) {
232
- $ argument = $ arguments [$ paramName ];
233
- } elseif ($ paramRequired ) {
234
- if ($ paramType ) {
235
- $ argument = ['instance ' => $ paramType ];
236
- } else {
237
- $ this ->creationStack = [];
238
- throw new \BadMethodCallException (
239
- 'Missing required argument $ ' . $ paramName . ' of ' . $ requestedType . '. '
240
- );
241
- }
236
+ $ resolvedArguments [] = $ this ->getResolvedArgument ((string )$ requestedType , $ parameter , $ arguments );
237
+ }
238
+
239
+ return empty ($ resolvedArguments ) ? [] : array_merge (...$ resolvedArguments );
240
+ }
241
+
242
+ /**
243
+ * Get resolved argument from parameter
244
+ *
245
+ * @param string $requestedType
246
+ * @param array $parameter
247
+ * @param array $arguments
248
+ * @return array
249
+ */
250
+ private function getResolvedArgument (string $ requestedType , array $ parameter , array $ arguments ): array
251
+ {
252
+ list ($ paramName , $ paramType , $ paramRequired , $ paramDefault , $ isVariadic ) = $ parameter ;
253
+ $ argument = null ;
254
+ if (!empty ($ arguments ) && (isset ($ arguments [$ paramName ]) || array_key_exists ($ paramName , $ arguments ))) {
255
+ $ argument = $ arguments [$ paramName ];
256
+ } elseif ($ paramRequired ) {
257
+ if ($ paramType ) {
258
+ $ argument = ['instance ' => $ paramType ];
242
259
} else {
243
- $ argument = $ paramDefault ;
260
+ $ this ->creationStack = [];
261
+ throw new \BadMethodCallException (
262
+ 'Missing required argument $ ' . $ paramName . ' of ' . $ requestedType . '. '
263
+ );
244
264
}
265
+ } else {
266
+ $ argument = $ paramDefault ;
267
+ }
245
268
246
- $ this ->resolveArgument ($ argument , $ paramType , $ paramDefault , $ paramName , $ requestedType );
247
-
248
- $ resolvedArguments [] = $ argument ;
269
+ if ($ isVariadic ) {
270
+ return is_array ($ argument ) ? $ argument : [$ argument ];
249
271
}
250
- return $ resolvedArguments ;
272
+
273
+ $ this ->resolveArgument ($ argument , $ paramType , $ paramDefault , $ paramName , $ requestedType );
274
+ return [$ argument ];
251
275
}
252
276
}
0 commit comments