Skip to content

Commit 4416bc1

Browse files
committed
#31854: Customized laminas-di to resolve only required constructor parameters
1 parent 98925c2 commit 4416bc1

File tree

4 files changed

+116
-7
lines changed

4 files changed

+116
-7
lines changed

setup/config/application.config.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
use Magento\Setup\Mvc\Bootstrap\InitParamListener;
8-
use Laminas\Di\ConfigInterface;
97
use Laminas\Di\InjectorInterface;
10-
use Laminas\Di\Container\ConfigFactory;
11-
use Laminas\Di\Container\InjectorFactory;
8+
use Magento\Setup\Mvc\Bootstrap\InitParamListener;
129

1310
return [
1411
'modules' => require __DIR__ . '/modules.config.php',
@@ -25,9 +22,7 @@
2522
],
2623
'service_manager' => [
2724
'factories' => [
28-
// ConfigInterface::class => ConfigFactory::class,
29-
// InjectorInterface::class => InjectorFactory::class,
30-
InitParamListener::BOOTSTRAP_PARAM => InitParamListener::class
25+
InitParamListener::BOOTSTRAP_PARAM => InitParamListener::class,
3126
],
3227
]
3328
];

setup/src/Magento/Setup/Application.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
*/
66
namespace Magento\Setup;
77

8+
use Laminas\Di\InjectorInterface;
89
use Laminas\Mvc\Application as LaminasApplication;
910
use Laminas\Mvc\Service\ServiceManagerConfig;
1011
use Laminas\ServiceManager\ServiceManager;
12+
use Magento\Setup\Di\InjectorFactory;
1113

1214
/**
1315
* This class is wrapper on \Laminas\Mvc\Application
@@ -35,6 +37,7 @@ public function bootstrap(array $configuration)
3537
$serviceManager->setService('ApplicationConfig', $configuration);
3638

3739
$serviceManager->get('ModuleManager')->loadModules();
40+
$serviceManager->setFactory(InjectorInterface::class, InjectorFactory::class);
3841

3942
// load specific services
4043
if (!empty($configuration['required_services'])) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Setup\Di;
9+
10+
use Laminas\Di\Definition\ClassDefinitionInterface;
11+
use Laminas\Di\Resolver\DependencyResolver as LaminasDependencyResolver;
12+
13+
/**
14+
* Dependency resolver fulfilling required constructor parameters only
15+
*/
16+
class DependencyResolver extends LaminasDependencyResolver
17+
{
18+
/**
19+
* @inheritDoc
20+
*/
21+
private function getClassDefinition(string $type): ClassDefinitionInterface
22+
{
23+
if ($this->config->isAlias($type)) {
24+
$type = $this->config->getClassForAlias($type) ?? $type;
25+
}
26+
27+
return $this->definition->getClassDefinition($type);
28+
}
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
public function resolveParameters(string $requestedType, array $callTimeParameters = []): array
34+
{
35+
$result = parent::resolveParameters($requestedType, $callTimeParameters);
36+
if (empty($result)) {
37+
return [];
38+
}
39+
40+
$parameters = $this->getClassDefinition($requestedType)->getParameters();
41+
$requiredOnlyResult = [];
42+
43+
foreach ($parameters as $paramInfo) {
44+
if (!$paramInfo->isRequired()) {
45+
continue;
46+
}
47+
$name = $paramInfo->getName();
48+
$requiredOnlyResult[$name] = $result[$name];
49+
}
50+
51+
return $requiredOnlyResult;
52+
}
53+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Setup\Di;
9+
10+
use Laminas\Di\ConfigInterface;
11+
use Laminas\Di\Definition\RuntimeDefinition;
12+
use Laminas\Di\Injector;
13+
use Laminas\Di\InjectorInterface;
14+
use Psr\Container\ContainerInterface;
15+
use Zend\Di\ConfigInterface as LegacyConfigInterace;
16+
use Laminas\Di\Container\ConfigFactory;
17+
18+
/**
19+
* Implements the DependencyInjector service factory for laminas-servicemanager
20+
*/
21+
class InjectorFactory
22+
{
23+
private function createConfig(ContainerInterface $container): ConfigInterface
24+
{
25+
if ($container->has(ConfigInterface::class)) {
26+
return $container->get(ConfigInterface::class);
27+
}
28+
29+
if ($container->has(LegacyConfigInterace::class)) {
30+
return $container->get(LegacyConfigInterace::class);
31+
}
32+
33+
return (new ConfigFactory())->create($container);
34+
}
35+
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
public function create(ContainerInterface $container): InjectorInterface
40+
{
41+
$config = $this->createConfig($container);
42+
$definition = new RuntimeDefinition();
43+
return new Injector(
44+
$config,
45+
$container,
46+
$definition,
47+
new DependencyResolver($definition, $config)
48+
);
49+
}
50+
51+
/**
52+
* Make the instance invokable
53+
*/
54+
public function __invoke(ContainerInterface $container): InjectorInterface
55+
{
56+
return $this->create($container);
57+
}
58+
}

0 commit comments

Comments
 (0)