Skip to content

Commit

Permalink
Merge pull request #1571 from jmolivas/site-helper-get-modules-method
Browse files Browse the repository at this point in the history
[helper] Separate installed not installed modules
  • Loading branch information
jmolivas committed Dec 23, 2015
2 parents 6869708 + ca686ac commit c718cb2
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 127 deletions.
4 changes: 2 additions & 2 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public function setModule($module)
*/
public function getTheme()
{
return $this->theme;
return $this->theme;
}

/**
* @param string $theme
*/
public function setTheme($theme)
{
$this->theme = $theme;
$this->theme = $theme;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ContainerAwareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public function validateServiceExist($service_name, $services = null)
public function validateModule($machine_name)
{
$machine_name = $this->validateMachineName($machine_name);
$modules = $this->getSite()->getModules(false, false, true, true, true);
$modules = $this->getSite()->getModules(false, true, true, true, true, true);
if (in_array($machine_name, $modules)) {
throw new \InvalidArgumentException(sprintf('Module "%s" already exist.', $machine_name));
}
Expand Down
139 changes: 66 additions & 73 deletions src/Command/Module/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

class InstallCommand extends ContainerAwareCommand
{
protected $moduleInstaller;

protected function configure()
{
$this
Expand All @@ -39,20 +37,12 @@ protected function interact(InputInterface $input, OutputInterface $output)

if (!$module) {
$moduleList = [];

$modules = system_rebuild_module_data();
foreach ($modules as $moduleId => $module) {
if ($module->status == 1) {
continue;
}

$moduleList[$moduleId] = $module->info['name'];
}
$modules = $this->getSite()->getModules(true, false, true, true, true, true);

while (true) {
$moduleName = $io->choiceNoList(
$this->trans('commands.module.install.questions.module'),
array_keys($moduleList),
$modules,
null,
true
);
Expand All @@ -61,88 +51,65 @@ protected function interact(InputInterface $input, OutputInterface $output)
break;
}

$moduleListInstall[] = $moduleName;
$moduleList[] = $moduleName;

if (array_search($moduleName, $moduleListInstall, true) >= 0) {
unset($moduleList[$moduleName]);
if (array_search($moduleName, $moduleList, true) >= 0) {
unset($modules[array_search($moduleName, $modules)]);
}
}

$input->setArgument('module', $moduleListInstall);
$input->setArgument('module', $moduleList);
}

$overwrite_config = $input->getOption('overwrite-config');

$input->setOption('overwrite-config', $overwrite_config);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$extension_config = $this->getConfigFactory()->getEditable('core.extension');

$this->moduleInstaller = $this->getModuleInstaller();

// Get info about modules available
$module_data = system_rebuild_module_data();

$modules = $input->getArgument('module');
$overwrite_config = $input->getOption('overwrite-config');
$overwriteConfig = $input->getOption('overwrite-config');

$module_list = array_combine($modules, $modules);
$validator = $this->getValidator();
$moduleInstaller = $this->getModuleInstaller();

// Determine if some module request is missing
if ($missing_modules = array_diff_key($module_list, $module_data)) {
$invalidModules = $validator->getInvalidModules($modules);
if ($invalidModules) {
$io->error(
sprintf(
$this->trans('commands.module.install.messages.missing'),
implode(', ', $modules),
implode(', ', $missing_modules)
implode(', ', $invalidModules)
)
);

return true;
return;
}

// Only process currently uninstalled modules.
$installed_modules = $extension_config->get('module') ?: array();
if (!$module_list = array_diff_key($module_list, $installed_modules)) {
$unInstalledModules = $validator->getUninstalledModules($modules);
if (!$unInstalledModules) {
$io->warning($this->trans('commands.module.install.messages.nothing'));

return;
}

// Calculate dependencies and missing dependencies
$dependencies = array();
$missing_dependencies = array();
while (list($module) = each($module_list)) {
foreach (array_keys($module_data[$module]->requires) as $dependency) {
if (!isset($module_data[$dependency])) {
$missing_dependencies[] = $dependency;
}

// Skip already installed modules.
if (!isset($module_list[$dependency]) && !isset($installed_modules[$dependency])) {
$module_list[$dependency] = $dependency;
$dependencies[] = $dependency;
}
}
}
$dependencies = $this->calculateDependencies($unInstalledModules);

// Error if there are missing dependencies
if (!empty($missing_dependencies)) {
$missingDependencies = $validator->getInvalidModules($dependencies);
if ($missingDependencies) {
$io->error(
sprintf(
$this->trans('commands.module.install.messages.missing-dependencies'),
implode(', ', $modules),
implode(', ', $missing_dependencies)
implode(', ', $missingDependencies)
)
);

return true;
}

// Confirm if user want to install dependencies uninstalled
if ($dependencies) {
if (!$io->confirm(
sprintf(
Expand All @@ -155,19 +122,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

// Installing modules
$moduleList = array_merge($unInstalledModules, $dependencies);

try {
// Install the modules.
$this->moduleInstaller->install($module_list);
system_rebuild_module_data();
$moduleInstaller->install($moduleList);
$io->success(
sprintf(
$this->trans('commands.module.install.messages.success'),
implode(', ', array_merge($modules, $dependencies))
implode(', ', $moduleList)
)
);
} catch (PreExistingConfigException $e) {
$this->overwriteConfig($e, $module_list, $modules, $dependencies, $overwrite_config, $io);
$this->overwriteConfig($io, $e, $moduleList, $overwriteConfig);

return;
} catch (\Exception $e) {
Expand All @@ -180,9 +146,38 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->getChain()->addCommand('cache:rebuild', ['cache' => 'all']);
}

protected function overwriteConfig(PreExistingConfigException $e, $module_list, $modules, $dependencies, $overwrite_config, DrupalStyle $io)
protected function calculateDependencies($modules)
{
if ($overwrite_config) {
$dependencies = [];

$config = $this->getApplication()->getConfig();
$moduleList = $this->getSite()->getModules(true, true, true, true, true, false);
$validator = $this->getValidator();

foreach ($modules as $moduleName) {
$module = $moduleList[$moduleName];
$moduleConfig = $config->getFileContents($module->getPathname());

$dependencies = array_unique(
array_merge(
$dependencies,
$validator->getUninstalledModules(
array_values($moduleConfig['dependencies'])
)
)
);
}

return $dependencies;
}

protected function overwriteConfig(
DrupalStyle $io,
PreExistingConfigException $e,
$moduleList,
$overwriteConfig
) {
if ($overwriteConfig) {
$io->info($this->trans('commands.module.install.messages.config-conflict-overwrite'));
} else {
$io->info($this->trans('commands.module.install.messages.config-conflict'));
Expand All @@ -195,20 +190,18 @@ protected function overwriteConfig(PreExistingConfigException $e, $module_list,
$config->delete();
}

if (!$overwrite_config) {
if (!$overwriteConfig) {
return;
}

// Try to reinstall modules
try {
// Install the modules.
$this->moduleInstaller->install($module_list);
system_rebuild_module_data();
$moduleInstaller = $this->getModuleInstaller();
$moduleInstaller->install($moduleList);
$io->info(
sprintf(
$this->trans('commands.module.install.messages.success'),
implode(', ', array_merge($modules, $dependencies))
)
sprintf(
$this->trans('commands.module.install.messages.success'),
implode(', ', $moduleList)
)
);
} catch (\Exception $e) {
$io->error($e->getMessage());
Expand Down
28 changes: 14 additions & 14 deletions src/Command/Module/UninstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
// Determine if some module request is missing
if ($missing_modules = array_diff_key($module_list, $module_data)) {
$io->error(
sprintf(
$this->trans('commands.module.uninstall.messages.missing'),
implode(', ', $modules),
implode(', ', $missing_modules)
)
sprintf(
$this->trans('commands.module.uninstall.messages.missing'),
implode(', ', $modules),
implode(', ', $missing_modules)
)
);

return true;
Expand Down Expand Up @@ -75,11 +75,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
// Error if there are missing dependencies
if (!empty($dependents)) {
$io->error(
sprintf(
$this->trans('commands.module.uninstall.messages.dependents'),
implode(', ', $modules),
implode(', ', $dependents)
)
sprintf(
$this->trans('commands.module.uninstall.messages.dependents'),
implode(', ', $modules),
implode(', ', $dependents)
)
);

return true;
Expand All @@ -91,10 +91,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$moduleInstaller->uninstall($module_list);

$io->info(
sprintf(
$this->trans('commands.module.uninstall.messages.success'),
implode(', ', $modules)
)
sprintf(
$this->trans('commands.module.uninstall.messages.success'),
implode(', ', $modules)
)
);
} catch (\Exception $e) {
$io->error($e->getMessage());
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ModuleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait ModuleTrait
*/
public function moduleQuestion(DrupalStyle $io)
{
$modules = $this->getSite()->getModules(false, false, false, true, true);
$modules = $this->getSite()->getModules(false, true, true, false, true, true);

if (empty($modules)) {
throw new \Exception('No modules available, execute `generate:module` command to generate one.');
Expand Down
2 changes: 1 addition & 1 deletion src/Helper/CommandDiscoveryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function getCustomCommands($type = 'modules')
$sources = [];

if ($type === 'modules') {
$sources = $this->getSite()->getModules(true, false, false, true, false);
$sources = $this->getSite()->getModules(true, true, false, false, true, false);

if ($this->disabledModules) {
foreach ($this->disabledModules as $disabledModule) {
Expand Down

0 comments on commit c718cb2

Please sign in to comment.