Skip to content

Commit f3cfb91

Browse files
Allow objects to be properly injected through factories when variadic argument is the only one in constructor, and compiled mode is used
1 parent 82fd193 commit f3cfb91

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function create($requestedType, array $arguments = [])
6464
*
6565
* Argument key meanings:
6666
*
67+
* _vdic_: variadic argument
6768
* _i_: shared instance of a class or interface
6869
* _ins_: non-shared instance of a class or interface
6970
* _v_: non-array literal value
@@ -73,7 +74,21 @@ public function create($requestedType, array $arguments = [])
7374
* _d_: default value in case environment variable specified by _a_ does not exist
7475
*/
7576
foreach ($args as $key => &$argument) {
76-
if (isset($arguments[$key])) {
77+
if (isset($argument['_vdic_'])) {
78+
// Process variadic
79+
if (isset($arguments[$key])) {
80+
$argument = (array)$arguments[$key];
81+
} else {
82+
$argument = (array)$argument['_vdic_'];
83+
$this->parseArray($argument);
84+
}
85+
unset($args[$key]);
86+
if (count($argument)) {
87+
array_push($args, ...array_values($argument));
88+
}
89+
// Variadic argument is always the last one
90+
break;
91+
} elseif (isset($arguments[$key])) {
7792
$argument = $arguments[$key];
7893
} elseif (isset($argument['_i_'])) {
7994
$argument = $this->get($argument['_i_']);

setup/src/Magento/Setup/Module/Di/Compiler/ArgumentsResolver.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ class ArgumentsResolver
5959
'_vac_' => true,
6060
];
6161

62+
/**
63+
* Variadic pattern used for configuration
64+
*
65+
* @var array
66+
*/
67+
private $variadicPattern = [
68+
'_vdic_' => [],
69+
];
70+
6271
/**
6372
* Configured argument pattern used for configuration
6473
*
@@ -96,21 +105,14 @@ public function getResolvedConstructorArguments($instanceType, $constructor)
96105
/** @var ConstructorArgument $constructorArgument */
97106
foreach ($constructor as $constructorArgument) {
98107
if ($constructorArgument->isVariadic()) {
108+
$argument = $this->variadicPattern;
99109
$variadicArguments = $configuredArguments[$constructorArgument->getName()] ?? [];
100110

101-
if (!is_array($variadicArguments) || !count($variadicArguments)) {
102-
// Variadic argument is always the last one
103-
break;
104-
}
105-
106-
foreach ($variadicArguments as $variadicArgumentKey => $variadicArgument) {
107-
$variadicArguments[$variadicArgumentKey] = $this->getConfiguredArgument(
108-
$variadicArgument,
109-
$constructorArgument
110-
);
111+
foreach ($variadicArguments as $variadicArgument) {
112+
$argument['_vdic_'][] = $this->getConfiguredArgument($variadicArgument, $constructorArgument);
111113
}
112114

113-
array_push($arguments, ...array_values($variadicArguments));
115+
$arguments[$constructorArgument->getName()] = $argument;
114116

115117
// Variadic argument is always the last one
116118
break;

0 commit comments

Comments
 (0)