Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ArgumentsResolver.php
Go to the documentation of this file.
1 <?php
9 
11 {
15  private $diContainerConfig;
16 
22  private $sharedInstancePattern = [
23  '_i_' => null,
24  ];
25 
31  private $notSharedInstancePattern = [
32  '_ins_' => null,
33  ];
34 
40  private $valuePattern = [
41  '_v_' => null,
42  ];
43 
49  private $nullValuePattern = [
50  '_vn_' => true,
51  ];
52 
58  private $configuredArrayValuePattern = [
59  '_vac_' => true,
60  ];
61 
67  private $configuredPattern = [
68  '_a_' => null,
69  '_d_' => null
70  ];
71 
75  public function __construct(\Magento\Framework\ObjectManager\ConfigInterface $diContainerConfig)
76  {
77  $this->diContainerConfig = $diContainerConfig;
78  }
79 
87  public function getResolvedConstructorArguments($instanceType, $constructor)
88  {
89  if (!$constructor) {
90  return null;
91  }
92  $configuredArguments = $this->getConfiguredArguments($instanceType);
93 
94  $arguments = [];
96  foreach ($constructor as $constructorArgument) {
97  $argument = $this->getNonObjectArgument(null);
98  if (!$constructorArgument->isRequired()) {
99  $argument = $this->getNonObjectArgument($constructorArgument->getDefaultValue());
100  } elseif ($constructorArgument->getType()) {
101  $argument = $this->getInstanceArgument($constructorArgument->getType());
102  }
103 
104  if (isset($configuredArguments[$constructorArgument->getName()])) {
105  $argument = $this->getConfiguredArgument(
106  $configuredArguments[$constructorArgument->getName()],
107  $constructorArgument
108  );
109  }
110  $arguments[$constructorArgument->getName()] = $argument;
111  }
112  return $arguments;
113  }
114 
122  private function getConfiguredArgument($configuredArgument, ConstructorArgument $constructorArgument)
123  {
124  if ($constructorArgument->getType()) {
125  $argument = $this->getConfiguredInstanceArgument($configuredArgument);
126  return $argument;
127  } elseif (isset($configuredArgument['argument'])) {
128  return $this->getGlobalArgument($configuredArgument['argument'], $constructorArgument->getDefaultValue());
129  }
130 
131  return $this->getNonObjectArgument($configuredArgument);
132  }
133 
140  private function getConfiguredArrayAttribute($array)
141  {
142  foreach ($array as $key => $value) {
143  if (!is_array($value)) {
144  continue;
145  }
146 
147  if (isset($value['instance'])) {
148  $array[$key] = $this->getConfiguredInstanceArgument($value);
149  continue;
150  }
151 
152  if (isset($value['argument'])) {
153  $array[$key] = $this->getGlobalArgument($value['argument'], null);
154  continue;
155  }
156 
157  $array[$key] = $this->getConfiguredArrayAttribute($value);
158  }
159 
160  return $array;
161  }
162 
169  private function getConfiguredInstanceArgument(array $config)
170  {
171  $argument = $this->getInstanceArgument($config['instance']);
172  if (isset($config['shared'])) {
173  if ($config['shared']) {
174  $pattern = $this->sharedInstancePattern;
175  $pattern['_i_'] = current($argument);
176  } else {
177  $pattern = $this->notSharedInstancePattern;
178  $pattern['_ins_'] = current($argument);
179  }
180  $argument = $pattern;
181  }
182  return $argument;
183  }
184 
191  private function getConfiguredArguments($instanceType)
192  {
193  $configuredArguments = $this->diContainerConfig->getArguments($instanceType);
194  return array_map(
195  function ($type) {
196  if (isset($type['instance'])) {
197  $type['instance'] = ltrim($type['instance'], '\\');
198  }
199 
200  return $type;
201  },
202  $configuredArguments
203  );
204  }
205 
212  private function getInstanceArgument($instanceType)
213  {
214  if ($this->diContainerConfig->isShared($instanceType)) {
215  $argument = $this->sharedInstancePattern;
216  $argument['_i_'] = $instanceType;
217  } else {
218  $argument = $this->notSharedInstancePattern;
219  $argument['_ins_'] = $instanceType;
220  }
221  return $argument;
222  }
223 
230  private function getNonObjectArgument($value)
231  {
232  if ($value === null) {
233  return $this->nullValuePattern;
234  }
235 
236  $argument = $this->valuePattern;
237  if (is_array($value)) {
238  if ($this->isConfiguredArray($value)) {
239  $value = $this->getConfiguredArrayAttribute($value);
240  $argument = $this->configuredArrayValuePattern;
241  $argument['_vac_'] = $value;
242  return $argument;
243  }
244  }
245 
246  $argument['_v_'] = $value;
247  return $argument;
248  }
249 
256  private function isConfiguredArray($value)
257  {
258  foreach ($value as $configuredValue) {
259  if (!is_array($configuredValue)) {
260  continue;
261  }
262 
263  if (array_key_exists('instance', $configuredValue) || array_key_exists('argument', $configuredValue)) {
264  return true;
265  }
266 
267  if ($this->isConfiguredArray($configuredValue)) {
268  return true;
269  }
270  }
271 
272  return false;
273  }
274 
282  private function getGlobalArgument($value, $default)
283  {
284  $argument = $this->configuredPattern;
285  $argument['_a_'] = $value;
286  $argument['_d_'] = $default;
287  return $argument;
288  }
289 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$pattern
Definition: website.php:22
$config
Definition: fraud_order.php:17
__construct(\Magento\Framework\ObjectManager\ConfigInterface $diContainerConfig)
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$arguments