Skip to content

Commit 2dcccbd

Browse files
Autowire container params
1 parent 038a783 commit 2dcccbd

File tree

3 files changed

+109
-23
lines changed

3 files changed

+109
-23
lines changed

src/Commands/AutowireTrait.php

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Drush\Commands;
44

5+
use Drupal\Component\DependencyInjection\ContainerInterface as DrupalContainer;
6+
use Drush\Drush;
7+
use League\Container\Container as DrushContainer;
58
use Psr\Container\ContainerInterface;
69
use Symfony\Component\DependencyInjection\Attribute\Autowire;
710
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
@@ -16,31 +19,75 @@
1619
*/
1720
trait AutowireTrait
1821
{
19-
/**
22+
/**
23+
* Limit to service and param or plain value.
24+
*
25+
* @see \Symfony\Component\DependencyInjection\Attribute\Autowire::__construct
26+
*/
27+
private const ACCEPTED_AUTOWIRE_ARGUMENTS = [
28+
0 => 'value',
29+
1 => 'service',
30+
4 => 'param',
31+
];
32+
33+
/**
2034
* Instantiates a new instance of the implementing class using autowiring.
2135
*
2236
* @param ContainerInterface $container
2337
* The service container this instance should use.
2438
*
2539
* @return static
2640
*/
27-
public static function create(ContainerInterface $container)
41+
public static function create(ContainerInterface $container, ?ContainerInterface $drushContainer = null): self
2842
{
2943
$args = [];
3044

3145
if (method_exists(static::class, '__construct')) {
46+
$drushContainer = $container instanceof DrushContainer ? $container : ($drushContainer instanceof DrushContainer ? $drushContainer : Drush::getContainer());
47+
$drupalContainer = $container instanceof DrupalContainer ? $container : null;
48+
3249
$constructor = new \ReflectionMethod(static::class, '__construct');
3350
foreach ($constructor->getParameters() as $parameter) {
34-
$service = ltrim((string) $parameter->getType(), '?');
35-
foreach ($parameter->getAttributes(Autowire::class) as $attribute) {
36-
$service = (string) $attribute->newInstance()->value;
51+
if (!$attributes = $parameter->getAttributes(Autowire::class)) {
52+
// No #[Autowire()] attribute.
53+
$service = ltrim((string) $parameter->getType(), '?');
54+
if (!$drushContainer->has($service)) {
55+
throw new AutowiringFailedException($service, sprintf('Cannot autowire service "%s": argument "$%s" of method "%s::_construct()", you should configure its value explicitly.', $service, $parameter->getName(), static::class));
56+
}
57+
$args[] = $drushContainer->get($service);
58+
continue;
3759
}
3860

39-
if (!$container->has($service)) {
40-
throw new AutowiringFailedException($service, sprintf('Cannot autowire service "%s": argument "$%s" of method "%s::_construct()", you should configure its value explicitly.', $service, $parameter->getName(), static::class));
41-
}
61+
// This parameter has an #[Autowire()] attribute.
62+
[$attribute] = $attributes;
63+
$value = null;
64+
foreach ($attribute->getArguments() as $key => $argument) {
65+
// Resolve argument name when arguments are passed as list.
66+
if (is_int($key)) {
67+
if ($argument === null || !isset(self::ACCEPTED_AUTOWIRE_ARGUMENTS[$key])) {
68+
continue;
69+
}
70+
$key = self::ACCEPTED_AUTOWIRE_ARGUMENTS[$key];
71+
}
72+
73+
if (!in_array($key, self::ACCEPTED_AUTOWIRE_ARGUMENTS, true)) {
74+
continue;
75+
}
4276

43-
$args[] = $container->get($service);
77+
$value = $attribute->newInstance()->value;
78+
$valueAsString = (string) $value;
79+
$value = match ($key) {
80+
'service' => $drushContainer->has($valueAsString) ? $drushContainer->get($valueAsString) : throw new AutowiringFailedException($valueAsString, sprintf('Cannot autowire service "%s": argument "$%s" of method "%s::_construct()", you should configure its value explicitly.', $valueAsString, $parameter->getName(), static::class)),
81+
// Container param comes as %foo.bar.param%.
82+
'param' => $drupalContainer ? $drupalContainer->getParameter(trim($valueAsString, '%')) : $valueAsString,
83+
default => $value,
84+
};
85+
// Done as Autowire::__construct() only needs one argument.
86+
break;
87+
}
88+
if ($value !== null) {
89+
$args[] = $value;
90+
}
4491
}
4592
}
4693

src/Commands/generate/GenerateCommands.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Drush\Commands\core\DocsCommands;
99
use Drush\Commands\DrushCommands;
1010
use Drush\Commands\help\ListCommands;
11-
use Psr\Container\ContainerInterface as DrushContainer;
11+
use League\Container\DefinitionContainerInterface as DrushContainer;
1212
use Symfony\Component\Console\Command\Command;
1313
use Symfony\Component\Console\Completion\CompletionInput;
1414
use Symfony\Component\Console\Completion\CompletionSuggestions;
@@ -21,6 +21,7 @@ final class GenerateCommands extends DrushCommands
2121
protected function __construct(
2222
private readonly DrushContainer $drush_container,
2323
) {
24+
parent::__construct();
2425
}
2526

2627
public static function create(DrushContainer $container): self

src/Runtime/ServiceManager.php

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
use Grasmash\YamlCli\Command\UnsetKeyCommand;
2424
use Grasmash\YamlCli\Command\UpdateKeyCommand;
2525
use Grasmash\YamlCli\Command\UpdateValueCommand;
26-
use League\Container\Container as DrushContainer;
26+
use League\Container\Container;
27+
use League\Container\DefinitionContainerInterface as DrushContainer;
28+
use Psr\Container\ContainerInterface;
2729
use Psr\Log\LoggerAwareInterface;
2830
use Psr\Log\LoggerInterface;
2931
use Robo\ClassDiscovery\RelativeNamespaceDiscovery;
@@ -342,18 +344,16 @@ public function instantiateServices(array $bootstrapCommandClasses, DrushContain
342344

343345
// Prevent duplicate calls to delegate() by checking for state.
344346
if ($container && !$drushContainer->has('state')) {
347+
assert($drushContainer instanceof Container);
345348
// Combine the two containers.
346349
$drushContainer->delegate($container);
347350
}
348351
foreach ($bootstrapCommandClasses as $class) {
349352
$commandHandler = null;
350353

351354
try {
352-
if ($this->hasStaticCreateFactory($class) && $this->supportsCompoundContainer($class, $drushContainer)) {
353-
// Hurray, this class is compatible with the container with delegate.
354-
$commandHandler = $class::create($drushContainer);
355-
} elseif ($container && $this->hasStaticCreateFactory($class)) {
356-
$commandHandler = $class::create($container, $drushContainer);
355+
if ($staticCreateFactoryArguments = $this->getStaticCreateFactoryArguments($class, $drushContainer, $container)) {
356+
$commandHandler = $class::create(...$staticCreateFactoryArguments);
357357
} elseif (!$container && $this->hasStaticCreateEarlyFactory($class)) {
358358
$commandHandler = $class::createEarly($drushContainer);
359359
} else {
@@ -372,22 +372,60 @@ public function instantiateServices(array $bootstrapCommandClasses, DrushContain
372372
return $commandHandlers;
373373
}
374374

375-
/**
376-
* Determine if the first parameter of the create method supports our container with delegate.
377-
*/
378-
protected function supportsCompoundContainer($class, $drush_container): bool
375+
protected function getStaticCreateFactoryArguments(string $class, DrushContainer $drushContainer, ?DrupalContainer $drupalContainer = null): ?array
379376
{
377+
if (!method_exists($class, 'create')) {
378+
return null;
379+
}
380+
380381
$reflection = new \ReflectionMethod($class, 'create');
381-
$hint = (string)$reflection->getParameters()[0]->getType();
382-
return is_a($drush_container, $hint);
382+
if (!$reflection->isStatic()) {
383+
return null;
384+
}
385+
386+
$params = $reflection->getParameters();
387+
388+
$args = [];
389+
$type = ltrim((string) $params[0]->getType(), '?');
390+
if ($drupalContainer && is_a($type, DrupalContainer::class, true)) {
391+
// The factory create() method explicitly expects a Drupal container as 1st argument.
392+
$args[] = $drupalContainer;
393+
} elseif (is_a($type, DrushContainer::class, true)) {
394+
// The factory create() method explicitly expects a Drush container as 1st argument. Don't add a 2nd
395+
// argument. If the Drupal container has been initialized, it's already a delegate.
396+
return [$drushContainer];
397+
} elseif ($drupalContainer && is_a($type, ContainerInterface::class, true)) {
398+
// The factory create() method expects a container of any type as st argument and the Drupal container has
399+
// been initialized. Pass it as 1st argument.
400+
$args[] = $drupalContainer;
401+
}
402+
403+
// Add Drush container as 2nd argument if the method expects one.
404+
if (isset($params[1])) {
405+
$type = ltrim((string) $params[1]->getType(), '?');
406+
if (is_a($type, ContainerInterface::class, true)) {
407+
$args[] = $drushContainer;
408+
}
409+
}
410+
411+
return $args;
383412
}
384413

385414
/**
386415
* Check to see if the provided class has a static `create` method.
387416
*/
388417
protected function hasStaticCreateFactory(string $class): bool
389418
{
390-
return static::hasStaticMethod($class, 'create');
419+
if (!$hasStaticCreateFactory = static::hasStaticMethod($class, 'create')) {
420+
return false;
421+
}
422+
$reflection = new \ReflectionMethod($class, 'create');
423+
// Check first two param typings.
424+
foreach (array_slice($reflection->getParameters(), 0, 2) as $param) {
425+
$typing = ltrim((string) $param->getType(), '?');
426+
$hasStaticCreateFactory = $hasStaticCreateFactory && is_a($typing, ContainerInterface::class, true);
427+
}
428+
return $hasStaticCreateFactory;
391429
}
392430

393431
/**

0 commit comments

Comments
 (0)