diff --git a/config/services/generate.yml b/config/services/generate.yml index fd10bd54e..87fa4ab5b 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'] tags: - { name: drupal.command } console.generate_controller: @@ -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 c2618f577..2409ad07c 100644 --- a/src/Command/Generate/AjaxCommand.php +++ b/src/Command/Generate/AjaxCommand.php @@ -2,46 +2,27 @@ /** * @file - * Contains Drupal\Console\Command\Generate\ControllerCommand. + * Contains Drupal\Console\Command\Generate\AjaxCommand. */ 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\ConfirmationTrait; -use Drupal\Console\Command\Shared\ModuleTrait; +use Drupal\Console\Command\Shared\ServicesTrait; 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; /** * Class AjaxCommand * * @package Drupal\Console\Command\Generate */ -class AjaxCommand extends Command +class AjaxCommand extends ModuleAwareCommand { - use ModuleTrait; - use ConfirmationTrait; - - /** - * @var Manager - */ - protected $extensionManager; - - /** - * @var AjaxCommandGenerator - */ - protected $generator; - - /** - * @var Validator - */ - protected $validator; + use ServicesTrait; /** * @var ChainQueue @@ -51,22 +32,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); } /** @@ -116,7 +90,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'); @@ -150,7 +124,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); diff --git a/src/Command/Generate/AuthenticationProviderCommand.php b/src/Command/Generate/AuthenticationProviderCommand.php index 41f0bf7e6..2570603dc 100644 --- a/src/Command/Generate/AuthenticationProviderCommand.php +++ b/src/Command/Generate/AuthenticationProviderCommand.php @@ -7,61 +7,25 @@ namespace Drupal\Console\Command\Generate; -use Drupal\Console\Utils\Validator; +use Drupal\Console\Command\ModuleAwareCommand; +use Drupal\Console\Command\Shared\ServicesTrait; +use Drupal\Console\Generator\AuthenticationProviderGenerator; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -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; -class AuthenticationProviderCommand extends Command +class AuthenticationProviderCommand extends ModuleAwareCommand { - use ModuleTrait; - use ConfirmationTrait; - - /** - * @var Manager - */ - protected $extensionManager; - - /** - * @var AuthenticationProviderGenerator - */ - protected $generator; - - /** - * @var StringConverter - */ - protected $stringConverter; - - /** - * @var Validator - */ - protected $validator; + use ServicesTrait; /** * AuthenticationProviderCommand constructor. * - * @param Manager $extensionManager * @param AuthenticationProviderGenerator $generator - * @param StringConverter $stringConverter - * @param Validator $validator */ - public function __construct( - Manager $extensionManager, - AuthenticationProviderGenerator $generator, - StringConverter $stringConverter, - Validator $validator - ) { - $this->extensionManager = $extensionManager; - $this->generator = $generator; - $this->stringConverter = $stringConverter; - $this->validator = $validator; - parent::__construct(); + public function __construct(AuthenticationProviderGenerator $generator) + { + parent::__construct($generator); } protected function configure() @@ -97,7 +61,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([ @@ -111,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output) protected function interact(InputInterface $input, OutputInterface $output) { - $stringUtils = $this->stringConverter; + $stringUtils = $this->stringConverter(); // --module option $this->getModuleOption(); @@ -125,7 +89,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); diff --git a/src/Command/ModuleAwareCommand.php b/src/Command/ModuleAwareCommand.php new file mode 100644 index 000000000..f8c4de3cf --- /dev/null +++ b/src/Command/ModuleAwareCommand.php @@ -0,0 +1,182 @@ +generator = $generator; + parent::__construct(); + } + + /** + * Retrieves string converter. + * + * @return Manager + * String converter. + */ + protected function stringConverter() { + if (!isset($this->stringConverter)) { + $this->stringConverter = $this->container->get('console.string_converter'); + } + return $this->stringConverter; + } + + /** + * 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; + } +} diff --git a/src/Generator/AuthenticationProviderGenerator.php b/src/Generator/AuthenticationProviderGenerator.php index 2380df164..63727a048 100644 --- a/src/Generator/AuthenticationProviderGenerator.php +++ b/src/Generator/AuthenticationProviderGenerator.php @@ -22,9 +22,8 @@ class AuthenticationProviderGenerator extends Generator * * @param Manager $extensionManager */ - public function __construct( - Manager $extensionManager - ) { + public function __construct(Manager $extensionManager) + { $this->extensionManager = $extensionManager; } @@ -37,24 +36,26 @@ public function generate(array $parameters) $class = $parameters['class']; $provider_id = $parameters['provider_id']; $moduleInstance = $this->extensionManager->getModule($module); - $modulePath = $moduleInstance->getPath() . '/' . $module; + $modulePath = "{$moduleInstance->getPath()}/{$module}"; $this->renderFile( - 'module/src/Authentication/Provider/authentication-provider.php.twig', - $moduleInstance->getAuthenticationPath('Provider') . '/' . $class . '.php', - $parameters + 'module/src/Authentication/Provider/authentication-provider.php.twig', + "{$moduleInstance->getAuthenticationPath('Provider')}/{$class}.php", + $parameters ); + $moduleServicePath = "$modulePath.services.yml"; + $parameters = array_merge($parameters, [ 'module' => $module, 'class' => $class, 'class_path' => sprintf('Drupal\%s\Authentication\Provider\%s', $module, $class), - 'name' => 'authentication.' . $module, + 'name' => "authentication.$module", 'services' => [ ['name' => 'config.factory'], ['name' => 'entity_type.manager'], ], - 'file_exists' => file_exists($modulePath . '.services.yml'), + 'file_exists' => file_exists($moduleServicePath), 'tags' => [ 'name' => 'authentication_provider', 'provider_id' => $provider_id, @@ -64,7 +65,7 @@ public function generate(array $parameters) $this->renderFile( 'module/services.yml.twig', - $modulePath . '.services.yml', + $moduleServicePath, $parameters, FILE_APPEND );