Skip to content

Commit

Permalink
Service, param injection
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiu-cristea committed Jul 12, 2024
1 parent 6e4f736 commit 6e8820e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
20 changes: 16 additions & 4 deletions src/Commands/core/DrupalDependenciesCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@
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;

/**
* Drush commands revealing Drupal dependencies.
*/
class DrupalDependenciesCommands extends DrushCommands
{
use AutowireTrait;

public const WHY_MODULE = 'why:module';
public const WHY_CONFIG = 'why:config';

Expand All @@ -35,6 +40,14 @@ class DrupalDependenciesCommands extends DrushCommands
'config-config' => [],
];

public function __construct(
#[Autowire(param: 'container.modules')]
private readonly array $installedModules,
private readonly ModuleExtensionList $moduleExtensionList,
) {
parent::__construct();
}

#[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')]
Expand Down Expand Up @@ -114,27 +127,26 @@ 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])) {
throw new \InvalidArgumentException(dt('Invalid @module module', [
'@module' => $module,
]));
}
} elseif (!isset($installedModules[$module])) {
} elseif (!isset($this->installedModules[$module])) {
throw new \InvalidArgumentException(dt('Invalid @module module', [
'@module' => $module,
]));
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/DrupalDependenciesTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

namespace Unish;
declare(strict_types=1);

use Drush\TestTraits\DrushTestTrait;
use PHPUnit\Framework\TestCase;
namespace Unish;

/**
* @coversDefaultClass \Drush\Commands\core\DrupalDependenciesCommands
Expand Down

0 comments on commit 6e8820e

Please sign in to comment.