Skip to content

Unpack object arguments as named parameters #37411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\ObjectManager\Factory;

use Magento\Framework\Exception\RuntimeException;
use Magento\Framework\ObjectManager\ConfigInterface;
use Magento\Framework\ObjectManager\Definition\Runtime;
use Magento\Framework\ObjectManager\DefinitionInterface;
use Magento\Framework\ObjectManager\FactoryInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Phrase;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\ObjectManager;
use Psr\Log\LoggerInterface;

/**
* Class AbstractFactory
*/
abstract class AbstractFactory implements \Magento\Framework\ObjectManager\FactoryInterface
abstract class AbstractFactory implements FactoryInterface
{
/**
* Object manager
* Object manager instance
*
* @var ObjectManagerInterface
*/
Expand All @@ -26,19 +31,19 @@ abstract class AbstractFactory implements \Magento\Framework\ObjectManager\Facto
/**
* Object manager config
*
* @var \Magento\Framework\ObjectManager\ConfigInterface
* @var ConfigInterface
*/
protected $config;

/**
* Definition list
*
* @var \Magento\Framework\ObjectManager\DefinitionInterface
* @var DefinitionInterface
*/
protected $definitions;

/**
* Global arguments
* Global arguments list
*
* @var array
*/
Expand All @@ -52,15 +57,15 @@ abstract class AbstractFactory implements \Magento\Framework\ObjectManager\Facto
protected $creationStack = [];

/**
* @param \Magento\Framework\ObjectManager\ConfigInterface $config
* @param ObjectManagerInterface $objectManager
* @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions
* @param array $globalArguments
* @param ConfigInterface $config
* @param ObjectManagerInterface $objectManager
* @param DefinitionInterface $definitions
* @param array $globalArguments
*/
public function __construct(
\Magento\Framework\ObjectManager\ConfigInterface $config,
ConfigInterface $config,
ObjectManagerInterface $objectManager = null,
\Magento\Framework\ObjectManager\DefinitionInterface $definitions = null,
DefinitionInterface $definitions = null,
$globalArguments = []
) {
$this->config = $config;
Expand Down Expand Up @@ -96,12 +101,12 @@ public function setArguments($arguments)
/**
* Get definitions
*
* @return \Magento\Framework\ObjectManager\DefinitionInterface
* @return DefinitionInterface
*/
public function getDefinitions()
{
if ($this->definitions === null) {
$this->definitions = new \Magento\Framework\ObjectManager\Definition\Runtime();
$this->definitions = new Runtime();
}
return $this->definitions;
}
Expand All @@ -110,15 +115,15 @@ public function getDefinitions()
* Create object
*
* @param string $type
* @param array $args
* @param array $args
*
* @return object
* @throws RuntimeException
*/
protected function createObject($type, $args)
{
try {
return new $type(...array_values($args));
return new $type(...$args);
} catch (\TypeError $exception) {
/**
* @var LoggerInterface $logger
Expand All @@ -140,9 +145,9 @@ protected function createObject($type, $args)
/**
* Resolve an argument
*
* @param array $argument
* @param array $argument
* @param string $paramType
* @param mixed $paramDefault
* @param mixed $paramDefault
* @param string $paramName
* @param string $requestedType
*
Expand Down Expand Up @@ -224,8 +229,8 @@ protected function parseArray(&$array)
* Resolve constructor arguments
*
* @param string $requestedType
* @param array $parameters
* @param array $arguments
* @param array $parameters
* @param array $arguments
*
* @return array
*
Expand All @@ -245,16 +250,16 @@ protected function resolveArgumentsInRuntime($requestedType, array $parameters,
/**
* Get resolved argument from parameter
*
* @param string $requestedType
* @param array $parameter
* @param array $arguments
* @param string $requestedType
* @param array $parameter
* @param array $arguments
* @return array
*/
private function getResolvedArgument(string $requestedType, array $parameter, array $arguments): array
{
list($paramName, $paramType, $paramRequired, $paramDefault, $isVariadic) = $parameter;
$argument = null;
if (!empty($arguments) && (isset($arguments[$paramName]) || array_key_exists($paramName, $arguments))) {
if (!empty($arguments) && (array_key_exists($paramName, $arguments))) {
$argument = $arguments[$paramName];
} elseif ($paramRequired) {
if ($paramType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Compiled extends AbstractFactory
protected $config;

/**
* Global arguments
* Global arguments list
*
* @var array
*/
Expand Down Expand Up @@ -94,7 +94,6 @@ public function create($requestedType, array $arguments = [])
}
}
}
$args = array_values($args);
} else {
// Case 3: arguments retrieved in runtime
$parameters = $this->getDefinitions()->getParameters($type) ?: [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/
namespace Magento\Framework\ObjectManager\Factory\Dynamic;

class Developer extends \Magento\Framework\ObjectManager\Factory\AbstractFactory
use Magento\Framework\ObjectManager\Factory\AbstractFactory;

class Developer extends AbstractFactory
{
/**
* Resolve constructor arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/
namespace Magento\Framework\ObjectManager\Factory\Dynamic;

class Production extends \Magento\Framework\ObjectManager\Factory\AbstractFactory
use Magento\Framework\ObjectManager\Factory\AbstractFactory;

class Production extends AbstractFactory
{
/**
* Resolve constructor arguments
Expand All @@ -28,7 +30,7 @@ protected function _resolveArguments($requestedType, array $parameters, array $a
foreach ($parameters as $parameter) {
list($paramName, $paramType, $paramRequired, $paramDefault) = $parameter;
$argument = null;
if (!empty($arguments) && (isset($arguments[$paramName]) || array_key_exists($paramName, $arguments))) {
if (!empty($arguments) && (array_key_exists($paramName, $arguments))) {
$argument = $arguments[$paramName];
} elseif ($paramRequired) {
$argument = ['instance' => $paramType];
Expand Down
Loading