Skip to content

Commit

Permalink
Aurowiring Drupal container parameters and plain values
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiu-cristea committed Jul 14, 2024
1 parent 87414b3 commit 16b2076
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 24 deletions.
65 changes: 56 additions & 9 deletions src/Commands/AutowireTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Drush\Commands;

use Drupal\Component\DependencyInjection\ContainerInterface as DrupalContainer;
use Drush\Drush;
use League\Container\Container as DrushContainer;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
Expand All @@ -16,31 +19,75 @@
*/
trait AutowireTrait
{
/**
/**
* Limit to service and param or plain value.
*
* @see \Symfony\Component\DependencyInjection\Attribute\Autowire::__construct
*/
private const ACCEPTED_AUTOWIRE_ARGUMENTS = [
0 => 'value',
1 => 'service',
4 => 'param',
];

/**
* Instantiates a new instance of the implementing class using autowiring.
*
* @param ContainerInterface $container
* The service container this instance should use.
*
* @return static
*/
public static function create(ContainerInterface $container)
public static function create(ContainerInterface $container, ?ContainerInterface $drushContainer = null)
{
$args = [];

if (method_exists(static::class, '__construct')) {
$drushContainer = $container instanceof DrushContainer ? $container : ($drushContainer instanceof DrushContainer ? $drushContainer : Drush::getContainer());
$drupalContainer = $container instanceof DrupalContainer ? $container : null;

$constructor = new \ReflectionMethod(static::class, '__construct');
foreach ($constructor->getParameters() as $parameter) {
$service = ltrim((string) $parameter->getType(), '?');
foreach ($parameter->getAttributes(Autowire::class) as $attribute) {
$service = (string) $attribute->newInstance()->value;
if (!$attributes = $parameter->getAttributes(Autowire::class)) {
// No #[Autowire()] attribute.
$service = ltrim((string) $parameter->getType(), '?');
if (!$drushContainer->has($service)) {
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));
}
$args[] = $drushContainer->get($service);
continue;
}

if (!$container->has($service)) {
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));
}
// This parameter has an #[Autowire()] attribute.
[$attribute] = $attributes;
$value = null;
foreach ($attribute->getArguments() as $key => $argument) {
// Resolve argument name when arguments are passed as list.
if (is_int($key)) {
if ($argument === null || !isset(self::ACCEPTED_AUTOWIRE_ARGUMENTS[$key])) {
continue;
}
$key = self::ACCEPTED_AUTOWIRE_ARGUMENTS[$key];
}

if (!in_array($key, self::ACCEPTED_AUTOWIRE_ARGUMENTS, true)) {
continue;
}

$args[] = $container->get($service);
$value = $attribute->newInstance()->value;
$valueAsString = (string) $value;
$value = match ($key) {
'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)),
// Container param comes as %foo.bar.param%.
'param' => $drupalContainer ? $drupalContainer->getParameter(trim($valueAsString, '%')) : $valueAsString,
default => $value,
};
// Done as Autowire::__construct() only needs one argument.
break;
}
if ($value !== null) {
$args[] = $value;
}
}
}

Expand Down
32 changes: 17 additions & 15 deletions src/Runtime/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Grasmash\YamlCli\Command\UpdateKeyCommand;
use Grasmash\YamlCli\Command\UpdateValueCommand;
use League\Container\Container as DrushContainer;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Robo\ClassDiscovery\RelativeNamespaceDiscovery;
Expand Down Expand Up @@ -349,11 +350,13 @@ public function instantiateServices(array $bootstrapCommandClasses, DrushContain
$commandHandler = null;

try {
if ($this->hasStaticCreateFactory($class) && $this->supportsCompoundContainer($class, $drushContainer)) {
// Hurray, this class is compatible with the container with delegate.
$commandHandler = $class::create($drushContainer);
} elseif ($container && $this->hasStaticCreateFactory($class)) {
$hasStaticCreateFactory = $this->hasStaticCreateFactory($class);
if ($hasStaticCreateFactory && $container instanceof DrupalContainer) {
$commandHandler = $class::create($container, $drushContainer);
}
elseif ($hasStaticCreateFactory && $drushContainer instanceof DrushContainer) {
// Class is compatible with the container with delegate.
$commandHandler = $class::create($drushContainer);
} elseif (!$container && $this->hasStaticCreateEarlyFactory($class)) {
$commandHandler = $class::createEarly($drushContainer);
} else {
Expand All @@ -372,22 +375,21 @@ public function instantiateServices(array $bootstrapCommandClasses, DrushContain
return $commandHandlers;
}

/**
* Determine if the first parameter of the create method supports our container with delegate.
*/
protected function supportsCompoundContainer($class, $drush_container): bool
{
$reflection = new \ReflectionMethod($class, 'create');
$hint = (string)$reflection->getParameters()[0]->getType();
return is_a($drush_container, $hint);
}

/**
* Check to see if the provided class has a static `create` method.
*/
protected function hasStaticCreateFactory(string $class): bool
{
return static::hasStaticMethod($class, 'create');
if (!$hasStaticCreateFactory = static::hasStaticMethod($class, 'create')) {
return false;
}
$reflection = new \ReflectionMethod($class, 'create');
// Check first two param typings.
foreach (array_slice($reflection->getParameters(), 0, 2) as $param) {
$typing = ltrim((string) $param->getType(), '?');
$hasStaticCreateFactory = $hasStaticCreateFactory && is_a($typing, ContainerInterface::class, true);
}
return $hasStaticCreateFactory;
}

/**
Expand Down

0 comments on commit 16b2076

Please sign in to comment.