function DevelReinstallCommand::__invoke

Uninstalls the modules and installs them again.

File

src/Command/DevelReinstallCommand.php, line 37

Class

DevelReinstallCommand
Uninstalls modules and installs them again.

Namespace

Drupal\devel\Command

Code

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;
}