From 8d28be28246d1bce114d2c0680c7573559abae3c Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 10 Jan 2018 14:44:35 -0600 Subject: [PATCH] [generate:ajax:command] Add new command. (#3668) * ajax-command: Added ajax command. * ajax-command: Added option to generate custom ajax command. * [generate:ajax:command]: Fix code styles. --- config/services/generate.yml | 5 + config/services/generator.yml | 5 + src/Command/Generate/AjaxCommand.php | 167 ++++++++++++++++++ src/Extension/Extension.php | 9 + src/Generator/AjaxCommandGenerator.php | 57 ++++++ templates/module/js/commands.php.twig | 5 + templates/module/module-libraries.yml.twig | 5 + .../module/src/Ajax/ajax-command.php.twig | 33 ++++ 8 files changed, 286 insertions(+) create mode 100644 src/Command/Generate/AjaxCommand.php create mode 100644 src/Generator/AjaxCommandGenerator.php create mode 100644 templates/module/js/commands.php.twig create mode 100644 templates/module/module-libraries.yml.twig create mode 100644 templates/module/src/Ajax/ajax-command.php.twig diff --git a/config/services/generate.yml b/config/services/generate.yml index 821586410..189db63e0 100644 --- a/config/services/generate.yml +++ b/config/services/generate.yml @@ -19,6 +19,11 @@ services: arguments: ['@console.extension_manager', '@console.controller_generator', '@console.string_converter', '@console.validator', '@router.route_provider', '@console.chain_queue'] tags: - { 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'] + tags: + - { name: drupal.command } console.generate_breakpoint: class: Drupal\Console\Command\Generate\BreakPointCommand arguments: ['@console.breakpoint_generator', '@app.root', '@theme_handler', '@console.validator', '@console.string_converter'] diff --git a/config/services/generator.yml b/config/services/generator.yml index 33de8156e..d38bac2b5 100644 --- a/config/services/generator.yml +++ b/config/services/generator.yml @@ -22,6 +22,11 @@ services: arguments: ['@console.extension_manager'] tags: - { name: drupal.generator } + console.ajax_command_generator: + class: Drupal\Console\Generator\AjaxCommandGenerator + arguments: ['@console.extension_manager'] + tags: + - { name: drupal.generator } console.breakpoint_generator: class: Drupal\Console\Generator\BreakPointGenerator arguments: ['@console.extension_manager'] diff --git a/src/Command/Generate/AjaxCommand.php b/src/Command/Generate/AjaxCommand.php new file mode 100644 index 000000000..3bfbbd20a --- /dev/null +++ b/src/Command/Generate/AjaxCommand.php @@ -0,0 +1,167 @@ +extensionManager = $extensionManager; + $this->generator = $generator; + $this->validator = $validator; + $this->chainQueue = $chainQueue; + parent::__construct(); + } + + protected function configure() + { + $this + ->setName('generate:ajax:command') + ->setDescription($this->trans('commands.generate.controller.description')) + ->setHelp($this->trans('commands.generate.controller.help')) + ->addOption( + 'module', + null, + InputOption::VALUE_REQUIRED, + $this->trans('commands.common.options.module') + ) + ->addOption( + 'class', + null, + InputOption::VALUE_OPTIONAL, + $this->trans('commands.generate.controller.options.class') + ) + ->addOption( + 'method', + null, + InputOption::VALUE_OPTIONAL, + $this->trans('commands.generate.controller.options.class') + ) + ->setAliases(['gac']); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new DrupalStyle($input, $output); + + // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration + if (!$this->confirmGeneration($io, $input)) { + return 1; + } + + $module = $input->getOption('module'); + $class = $this->validator->validateClassName($input->getOption('class')); + $method = $input->getOption('method'); + + $this->generator->generate( + $module, + $class, + $method + ); + + // Run cache rebuild to see changes in Web UI + $this->chainQueue->addCommand('router:rebuild', []); + + return 0; + } + + /** + * {@inheritdoc} + */ + protected function interact(InputInterface $input, OutputInterface $output) + { + $io = new DrupalStyle($input, $output); + + // --module option + $this->moduleFromInput($io, $input); + + // --class option + $class = $input->getOption('class'); + if (!$class) { + $class = $io->ask( + $this->trans('commands.generate.controller.questions.class'), + 'AjaxCommand', + function ($class) { + return $this->validator->validateClassName($class); + } + ); + $input->setOption('class', $class); + } + + // --method option + $method = $input->getOption('method'); + if (!$method) { + $method = $io->ask( + $this->trans('commands.generate.controller.questions.method'), + 'hello' + ); + $input->setOption('method', $method); + } + } + + /** + * @return \Drupal\Console\Generator\AjaxCommandGenerator + */ + protected function createGenerator() + { + return new AjaxCommandGenerator(); + } +} diff --git a/src/Extension/Extension.php b/src/Extension/Extension.php index 42d54ad26..576afc859 100644 --- a/src/Extension/Extension.php +++ b/src/Extension/Extension.php @@ -33,6 +33,15 @@ public function getControllerPath($fullPath = false) return $this->getSourcePath($fullPath) . '/Controller'; } + /** + * @param bool $fullPath + * @return string + */ + public function getAjaxPath($fullPath = false) + { + return $this->getSourcePath($fullPath) . '/Ajax'; + } + /** * @param bool $fullPath * @return string diff --git a/src/Generator/AjaxCommandGenerator.php b/src/Generator/AjaxCommandGenerator.php new file mode 100644 index 000000000..ba0082619 --- /dev/null +++ b/src/Generator/AjaxCommandGenerator.php @@ -0,0 +1,57 @@ +extensionManager = $extensionManager; + } + + public function generate($module, $class, $method) + { + $parameters = [ + 'class_name' => $class, + 'module' => $module, + 'method' => $method + ]; + + $this->renderFile( + 'module/src/Ajax/ajax-command.php.twig', + $this->extensionManager->getModule($module)->getAjaxPath().'/'.$class.'.php', + $parameters + ); + + $this->renderFile( + 'module/js/commands.php.twig', + $this->extensionManager->getModule($module)->getPath().'/js'.'/'.'custom.js', + $parameters + ); + + $this->renderFile( + 'module/module-libraries.yml.twig', + $this->extensionManager->getModule($module)->getPath().'/'.$module.'.libraries.yml', + $parameters + ); + } +} diff --git a/templates/module/js/commands.php.twig b/templates/module/js/commands.php.twig new file mode 100644 index 000000000..1c37459dc --- /dev/null +++ b/templates/module/js/commands.php.twig @@ -0,0 +1,5 @@ +(function ($, Drupal) { +Drupal.AjaxCommands.prototype.{{ method }} = function (ajax, response, status) { + console.log(response.message); +} +})(jQuery, Drupal); \ No newline at end of file diff --git a/templates/module/module-libraries.yml.twig b/templates/module/module-libraries.yml.twig new file mode 100644 index 000000000..9de3d9605 --- /dev/null +++ b/templates/module/module-libraries.yml.twig @@ -0,0 +1,5 @@ +{{ module }}.commands: + js: + js/custom.js: {} + dependencies: + - core/drupal.ajax \ No newline at end of file diff --git a/templates/module/src/Ajax/ajax-command.php.twig b/templates/module/src/Ajax/ajax-command.php.twig new file mode 100644 index 000000000..31ff8d530 --- /dev/null +++ b/templates/module/src/Ajax/ajax-command.php.twig @@ -0,0 +1,33 @@ +{% extends "base/class.php.twig" %} + +{% block file_path %} + \Drupal\{{ module }}\Ajax\{{ class_name }}. +{% endblock %} + +{% block namespace_class %} + namespace Drupal\{{ module }}\Ajax; +{% endblock %} + +{% block use_class %} + use Drupal\Core\Ajax\CommandInterface; +{% endblock %} +{% block class_declaration %} + /** + * Class {{ class_name }}. + */ + class {{ class_name }} implements CommandInterface {% endblock %} + +{% block class_methods %} + /** + * Render custom ajax command. + * + * @return ajax command function + */ + public function render() { + return [ + 'command' => '{{ method }}' + 'message' => 'My Awesome Message' + ]; + } + +{% endblock %}