Skip to content

Commit 82fd193

Browse files
Allow use factories and OM for creating objects with variadic arguments in constructor
Related with #24556. That PR allowed to inject scalar values as variadic parameters, but do not work for objects injections. Object definitions don't get instantiated, and target class constructor receives an array of strings instead (with '_instance' key, that should have been transformed into real object)
1 parent 7d5967f commit 82fd193

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,13 @@ private function getResolvedArgument(string $requestedType, array $parameter, ar
270270
}
271271

272272
if ($isVariadic) {
273-
return is_array($argument) ? $argument : [$argument];
273+
$variadicArguments = is_array($argument) ? $argument : [$argument];
274+
275+
foreach ($variadicArguments as &$variadicArgument) {
276+
$this->resolveArgument($variadicArgument, $paramType, $paramDefault, $paramName, $requestedType);
277+
};
278+
279+
return $variadicArguments;
274280
}
275281

276282
$this->resolveArgument($argument, $paramType, $paramDefault, $paramName, $requestedType);

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

+24
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,33 @@ public function getResolvedConstructorArguments($instanceType, $constructor)
8989
if (!$constructor) {
9090
return null;
9191
}
92+
9293
$configuredArguments = $this->getConfiguredArguments($instanceType);
9394

9495
$arguments = [];
9596
/** @var ConstructorArgument $constructorArgument */
9697
foreach ($constructor as $constructorArgument) {
98+
if ($constructorArgument->isVariadic()) {
99+
$variadicArguments = $configuredArguments[$constructorArgument->getName()] ?? [];
100+
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+
}
112+
113+
array_push($arguments, ...array_values($variadicArguments));
114+
115+
// Variadic argument is always the last one
116+
break;
117+
}
118+
97119
$argument = $this->getNonObjectArgument(null);
98120
if (!$constructorArgument->isRequired()) {
99121
$argument = $this->getNonObjectArgument($constructorArgument->getDefaultValue());
@@ -107,8 +129,10 @@ public function getResolvedConstructorArguments($instanceType, $constructor)
107129
$constructorArgument
108130
);
109131
}
132+
110133
$arguments[$constructorArgument->getName()] = $argument;
111134
}
135+
112136
return $arguments;
113137
}
114138

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

+16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class ConstructorArgument
2929
*/
3030
private $defaultValue;
3131

32+
/**
33+
* @var bool
34+
*/
35+
private $isVariadic;
36+
3237
/**
3338
* @param array $configuration
3439
*/
@@ -38,6 +43,7 @@ public function __construct(array $configuration)
3843
$this->type = $configuration[1];
3944
$this->isRequired = $configuration[2];
4045
$this->defaultValue = $configuration[3];
46+
$this->isVariadic = $configuration[4];
4147
}
4248

4349
/**
@@ -79,4 +85,14 @@ public function getDefaultValue()
7985
{
8086
return $this->defaultValue;
8187
}
88+
89+
/**
90+
* Returns argument is variadic
91+
*
92+
* @return bool
93+
*/
94+
public function isVariadic(): bool
95+
{
96+
return $this->isVariadic;
97+
}
8298
}

0 commit comments

Comments
 (0)