DevelReinstallCommand.php

Same filename in this branch
  1. 5.x src/Drush/Commands/DevelReinstallCommand.php

Namespace

Drupal\devel\Command

File

src/Command/DevelReinstallCommand.php

View source
<?php

declare (strict_types=1);
namespace Drupal\devel\Command;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ModuleInstallerInterface;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
 * Uninstalls modules and installs them again.
 */
class DevelReinstallCommand {
  public const NAME = 'devel:reinstall';
  public function __construct(protected readonly ModuleInstallerInterface $moduleInstaller, protected readonly ModuleHandlerInterface $moduleHandler) {
  }
  
  /**
   * Uninstalls the modules and installs them again.
   */
  public function __invoke(InputInterface $input, OutputInterface $output, #[Argument('A comma-separated list of module names.')] string $modules) : int {
    $modules = array_values(array_filter(array_map(trim(...), explode(',', $modules))));
    if ($modules === []) {
      throw new \InvalidArgumentException('No modules specified.');
    }
    $missing = array_filter($modules, fn(string $module): bool => !$this->moduleHandler
      ->moduleExists($module));
    if ($missing !== []) {
      throw new \InvalidArgumentException(sprintf('Not installed: %s', implode(', ', $missing)));
    }
    $reasons = $this->moduleInstaller
      ->validateUninstall($modules);
    if ($reasons !== []) {
      $messages = [];
      foreach ($reasons as $module => $module_reasons) {
        $messages[] = $module . ': ' . implode(' ', array_map(strval(...), $module_reasons));
      }
      throw new \RuntimeException(sprintf('Unable to uninstall. %s', implode(' ', $messages)));
    }
    // Like the reinstall form, leave dependent modules alone.
    $this->moduleInstaller
      ->uninstall($modules, FALSE);
    $this->moduleInstaller
      ->install($modules, FALSE);
    (new SymfonyStyle($input, $output))->success(sprintf('%s reinstalled.', implode(',', $modules)));
    return Command::SUCCESS;
  }

}

Classes

Title Deprecated Summary
DevelReinstallCommand Uninstalls modules and installs them again.