-
-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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.
- Loading branch information
Showing
8 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains Drupal\Console\Command\Generate\ControllerCommand. | ||
*/ | ||
|
||
namespace Drupal\Console\Command\Generate; | ||
|
||
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\ContainerAwareCommand; | ||
use Drupal\Console\Core\Style\DrupalStyle; | ||
use Drupal\Console\Core\Utils\ChainQueue; | ||
use Drupal\Console\Extension\Manager; | ||
use Drupal\Console\Utils\Validator; | ||
|
||
class AjaxCommand extends ContainerAwareCommand | ||
{ | ||
use ModuleTrait; | ||
use ServicesTrait; | ||
use ConfirmationTrait; | ||
|
||
/** | ||
* @var Manager | ||
*/ | ||
protected $extensionManager; | ||
|
||
/** | ||
* @var ControllerGenerator | ||
*/ | ||
protected $generator; | ||
|
||
|
||
/** | ||
* @var Validator | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* @var ChainQueue | ||
*/ | ||
protected $chainQueue; | ||
|
||
/** | ||
* 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(); | ||
} | ||
|
||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains Drupal\Console\Generator\AjaxCommandGenerator. | ||
*/ | ||
|
||
namespace Drupal\Console\Generator; | ||
|
||
use Drupal\Console\Core\Generator\Generator; | ||
use Drupal\Console\Extension\Manager; | ||
|
||
class AjaxCommandGenerator extends Generator | ||
{ | ||
/** | ||
* @var Manager | ||
*/ | ||
protected $extensionManager; | ||
|
||
/** | ||
* AuthenticationProviderGenerator constructor. | ||
* | ||
* @param Manager $extensionManager | ||
*/ | ||
public function __construct( | ||
Manager $extensionManager | ||
) { | ||
$this->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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(function ($, Drupal) { | ||
Drupal.AjaxCommands.prototype.{{ method }} = function (ajax, response, status) { | ||
console.log(response.message); | ||
} | ||
})(jQuery, Drupal); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{{ module }}.commands: | ||
js: | ||
js/custom.js: {} | ||
dependencies: | ||
- core/drupal.ajax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %} |