From 3e8bd7f2b566b7fae7229e1f343dc38dd778a5fa Mon Sep 17 00:00:00 2001 From: Nikolay Lobachev Date: Wed, 21 Feb 2018 23:14:29 +0100 Subject: [PATCH 1/5] Add new arguments key and target. Make it possible use not only default target for db connections --- src/Command/Database/ConnectCommand.php | 15 +++++++++++---- src/Command/Shared/ConnectTrait.php | 14 +++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Command/Database/ConnectCommand.php b/src/Command/Database/ConnectCommand.php index 85ddd266b..bbdf9bf82 100644 --- a/src/Command/Database/ConnectCommand.php +++ b/src/Command/Database/ConnectCommand.php @@ -26,9 +26,15 @@ protected function configure() ->setName('database:connect') ->setDescription($this->trans('commands.database.connect.description')) ->addArgument( - 'database', + 'key', InputArgument::OPTIONAL, - $this->trans('commands.database.connect.arguments.database'), + $this->trans('commands.database.connect.arguments.key'), + 'default' + ) + ->addArgument( + 'target', + InputArgument::OPTIONAL, + $this->trans('commands.database.connect.arguments.target'), 'default' ) ->setHelp($this->trans('commands.database.connect.help')) @@ -40,8 +46,9 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $database = $input->getArgument('database'); - $databaseConnection = $this->resolveConnection($database); + $key = $input->getArgument('key'); + $target = $input->getArgument('target'); + $databaseConnection = $this->resolveConnection($key, $target); $connection = sprintf( '%s -A --database=%s --user=%s --password=%s --host=%s --port=%s', diff --git a/src/Command/Shared/ConnectTrait.php b/src/Command/Shared/ConnectTrait.php index c4eff2153..5af8c5d16 100644 --- a/src/Command/Shared/ConnectTrait.php +++ b/src/Command/Shared/ConnectTrait.php @@ -11,24 +11,24 @@ trait ConnectTrait { - protected $supportedDrivers = ['mysql','pgsql']; + protected $supportedDrivers = ['mysql', 'pgsql']; - public function resolveConnection($database = 'default') + public function resolveConnection($key = 'default', $target = 'default') { - $connectionInfo = Database::getConnectionInfo(); - - if (!$connectionInfo || !isset($connectionInfo[$database])) { + $connectionInfo = Database::getConnectionInfo($key); + if (!$connectionInfo || !isset($connectionInfo[$target])) { $this->getIo()->error( sprintf( $this->trans('commands.database.connect.messages.database-not-found'), - $database + $key, + $target ) ); return null; } - $databaseConnection = $connectionInfo[$database]; + $databaseConnection = $connectionInfo[$target]; if (!in_array($databaseConnection['driver'], $this->supportedDrivers)) { $this->getIo()->error( sprintf( From a71ed5ead5e3d81b9cf6012e3d07d307262f10a4 Mon Sep 17 00:00:00 2001 From: Nikolay Lobachev Date: Tue, 23 Oct 2018 14:15:32 +0200 Subject: [PATCH 2/5] Add Module Aware class --- src/Command/ModuleAwareCommand.php | 165 +++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 src/Command/ModuleAwareCommand.php diff --git a/src/Command/ModuleAwareCommand.php b/src/Command/ModuleAwareCommand.php new file mode 100644 index 000000000..e79bd48b1 --- /dev/null +++ b/src/Command/ModuleAwareCommand.php @@ -0,0 +1,165 @@ +generator = $generator; + parent::__construct(); + } + + /** + * Retrieves the extension manager. + * + * @return Manager + * The extension manager. + */ + protected function extensionManager() { + if (!isset($this->extensionManager)) { + $this->extensionManager = $this->container->get('console.extension_manager'); + } + return $this->extensionManager; + } + + /** + * Retrieves the console validator. + * + * @return Validator + * The console validator. + */ + protected function validator() { + if (!isset($this->validator)) { + $this->validator = $this->container->get('console.validator'); + } + return $this->validator; + } + + /** + * Ask the user to choose a module or profile. + * + * @param bool $showProfile + * If profiles should be discovered. + * + * @throws \Exception + * When no modules are found. + * + * @return string + */ + public function moduleQuestion($showProfile = true) + { + $modules = $this->extensionManager()->discoverModules() + ->showInstalled() + ->showUninstalled() + ->showNoCore() + ->getList(true); + + if ($showProfile) { + $profiles = $this->extensionManager()->discoverProfiles() + ->showInstalled() + ->showUninstalled() + ->showNoCore() + ->showCore() + ->getList(true); + + $modules = array_merge($modules, $profiles); + } + + if (empty($modules)) { + throw new \Exception('No extension available, execute the proper generator command to generate one.'); + } + + $module = $this->getIo()->choiceNoList( + $this->trans('commands.common.questions.module'), + $modules + ); + + return $module; + } + + /** + * Get module name from user. + * + * @return mixed|string + * Module name. + + */ + public function getModuleOption() + { + $input = $this->getIo()->getInput(); + $module = $input->getOption('module'); + if (!$module) { + // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion + $module = $this->moduleQuestion(); + $input->setOption('module', $module); + } else { + $this->validateModule($module); + } + + return $module; + } + + /** + * Validate module. + * + * @param string $module + * Module name. + * @return string + * Module name. + * + * @throws \Exception + * When module is not found. + */ + public function validateModule($module) { + $missing_modules = $this->validator()->getMissingModules([$module]); + if ($missing_modules) { + throw new \Exception( + sprintf( + $this->trans( + 'commands.module.download.messages.no-releases' + ), + $module + ) + ); + } + return $module; + } +} From fde5701f540fdd25d25063d941850973a21f6f21 Mon Sep 17 00:00:00 2001 From: Nikolay Lobachev Date: Tue, 23 Oct 2018 14:17:44 +0200 Subject: [PATCH 3/5] Add ModuleAware class as a base for class AjaxCommand --- config/services/generate.yml | 2 +- src/Command/Generate/AjaxCommand.php | 36 ++++------------------------ 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/config/services/generate.yml b/config/services/generate.yml index 02fc07ab3..e8d7d5ec9 100644 --- a/config/services/generate.yml +++ b/config/services/generate.yml @@ -21,7 +21,7 @@ services: - { name: drupal.command } console.generate_ajax: class: Drupal\Console\Command\Generate\AjaxCommand - arguments: ['@console.extension_manager', '@console.ajax_command_generator', '@console.validator', '@console.chain_queue'] + arguments: ['@console.ajax_command_generator', '@console.chain_queue'] tags: - { name: drupal.command } console.generate_breakpoint: diff --git a/src/Command/Generate/AjaxCommand.php b/src/Command/Generate/AjaxCommand.php index a3c062adf..828be448a 100644 --- a/src/Command/Generate/AjaxCommand.php +++ b/src/Command/Generate/AjaxCommand.php @@ -7,14 +7,12 @@ namespace Drupal\Console\Command\Generate; +use Drupal\Console\Command\ModuleAwareCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Drupal\Console\Command\Shared\ServicesTrait; -use Drupal\Console\Command\Shared\ConfirmationTrait; -use Drupal\Console\Command\Shared\ModuleTrait; use Drupal\Console\Generator\AjaxCommandGenerator; -use Drupal\Console\Core\Command\Command; use Drupal\Console\Core\Utils\ChainQueue; use Drupal\Console\Extension\Manager; use Drupal\Console\Utils\Validator; @@ -24,26 +22,9 @@ * * @package Drupal\Console\Command\Generate */ -class AjaxCommand extends Command +class AjaxCommand extends ModuleAwareCommand { - use ModuleTrait; use ServicesTrait; - use ConfirmationTrait; - - /** - * @var Manager - */ - protected $extensionManager; - - /** - * @var AjaxCommandGenerator - */ - protected $generator; - - /** - * @var Validator - */ - protected $validator; /** * @var ChainQueue @@ -53,22 +34,15 @@ class AjaxCommand extends Command /** * AjaxCommand constructor. * - * @param Manager $extensionManager * @param AjaxCommandGenerator $generator - * @param Validator $validator * @param ChainQueue $chainQueue */ public function __construct( - Manager $extensionManager, AjaxCommandGenerator $generator, - Validator $validator, ChainQueue $chainQueue ) { - $this->extensionManager = $extensionManager; - $this->generator = $generator; - $this->validator = $validator; $this->chainQueue = $chainQueue; - parent::__construct(); + parent::__construct($generator); } /** @@ -118,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } $module = $this->validateModule($input->getOption('module')); - $class = $this->validator->validateClassName($input->getOption('class')); + $class = $this->validator()->validateClassName($input->getOption('class')); $method = $input->getOption('method'); $js_name = $input->getOption('js-name'); @@ -152,7 +126,7 @@ protected function interact(InputInterface $input, OutputInterface $output) $this->trans('commands.generate.ajax.command.questions.class'), 'AjaxCommand', function ($class) { - return $this->validator->validateClassName($class); + return $this->validator()->validateClassName($class); } ); $input->setOption('class', $class); From d660958028fc863b613bc8114f95a060866ad1f9 Mon Sep 17 00:00:00 2001 From: Nikolay Lobachev Date: Tue, 23 Oct 2018 14:37:35 +0200 Subject: [PATCH 4/5] Add ModuleAware class as a base for class AuthenticationProviderCommand --- config/services/generate.yml | 2 +- .../AuthenticationProviderCommand.php | 42 ++++--------------- 2 files changed, 10 insertions(+), 34 deletions(-) diff --git a/config/services/generate.yml b/config/services/generate.yml index e8d7d5ec9..c61f1e581 100644 --- a/config/services/generate.yml +++ b/config/services/generate.yml @@ -11,7 +11,7 @@ services: - { name: drupal.command } console.generate_authentication_provider: class: Drupal\Console\Command\Generate\AuthenticationProviderCommand - arguments: ['@console.extension_manager', '@console.authentication_provider_generator', '@console.string_converter', '@console.validator'] + arguments: ['@console.authentication_provider_generator', '@console.string_converter'] tags: - { name: drupal.command } console.generate_controller: diff --git a/src/Command/Generate/AuthenticationProviderCommand.php b/src/Command/Generate/AuthenticationProviderCommand.php index e4669e813..793f795ad 100644 --- a/src/Command/Generate/AuthenticationProviderCommand.php +++ b/src/Command/Generate/AuthenticationProviderCommand.php @@ -8,43 +8,24 @@ namespace Drupal\Console\Command\Generate; use Drupal\Console\Utils\Validator; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; +use Drupal\Console\Command\ModuleAwareCommand; use Drupal\Console\Command\Shared\ServicesTrait; -use Drupal\Console\Command\Shared\ModuleTrait; -use Drupal\Console\Core\Command\Command; use Drupal\Console\Generator\AuthenticationProviderGenerator; -use Drupal\Console\Command\Shared\ConfirmationTrait; use Drupal\Console\Core\Utils\StringConverter; use Drupal\Console\Extension\Manager; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; -class AuthenticationProviderCommand extends Command +class AuthenticationProviderCommand extends ModuleAwareCommand { use ServicesTrait; - use ModuleTrait; - use ConfirmationTrait; - - /** - * @var Manager - */ - protected $extensionManager; - - /** - * @var AuthenticationProviderGenerator - */ - protected $generator; /** * @var StringConverter */ protected $stringConverter; - /** - * @var Validator - */ - protected $validator; - /** * AuthenticationProviderCommand constructor. * @@ -54,16 +35,11 @@ class AuthenticationProviderCommand extends Command * @param Validator $validator */ public function __construct( - Manager $extensionManager, AuthenticationProviderGenerator $generator, - StringConverter $stringConverter, - Validator $validator + StringConverter $stringConverter ) { - $this->extensionManager = $extensionManager; - $this->generator = $generator; $this->stringConverter = $stringConverter; - $this->validator = $validator; - parent::__construct(); + parent::__construct($generator); } protected function configure() @@ -99,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } $module = $this->validateModule($input->getOption('module')); - $class = $this->validator->validateClassName($input->getOption('class')); + $class = $this->validator()->validateClassName($input->getOption('class')); $provider_id = $input->getOption('provider-id'); $this->generator->generate([ @@ -127,7 +103,7 @@ protected function interact(InputInterface $input, OutputInterface $output) ), 'DefaultAuthenticationProvider', function ($class) { - return $this->validator->validateClassName($class); + return $this->validator()->validateClassName($class); } ); $input->setOption('class', $class); From e112a010f6a15a3103c9c10d0c4ed04f37dfe902 Mon Sep 17 00:00:00 2001 From: Nikolay Lobachev Date: Wed, 10 Apr 2019 22:54:01 +0200 Subject: [PATCH 5/5] Add missing use statements --- src/Command/Generate/PluginMigrateSourceCommand.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Command/Generate/PluginMigrateSourceCommand.php b/src/Command/Generate/PluginMigrateSourceCommand.php index 602ca34b9..d8739eaae 100644 --- a/src/Command/Generate/PluginMigrateSourceCommand.php +++ b/src/Command/Generate/PluginMigrateSourceCommand.php @@ -7,11 +7,13 @@ namespace Drupal\Console\Command\Generate; + use Drupal\Console\Core\Command\Command; -use Drupal\Console\Command\Shared\ModuleTrait; -use Drupal\Console\Command\Shared\ConfirmationTrait; use Drupal\Console\Core\Utils\StringConverter; use Drupal\Console\Core\Utils\ChainQueue; +use Drupal\Console\Command\Shared\ArrayInputTrait; +use Drupal\Console\Command\Shared\ModuleTrait; +use Drupal\Console\Command\Shared\ConfirmationTrait; use Drupal\Console\Extension\Manager; use Drupal\Console\Generator\PluginMigrateSourceGenerator; use Drupal\Console\Utils\Validator;