Skip to content

Commit 89cd330

Browse files
Aurowiring Drupal container parameters and plain values
1 parent 87414b3 commit 89cd330

File tree

2 files changed

+72
-24
lines changed

2 files changed

+72
-24
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)
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/Runtime/ServiceManager.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Grasmash\YamlCli\Command\UpdateKeyCommand;
2525
use Grasmash\YamlCli\Command\UpdateValueCommand;
2626
use League\Container\Container as DrushContainer;
27+
use Psr\Container\ContainerInterface;
2728
use Psr\Log\LoggerAwareInterface;
2829
use Psr\Log\LoggerInterface;
2930
use Robo\ClassDiscovery\RelativeNamespaceDiscovery;
@@ -349,11 +350,12 @@ public function instantiateServices(array $bootstrapCommandClasses, DrushContain
349350
$commandHandler = null;
350351

351352
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)) {
353+
$hasStaticCreateFactory = $this->hasStaticCreateFactory($class);
354+
if ($hasStaticCreateFactory && $container instanceof DrupalContainer) {
356355
$commandHandler = $class::create($container, $drushContainer);
356+
} elseif ($hasStaticCreateFactory && $drushContainer instanceof DrushContainer) {
357+
// Class is compatible with the container with delegate.
358+
$commandHandler = $class::create($drushContainer);
357359
} elseif (!$container && $this->hasStaticCreateEarlyFactory($class)) {
358360
$commandHandler = $class::createEarly($drushContainer);
359361
} else {
@@ -372,22 +374,21 @@ public function instantiateServices(array $bootstrapCommandClasses, DrushContain
372374
return $commandHandlers;
373375
}
374376

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
379-
{
380-
$reflection = new \ReflectionMethod($class, 'create');
381-
$hint = (string)$reflection->getParameters()[0]->getType();
382-
return is_a($drush_container, $hint);
383-
}
384-
385377
/**
386378
* Check to see if the provided class has a static `create` method.
387379
*/
388380
protected function hasStaticCreateFactory(string $class): bool
389381
{
390-
return static::hasStaticMethod($class, 'create');
382+
if (!$hasStaticCreateFactory = static::hasStaticMethod($class, 'create')) {
383+
return false;
384+
}
385+
$reflection = new \ReflectionMethod($class, 'create');
386+
// Check first two param typings.
387+
foreach (array_slice($reflection->getParameters(), 0, 2) as $param) {
388+
$typing = ltrim((string) $param->getType(), '?');
389+
$hasStaticCreateFactory = $hasStaticCreateFactory && is_a($typing, ContainerInterface::class, true);
390+
}
391+
return $hasStaticCreateFactory;
391392
}
392393

393394
/**

0 commit comments

Comments
 (0)