diff --git a/src/Command/Theme/InstallCommand.php b/src/Command/Theme/InstallCommand.php index 8c6a9b37f..a63acc79f 100644 --- a/src/Command/Theme/InstallCommand.php +++ b/src/Command/Theme/InstallCommand.php @@ -11,47 +11,10 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Drupal\Console\Core\Command\Command; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Extension\ThemeHandlerInterface; use Drupal\Core\Config\UnmetDependenciesException; -use Drupal\Console\Core\Utils\ChainQueue; -class InstallCommand extends Command +class InstallCommand extends ThemeBaseCommand { - /** - * @var ConfigFactory - */ - protected $configFactory; - - /** - * @var ThemeHandler - */ - protected $themeHandler; - - /** - * @var ChainQueue - */ - protected $chainQueue; - - /** - * DebugCommand constructor. - * - * @param ConfigFactory $configFactory - * @param ThemeHandler $themeHandler - * @param ChainQueue $chainQueue - */ - public function __construct( - ConfigFactoryInterface $configFactory, - ThemeHandlerInterface $themeHandler, - ChainQueue $chainQueue - ) { - $this->configFactory = $configFactory; - $this->themeHandler = $themeHandler; - $this->chainQueue = $chainQueue; - parent::__construct(); - } - protected function configure() { $this @@ -75,48 +38,10 @@ protected function configure() */ protected function interact(InputInterface $input, OutputInterface $output) { - $theme = $input->getArgument('theme'); - - if (!$theme) { - $theme_list = []; - - $themes = $this->themeHandler->rebuildThemeData(); - - foreach ($themes as $theme_id => $theme) { - if (!empty($theme->info['hidden'])) { - continue; - } - - if ($theme->status) { - continue; - } - - $theme_list[$theme_id] = $theme->getName(); - } - - $this->getIo()->info($this->trans('commands.theme.install.messages.disabled-themes')); - - while (true) { - $theme_name = $this->getIo()->choiceNoList( - $this->trans('commands.theme.install.questions.theme'), - array_keys($theme_list), - '', - true - ); - - if (empty($theme_name) || is_numeric($theme_name)) { - break; - } - - $theme_list_install[] = $theme_name; - - if (array_search($theme_name, $theme_list_install, true) >= 0) { - unset($theme_list[$theme_name]); - } - } - - $input->setArgument('theme', $theme_list_install); - } + $titleTranslatableString = 'commands.theme.install.messages.disabled-themes'; + $questionTranslatableString = 'commands.theme.install.questions.theme'; + $autocompleteAvailableThemes = $this->getAutoCompleteList(); + $this->getThemeArgument($titleTranslatableString, $questionTranslatableString, $autocompleteAvailableThemes); } protected function execute(InputInterface $input, OutputInterface $output) @@ -133,92 +58,37 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - $themes = $this->themeHandler->rebuildThemeData(); - $themesAvailable = []; - $themesInstalled = []; - $themesUnavailable = []; - - foreach ($theme as $themeName) { - if (isset($themes[$themeName]) && $themes[$themeName]->status == 0) { - $themesAvailable[] = $themes[$themeName]->info['name']; - } elseif (isset($themes[$themeName]) && $themes[$themeName]->status == 1) { - $themesInstalled[] = $themes[$themeName]->info['name']; - } else { - $themesUnavailable[] = $themeName; - } - } - - if (count($themesAvailable) > 0) { + $this->prepareThemesArrays($theme); + if (count($this->getUninstalledThemes()) > 0) { try { if ($this->themeHandler->install($theme)) { - if (count($themesAvailable) > 1) { - $this->getIo()->info( - sprintf( - $this->trans('commands.theme.install.messages.themes-success'), - implode(',', $themesAvailable) - ) - ); + if (count($this->getUninstalledThemes()) > 1) { + $this->setInfoMessage('commands.theme.install.messages.themes-success', $this->getUninstalledThemes()); } else { if ($default) { // Set the default theme. $config->set('default', $theme[0])->save(); - $this->getIo()->info( - sprintf( - $this->trans('commands.theme.install.messages.theme-default-success'), - $themesAvailable[0] - ) - ); + $this->setInfoMessage('commands.theme.install.messages.theme-default-success', array_shift($this->getUninstalledThemes())); } else { - $this->getIo()->info( - sprintf( - $this->trans('commands.theme.install.messages.theme-success'), - $themesAvailable[0] - ) - ); + $this->setInfoMessage('commands.theme.install.messages.theme-success', array_shift($this->getUninstalledThemes())); } } } } catch (UnmetDependenciesException $e) { - $this->getIo()->error( - sprintf( - $this->trans('commands.theme.install.messages.dependencies'), - $theme - ) - ); - + $this->setErrorMessage('commands.theme.install.messages.dependencies', $theme); return 1; } - } elseif (empty($themesAvailable) && count($themesInstalled) > 0) { - if (count($themesInstalled) > 1) { - $this->getIo()->info( - sprintf( - $this->trans('commands.theme.install.messages.themes-nothing'), - implode(',', $themesInstalled) - ) - ); + } elseif (empty($this->getUninstalledThemes()) && count($this->getAvailableThemes()) > 0) { + if (count($this->getAvailableThemes()) > 1) { + $this->setInfoMessage('commands.theme.install.messages.themes-nothing', $this->getAvailableThemes()); } else { - $this->getIo()->info( - sprintf( - $this->trans('commands.theme.install.messages.theme-nothing'), - implode(',', $themesInstalled) - ) - ); + $this->setInfoMessage('commands.theme.install.messages.theme-nothing', $this->getAvailableThemes()); } } else { - if (count($themesUnavailable) > 1) { - $this->getIo()->error( - sprintf( - $this->trans('commands.theme.install.messages.themes-missing'), - implode(',', $themesUnavailable) - ) - ); + if (count($this->getUnavailableThemes()) > 1) { + $this->setErrorMessage('commands.theme.install.messages.themes-missing', $this->getUnavailableThemes()); } else { - $this->getIo()->error( - sprintf( - $this->trans('commands.theme.install.messages.theme-missing'), - implode(',', $themesUnavailable) - ) - ); + $this->setErrorMessage('commands.theme.install.messages.themes-missing', $this->getUnavailableThemes()); } } diff --git a/src/Command/Theme/ThemeBaseCommand.php b/src/Command/Theme/ThemeBaseCommand.php new file mode 100644 index 000000000..eb3c1e976 --- /dev/null +++ b/src/Command/Theme/ThemeBaseCommand.php @@ -0,0 +1,286 @@ +configFactory = $configFactory; + $this->themeHandler = $themeHandler; + $this->chainQueue = $chainQueue; + $this->themes = $this->themeHandler->rebuildThemeData(); + parent::__construct(); + } + + /** + * Gets the list of themes available on the website. + * + * @return array themes from site. + */ + public function getThemes() + { + return $this->themes; + } + + /** + * Gets unavailable themes. + * + * @return array + * Available themes from input. + */ + public function getAvailableThemes() + { + return $this->availableThemes; + } + + /** + * Gets unavailable themes. + * + * @return array + * Unavailable themes from input. + */ + public function getUnavailableThemes() + { + return $this->unavailableThemes; + } + + /** + * Gets uninstalled themes. + * + * @return array + * Uninstalled themes from input. + */ + public function getUninstalledThemes() + { + return $this->uninstalledThemes; + } + + /** + * Adds available theme. + * + * @param string $themeMachineName + * Theme machine name. + * @param string $themeName + * Theme name. + */ + public function addAvailableTheme($themeMachineName, $themeName) + { + $this->availableThemes[$themeMachineName] = $themeName; + } + + /** + * Adds unavailable theme. + * + * @param string $themeMachineName + * Theme machine name. + * @param string $themeName + * Theme name. + */ + public function addUnavailableTheme($themeMachineName, $themeName) + { + $this->unavailableThemes[$themeMachineName] = $themeName; + } + + /** + * Adds uninstall theme. + * + * @param string $themeMachineName + * Theme machine name. + * @param string $themeName + * Theme name. + */ + public function addUninstalledTheme($themeMachineName, $themeName) + { + $this->uninstalledThemes[$themeMachineName] = $themeName; + } + + /** + * Prepare theme arrays: available, unavailable, uninstalled. + * + * @param array $themes + * Themes passed from a user. + */ + protected function prepareThemesArrays($themes) + { + $siteThemes = $this->getThemes(); + foreach ($themes as $themeName) { + if (isset($siteThemes[$themeName]) && $siteThemes[$themeName]->status == 1) { + $this->addAvailableTheme($themeName, $siteThemes[$themeName]->info['name']); + } elseif (isset($siteThemes[$themeName]) && $siteThemes[$themeName]->status == 0) { + $this->addUninstalledTheme($themeName, $siteThemes[$themeName]->info['name']); + } else { + $this->addUnavailableTheme($themeName, $themeName); + } + } + } + + /** + * Gets list of themes for autocomplete based on status. + * + * @param int $status + * Status of the themes. + * + * @return array + * Themes list. + */ + protected function getAutocompleteList($status = 1) + { + $theme_list = []; + foreach ($this->getThemes() as $theme_id => $theme) { + if (!empty($theme->info['hidden'])) { + continue; + } + + if (!empty($theme->status == $status)) { + continue; + } + $theme_list[$theme_id] = $theme->getName(); + } + + return $theme_list; + } + + /** + * Sets message of type 'info'. + * + * @param string $translationString + * String which will be replaced with translation. + * @param string $value + * The value to be include into the string. + */ + protected function setInfoMessage($translationString, $value) + { + $this->setMessage('info', $translationString, $value); + } + + /** + * Sets message of type 'error'. + * + * @param string $translationString + * String which will be replaced with translation. + * @param string $value + * The value to be include into the string. + */ + protected function setErrorMessage($translationString, $value) + { + $this->setMessage('error', $translationString, $value); + } + + /** + * Sets message in Drupal Console. + * + * @param string $type + * Type of the message: info, error and etc. + * @param string $translationString + * String which will be replaced with translation. + * @param string $value + * The value to be include into the string. + */ + protected function setMessage($type, $translationString, $value) + { + $text = is_array($value) ? implode(',', $value) : $value; + $this->getIo()->{$type}( + sprintf( + $this->trans($translationString), + $text + ) + ); + } + + + /** + * @param string $title + * @param string $question + * @param array $theme_list + */ + protected function getThemeArgument($title, $question, $theme_list) { + $input = $this->getIo()->getInput(); + $theme = $input->getArgument('theme'); + + if (!$theme) { + $this->getIo()->info($this->trans($title)); + $theme_list_install = []; + while (TRUE) { + $theme_name = $this->getIo()->choiceNoList( + $this->trans($question), + array_keys($theme_list), + '', + TRUE + ); + + if (empty($theme_name) || is_numeric($theme_name)) { + break; + } + + $theme_list_install[] = $theme_name; + + if (array_search($theme_name, $theme_list_install, TRUE) >= 0) { + unset($theme_list[$theme_name]); + } + } + } + } + +} diff --git a/src/Command/Theme/UninstallCommand.php b/src/Command/Theme/UninstallCommand.php index 786b4c4d8..e4574e872 100644 --- a/src/Command/Theme/UninstallCommand.php +++ b/src/Command/Theme/UninstallCommand.php @@ -10,47 +10,10 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Drupal\Console\Core\Command\Command; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Extension\ThemeHandlerInterface; use Drupal\Core\Config\UnmetDependenciesException; -use Drupal\Console\Core\Utils\ChainQueue; -class UninstallCommand extends Command +class UninstallCommand extends ThemeBaseCommand { - /** - * @var ConfigFactory - */ - protected $configFactory; - - /** - * @var ThemeHandler - */ - protected $themeHandler; - - /** - * @var ChainQueue - */ - protected $chainQueue; - - /** - * DebugCommand constructor. - * - * @param ConfigFactory $configFactory - * @param ThemeHandler $themeHandler - * @param ChainQueue $chainQueue - */ - public function __construct( - ConfigFactoryInterface $configFactory, - ThemeHandlerInterface $themeHandler, - ChainQueue $chainQueue - ) { - $this->configFactory = $configFactory; - $this->themeHandler = $themeHandler; - $this->chainQueue = $chainQueue; - parent::__construct(); - } - protected function configure() { $this @@ -69,154 +32,55 @@ protected function configure() */ protected function interact(InputInterface $input, OutputInterface $output) { - $theme = $input->getArgument('theme'); - - if (!$theme) { - $theme_list = []; - - $themes = $this->themeHandler->rebuildThemeData(); - - foreach ($themes as $theme_id => $theme) { - if (!empty($theme->info['hidden'])) { - continue; - } - - if (!empty($theme->status == 0)) { - continue; - } - $theme_list[$theme_id] = $theme->getName(); - } - - $this->getIo()->info($this->trans('commands.theme.uninstall.messages.installed-themes')); - - while (true) { - $theme_name = $this->getIo()->choiceNoList( - $this->trans('commands.theme.uninstall.questions.theme'), - array_keys($theme_list), - '', - true - ); - - if (empty($theme_name) || is_numeric($theme_name)) { - break; - } - - $theme_list_install[] = $theme_name; - - if (array_search($theme_name, $theme_list_install, true) >= 0) { - unset($theme_list[$theme_name]); - } - } - - $input->setArgument('theme', $theme_list_install); - } + $titleTranslatableString = 'commands.theme.uninstall.messages.installed-themes'; + $questionTranslatableString = 'commands.theme.uninstall.questions.theme'; + $autocompleteAvailableThemes = $this->getAutoCompleteList(0); + $this->getThemeArgument($titleTranslatableString, $questionTranslatableString, $autocompleteAvailableThemes); } protected function execute(InputInterface $input, OutputInterface $output) { $config = $this->configFactory->getEditable('system.theme'); - $this->themeHandler->refreshInfo(); $theme = $input->getArgument('theme'); + $this->prepareThemesArrays($theme); - $themes = $this->themeHandler->rebuildThemeData(); - $themesAvailable = []; - $themesUninstalled = []; - $themesUnavailable = []; - - foreach ($theme as $themeName) { - if (isset($themes[$themeName]) && $themes[$themeName]->status == 1) { - $themesAvailable[$themeName] = $themes[$themeName]->info['name']; - } elseif (isset($themes[$themeName]) && $themes[$themeName]->status == 0) { - $themesUninstalled[] = $themes[$themeName]->info['name']; - } else { - $themesUnavailable[] = $themeName; - } - } - - if (count($themesAvailable) > 0) { + if (count($this->getAvailableThemes()) > 0) { try { - foreach ($themesAvailable as $themeKey => $themeName) { + foreach ($this->getAvailableThemes() as $themeKey => $themeName) { if ($themeKey === $config->get('default')) { - $this->getIo()->error( - sprintf( - $this->trans('commands.theme.uninstall.messages.error-default-theme'), - implode(',', $themesAvailable) - ) - ); - + $this->setInfoMessage('commands.theme.uninstall.messages.error-default-theme', $this->getAvailableThemes()); return 1; } if ($themeKey === $config->get('admin')) { - $this->getIo()->error( - sprintf( - $this->trans('commands.theme.uninstall.messages.error-admin-theme'), - implode(',', $themesAvailable) - ) - ); + $this->setErrorMessage('commands.theme.uninstall.messages.error-admin-theme', $this->getAvailableThemes()); return 1; } } $this->themeHandler->uninstall($theme); - if (count($themesAvailable) > 1) { - $this->getIo()->info( - sprintf( - $this->trans('commands.theme.uninstall.messages.themes-success'), - implode(',', $themesAvailable) - ) - ); + if (count($this->getAvailableThemes()) > 1) { + $this->setInfoMessage('commands.theme.uninstall.messages.themes-success', $this->getAvailableThemes()); } else { - $this->getIo()->info( - sprintf( - $this->trans('commands.theme.uninstall.messages.theme-success'), - array_shift($themesAvailable) - ) - ); + $this->setInfoMessage('commands.theme.uninstall.messages.theme-success', array_shift($this->getAvailableThemes())); } } catch (UnmetDependenciesException $e) { - $this->getIo()->error( - sprintf( - $this->trans('commands.theme.uninstall.messages.dependencies'), - $e->getMessage() - ) - ); - + $this->setErrorMessage('commands.theme.uninstall.messages.dependencies', $this->getMessage()); return 1; } - } elseif (empty($themesAvailable) && count($themesUninstalled) > 0) { - if (count($themesUninstalled) > 1) { - $this->getIo()->info( - sprintf( - $this->trans('commands.theme.uninstall.messages.themes-nothing'), - implode(',', $themesUninstalled) - ) - ); + } elseif (empty($this->getAvailableThemes()) && count($this->getUninstalledThemes()) > 0) { + if (count($this->getUninstalledThemes()) > 1) { + $this->setInfoMessage('commands.theme.uninstall.messages.themes-nothing', $this->getUninstalledThemes()); } else { - $this->getIo()->info( - sprintf( - $this->trans('commands.theme.uninstall.messages.theme-nothing'), - implode(',', $themesUninstalled) - ) - ); + $this->setInfoMessage('commands.theme.uninstall.messages.theme-nothing', $this->getUninstalledThemes()); } } else { - if (count($themesUnavailable) > 1) { - $this->getIo()->error( - sprintf( - $this->trans('commands.theme.uninstall.messages.themes-missing'), - implode(',', $themesUnavailable) - ) - ); + if (count($this->getUnavailableThemes()) > 1) { + $this->setErrorMessage('commands.theme.uninstall.messages.themes-missing', $this->getUnavailableThemes()); } else { - $this->getIo()->error( - sprintf( - $this->trans('commands.theme.uninstall.messages.theme-missing'), - implode(',', $themesUnavailable) - ) - ); + $this->setErrorMessage('commands.theme.uninstall.messages.theme-missing', $this->getUnavailableThemes()); } } diff --git a/src/Generator/HelpGenerator.php b/src/Generator/HelpGenerator.php index bd0283053..21076eb89 100644 --- a/src/Generator/HelpGenerator.php +++ b/src/Generator/HelpGenerator.php @@ -8,9 +8,10 @@ namespace Drupal\Console\Generator; use Drupal\Console\Core\Generator\Generator; +use Drupal\Console\Core\Generator\GeneratorInterface; use Drupal\Console\Extension\Manager; -class HelpGenerator extends Generator implements Gener +class HelpGenerator extends Generator implements GeneratorInterface { /** * @var Manager