forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeveloper.php
213 lines (200 loc) · 7.43 KB
/
Developer.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\ObjectManager\Factory\Dynamic;
/**
* Class Developer
*/
class Developer implements \Magento\FunctionalTestingFramework\ObjectManager\FactoryInterface
{
/**
* Object manager
*
* @var \Magento\FunctionalTestingFramework\ObjectManagerInterface
*/
protected $objectManager;
/**
* Object manager config
*
* @var \Magento\FunctionalTestingFramework\ObjectManager\ConfigInterface
*/
protected $config;
/**
* Definition list
*
* @var \Magento\FunctionalTestingFramework\ObjectManager\DefinitionInterface
*/
protected $definitions;
/**
* Object creation stack
*
* @var array
*/
protected $creationStack = [];
/**
* Global arguments.
*
* @var array
*/
protected $globalArguments;
/**
* Developer constructor.
* @param \Magento\FunctionalTestingFramework\ObjectManager\ConfigInterface $config
* @param \Magento\FunctionalTestingFramework\ObjectManagerInterface|null $objectManager
* @param \Magento\FunctionalTestingFramework\ObjectManager\DefinitionInterface|null $definitions
* @param array $globalArguments
*/
public function __construct(
\Magento\FunctionalTestingFramework\ObjectManager\ConfigInterface $config,
\Magento\FunctionalTestingFramework\ObjectManagerInterface $objectManager = null,
\Magento\FunctionalTestingFramework\ObjectManager\DefinitionInterface $definitions = null,
$globalArguments = []
) {
$this->config = $config;
$this->objectManager = $objectManager;
$this->definitions = $definitions ?: new \Magento\FunctionalTestingFramework\ObjectManager\Definition\Runtime();
$this->globalArguments = $globalArguments;
}
/**
* Set object manager
*
* @param \Magento\FunctionalTestingFramework\ObjectManagerInterface $objectManager
* @return void
*/
public function setObjectManager(\Magento\FunctionalTestingFramework\ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* 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 (!empty($arguments) && (isset($arguments[$paramName]) || array_key_exists($paramName, $arguments))) {
$argument = $arguments[$paramName];
} elseif ($paramRequired) {
if ($paramType) {
$argument = ['instance' => $paramType];
} else {
$this->creationStack = [];
throw new \BadMethodCallException(
'Missing required argument $' . $paramName . ' of ' . $requestedType . '.'
);
}
} else {
$argument = $paramDefault;
}
if ($paramType && $argument !== $paramDefault && !is_object($argument)) {
if (!isset($argument['instance']) || !is_array($argument)) {
throw new \UnexpectedValueException(
'Invalid parameter configuration provided for $' . $paramName . ' argument of ' . $requestedType
);
}
$argumentType = $argument['instance'];
$isShared = (isset($argument['shared']) ? $argument['shared'] : $this->config->isShared($argumentType));
$argument = $isShared
? $this->objectManager->get($argumentType)
: $this->objectManager->create($argumentType);
} elseif (is_array($argument)) {
if (isset($argument['argument'])) {
$argument = isset($this->globalArguments[$argument['argument']])
? $this->globalArguments[$argument['argument']]
: $paramDefault;
} elseif (!empty($argument)) {
$this->parseArray($argument);
}
}
$resolvedArguments[] = $argument;
}
return $resolvedArguments;
}
/**
* Parse array argument
*
* @param array $array
* @return void
*/
protected function parseArray(&$array)
{
foreach ($array as $key => $item) {
if (is_array($item)) {
if (isset($item['instance'])) {
$itemType = $item['instance'];
$isShared = (isset($item['shared'])) ? $item['shared'] : $this->config->isShared($itemType);
$array[$key] = $isShared
? $this->objectManager->get($itemType)
: $this->objectManager->create($itemType);
} elseif (isset($item['argument'])) {
$array[$key] = isset($this->globalArguments[$item['argument']])
? $this->globalArguments[$item['argument']]
: null;
} else {
$this->parseArray($array[$key]);
}
}
}
}
/**
* Create instance with call time arguments
*
* @param string $requestedType
* @param array $arguments
* @return object
* @throws \Exception
*
* @SuppressWarnings(PHPCPD)
*/
public function create($requestedType, array $arguments = [])
{
$type = $this->config->getInstanceType($requestedType);
$parameters = $this->definitions->getParameters($type);
if ($parameters == null) {
return new $type();
}
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($type);
return $reflection->newInstanceArgs($args);
}
/**
* Set global arguments
*
* @param array $arguments
* @return void
*/
public function setArguments($arguments)
{
$this->globalArguments = $arguments;
}
}