-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathFactory.php
253 lines (231 loc) · 8.85 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\ObjectManager;
use Magento\FunctionalTestingFramework\System\Code\ClassReader;
/**
* Class Factory
*
* @internal
*/
class Factory extends \Magento\FunctionalTestingFramework\ObjectManager\Factory\Dynamic\Developer
{
/**
* Class reader.
*
* @var \Magento\FunctionalTestingFramework\System\Code\ClassReader
*/
protected $classReader;
/**
* Factory constructor.
* @param ConfigInterface $config
* @param \Magento\FunctionalTestingFramework\ObjectManagerInterface|null $objectManager
* @param DefinitionInterface|null $definitions
* @param array $globalArguments
*/
public function __construct(
ConfigInterface $config,
\Magento\FunctionalTestingFramework\ObjectManagerInterface $objectManager = null,
DefinitionInterface $definitions = null,
$globalArguments = []
) {
parent::__construct($config, $objectManager, $definitions, $globalArguments);
$this->classReader = new ClassReader();
}
// @codingStandardsIgnoreStart
/**
* Invoke class method and prepared arguments
*
* @param mixed $object
* @param string $method
* @param array $args
* @return mixed
*/
public function invoke($object, $method, array $args = [])
{
$args = $this->prepareArguments($object, $method, $args);
$type = get_class($object);
$class = new \ReflectionClass($type);
$method = $class->getMethod($method);
return $method->invokeArgs($object, $args);
}
// @codingStandardsIgnoreEnd
/**
* Get list of parameters for class method
*
* @param string $type
* @param string $method
* @return array|null
*/
public function getParameters($type, $method)
{
return $this->classReader->getParameters($type, $method);
}
/**
* Resolve and prepare arguments for class method
*
* @param object $object
* @param string $method
* @param array $arguments
* @return array
*/
public function prepareArguments($object, $method, array $arguments = [])
{
$type = get_class($object);
$parameters = $this->classReader->getParameters($type, $method);
if ($parameters === null) {
return [];
}
return $this->resolveArguments($type, $parameters, $arguments);
}
/**
* Resolve constructor arguments
*
* @param string $requestedType
* @param array $parameters
* @param array $arguments
* @return array
* @throws \UnexpectedValueException
* @throws \BadMethodCallException
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* Revisited to reduce cyclomatic complexity, left unrefactored for readability
*/
protected function resolveArguments($requestedType, array $parameters, array $arguments = [])
{
$resolvedArguments = [];
$arguments = count($arguments)
? array_replace($this->config->getArguments($requestedType), $arguments)
: $this->config->getArguments($requestedType);
foreach ($parameters as $parameter) {
list($paramName, $paramType, $paramRequired, $paramDefault) = $parameter;
$argument = null;
if (array_key_exists($paramName, $arguments)) {
$argument = $arguments[$paramName];
} elseif (array_key_exists('options', $arguments) && array_key_exists($paramName, $arguments['options'])) {
// The parameter name doesn't exist in the arguments, but it is contained in the 'options' argument.
$argument = $arguments['options'][$paramName];
} else {
if ($paramRequired) {
if ($paramType) {
$argument = ['instance' => $paramType];
} else {
$this->creationStack = [];
throw new \BadMethodCallException(
'Missing required argument $' . $paramName . ' of ' . $requestedType . '.'
);
}
} else {
$argument = $paramDefault;
}
}
if ($paramType && !is_object($argument) && $argument !== $paramDefault) {
if (!is_array($argument)) {
throw new \UnexpectedValueException(
'Invalid parameter configuration provided for $' . $paramName . ' argument of ' . $requestedType
);
}
if (isset($argument['instance']) && !empty($argument['instance'])) {
$argumentType = $argument['instance'];
unset($argument['instance']);
if (array_key_exists('shared', $argument)) {
$isShared = $argument['shared'];
unset($argument['shared']);
} else {
$isShared = $this->config->isShared($argumentType);
}
} else {
$argumentType = $paramType;
$isShared = $this->config->isShared($argumentType);
}
$_arguments = !empty($argument) ? $argument : [];
$argument = $isShared
? $this->objectManager->get($argumentType)
: $this->objectManager->create($argumentType, $_arguments);
} else {
if (is_array($argument)) {
if (isset($argument['argument'])) {
$argKey = $argument['argument'];
$argument = isset($this->globalArguments[$argKey])
? $this->globalArguments[$argKey]
: $paramDefault;
} else {
$this->parseArray($argument);
}
}
}
$resolvedArguments[$paramName] = $argument;
}
return $resolvedArguments;
}
/**
* Parse array argument
*
* @param array $array
* @return void
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* Revisited to reduce cyclomatic complexity, left unrefactored for readability
*/
protected function parseArray(&$array)
{
foreach ($array as $key => $item) {
if (!is_array($item)) {
continue;
}
if (isset($item['instance'])) {
$itemType = $item['instance'];
$isShared = isset($item['shared']) ? $item['shared'] : $this->config->isShared($itemType);
unset($item['instance']);
if (array_key_exists('shared', $item)) {
unset($item['shared']);
}
$_arguments = !empty($item) ? $item : [];
$array[$key] = $isShared
? $this->objectManager->get($itemType)
: $this->objectManager->create($itemType, $_arguments);
} elseif (isset($item['argument'])) {
$array[$key] = isset($this->globalArguments[$item['argument']])
? $this->globalArguments[$item['argument']]
: null;
} else {
$this->parseArray($item);
}
}
}
/**
* Create instance with call time arguments
*
* @param string $requestedType
* @param array $arguments
* @return object
* @throws \Exception
*/
public function create($requestedType, array $arguments = [])
{
$instanceType = $this->config->getInstanceType($requestedType);
$parameters = $this->definitions->getParameters($instanceType);
if ($parameters === null) {
return new $instanceType();
}
if (isset($this->creationStack[$requestedType])) {
$lastFound = end($this->creationStack);
$this->creationStack = [];
throw new \LogicException("Circular dependency: {$requestedType} depends on {$lastFound} and vice versa.");
}
$this->creationStack[$requestedType] = $requestedType;
try {
$args = $this->resolveArguments($requestedType, $parameters, $arguments);
unset($this->creationStack[$requestedType]);
} catch (\Exception $e) {
unset($this->creationStack[$requestedType]);
throw $e;
}
$reflection = new \ReflectionClass($instanceType);
return $reflection->newInstanceArgs($args);
}
}