Skip to content

Commit

Permalink
[generate:ajax:command] Add new command. (#3668)
Browse files Browse the repository at this point in the history
* ajax-command: Added ajax command.

* ajax-command: Added option to generate custom ajax command.

* [generate:ajax:command]: Fix code styles.
  • Loading branch information
miguel303 authored and jmolivas committed Jan 10, 2018
1 parent 1e8be8b commit 8d28be2
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/services/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
5 changes: 5 additions & 0 deletions config/services/generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
167 changes: 167 additions & 0 deletions src/Command/Generate/AjaxCommand.php
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();
}
}
9 changes: 9 additions & 0 deletions src/Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions src/Generator/AjaxCommandGenerator.php
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
);
}
}
5 changes: 5 additions & 0 deletions templates/module/js/commands.php.twig
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);
5 changes: 5 additions & 0 deletions templates/module/module-libraries.yml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{ module }}.commands:
js:
js/custom.js: {}
dependencies:
- core/drupal.ajax
33 changes: 33 additions & 0 deletions templates/module/src/Ajax/ajax-command.php.twig
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 %}

0 comments on commit 8d28be2

Please sign in to comment.