From aed0bf20c6e9c4fc149054cc822abbf1ef489ca0 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Fri, 12 Jul 2024 19:33:29 +0300 Subject: [PATCH] Service, param injection --- .../core/DrupalDependenciesCommands.php | 33 ++++++++++++++++--- tests/integration/DrupalDependenciesTest.php | 5 ++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/Commands/core/DrupalDependenciesCommands.php b/src/Commands/core/DrupalDependenciesCommands.php index e8e061f455..827fcb02ae 100644 --- a/src/Commands/core/DrupalDependenciesCommands.php +++ b/src/Commands/core/DrupalDependenciesCommands.php @@ -12,16 +12,24 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Extension\Dependency; use Drupal\Core\Extension\Extension; +use Drupal\Core\Extension\ModuleExtensionList; use Drush\Attributes as CLI; use Drush\Boot\DrupalBootLevels; +use Drush\Commands\AutowireTrait; use Drush\Commands\DrushCommands; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\DependencyInjection\Attribute\Autowire; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Drush commands revealing Drupal dependencies. */ class DrupalDependenciesCommands extends DrushCommands { + use AutowireTrait { + create as traitCreate; + } + public const WHY_MODULE = 'why:module'; public const WHY_CONFIG = 'why:config'; @@ -35,6 +43,24 @@ class DrupalDependenciesCommands extends DrushCommands 'config-config' => [], ]; + public function __construct( + #[Autowire(param: 'container.modules')] + private readonly array $installedModules, + private readonly ModuleExtensionList $moduleExtensionList, + ) { + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container): self + { + // Wrap the AutowireTrait method, to pass the Symfony container instead + // of League container so that we can benefit from container params. + return static::traitCreate($container); + } + #[CLI\Command(name: self::WHY_MODULE, aliases: ['wm'])] #[CLI\Help(description: 'List all objects (modules, configurations) depending on a given module')] #[CLI\Argument(name: 'module', description: 'The module to check dependents for')] @@ -114,19 +140,18 @@ public function validateDependentsOfModule(CommandData $commandData): void throw new \InvalidArgumentException("Cannot use --dependent-type=config together with --no-only-installed"); } - $installedModules = \Drupal::getContainer()->getParameter('container.modules'); $module = $commandData->input()->getArgument('module'); if ($type === 'module') { $this->dependencies['module-module'] = array_map(function (Extension $extension): array { return array_map(function (string $dependencyString) { return Dependency::createFromString($dependencyString)->getName(); }, $extension->info['dependencies']); - }, \Drupal::service('extension.list.module')->getList()); + }, $this->moduleExtensionList->getList()); if (!$notOnlyInstalled) { $this->dependencies['module-module'] = array_intersect_key( $this->dependencies['module-module'], - $installedModules + $this->installedModules ); } if (!isset($this->dependencies['module-module'][$module])) { @@ -134,7 +159,7 @@ public function validateDependentsOfModule(CommandData $commandData): void '@module' => $module, ])); } - } elseif (!isset($installedModules[$module])) { + } elseif (!isset($this->installedModules[$module])) { throw new \InvalidArgumentException(dt('Invalid @module module', [ '@module' => $module, ])); diff --git a/tests/integration/DrupalDependenciesTest.php b/tests/integration/DrupalDependenciesTest.php index 993245c211..3ff9fb7559 100644 --- a/tests/integration/DrupalDependenciesTest.php +++ b/tests/integration/DrupalDependenciesTest.php @@ -1,9 +1,8 @@