use Drupal\Console\Command\Shared\ConfirmationTrait;
use Drupal\Console\Command\Shared\ModuleTrait;
use Drupal\Console\Generator\ControllerGenerator;
-use Symfony\Component\Console\Command\Command;
+use Drupal\Console\Core\Command\ContainerAwareCommand;
use Drupal\Core\Routing\RouteProviderInterface;
-use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Core\Utils\StringConverter;
-use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Console\Core\Utils\ChainQueue;
use Drupal\Console\Core\Command\Shared\InputTrait;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Utils\Validator;
-class ControllerCommand extends Command
+class ControllerCommand extends ContainerAwareCommand
{
use ModuleTrait;
use ServicesTrait;
use ConfirmationTrait;
use InputTrait;
- use ContainerAwareCommandTrait;
/**
- * @var Manager
-*/
+ * @var Manager
+ */
protected $extensionManager;
/**
- * @var ControllerGenerator
-*/
+ * @var ControllerGenerator
+ */
protected $generator;
/**
protected $stringConverter;
/**
- * @var Validator
-*/
+ * @var Validator
+ */
protected $validator;
/**
- * @var RouteProviderInterface
-*/
+ * @var RouteProviderInterface
+ */
protected $routeProvider;
/**
null,
InputOption::VALUE_NONE,
$this->trans('commands.generate.controller.options.test')
- );
+ )
+ ->setAliases(['gcon']);
}
/**
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
- $yes = $input->hasOption('yes')?$input->getOption('yes'):false;
-
- // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
- if (!$this->confirmGeneration($io, $yes)) {
+ // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
+ if (!$this->confirmOperation()) {
return 1;
}
$module = $input->getOption('module');
- $class = $input->getOption('class');
+ $class = $this->validator->validateControllerName($input->getOption('class'));
$routes = $input->getOption('routes');
$test = $input->getOption('test');
$services = $input->getOption('services');
$build_services = $this->buildServices($services);
//$this->generator->setLearning($learning);
- $this->generator->generate(
- $module,
- $class,
- $routes,
- $test,
- $build_services
- );
+ $this->generator->generate([
+ 'module' => $module,
+ 'class_name' => $class,
+ 'routes' => $routes,
+ 'test' => $test,
+ 'services' => $build_services
+ ]);
// Run cache rebuild to see changes in Web UI
$this->chainQueue->addCommand('router:rebuild', []);
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
-
// --module option
- $module = $input->getOption('module');
- if (!$module) {
- // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
- $module = $this->moduleQuestion($io);
- $input->setOption('module', $module);
- }
+ $module = $this->getModuleOption();
// --class option
$class = $input->getOption('class');
if (!$class) {
- $class = $io->ask(
+ $class = $this->getIo()->ask(
$this->trans('commands.generate.controller.questions.class'),
'DefaultController',
function ($class) {
- return $this->validator->validateClassName($class);
+ return $this->validator->validateControllerName($class);
}
);
$input->setOption('class', $class);
$routes = $input->getOption('routes');
if (!$routes) {
while (true) {
- $title = $io->askEmpty(
+ $title = $this->getIo()->askEmpty(
$this->trans('commands.generate.controller.questions.title'),
+ '',
function ($title) use ($routes) {
if ($routes && empty(trim($title))) {
return false;
break;
}
- $method = $io->ask(
+ $method = $this->getIo()->ask(
$this->trans('commands.generate.controller.questions.method'),
'hello',
function ($method) use ($routes) {
}
);
- $path = $io->ask(
+ $path = $this->getIo()->ask(
$this->trans('commands.generate.controller.questions.path'),
- sprintf('/%s/hello/{name}', $module),
+ sprintf(
+ '/%s/'.($method!='hello'?$method:'hello/{name}'),
+ $module
+ ),
function ($path) use ($routes) {
if (count($this->routeProvider->getRoutesByPattern($path)) > 0
|| in_array($path, array_column($routes, 'path'))
// --test option
$test = $input->getOption('test');
if (!$test) {
- $test = $io->confirm(
+ $test = $this->getIo()->confirm(
$this->trans('commands.generate.controller.questions.test'),
true
);
// --services option
// @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
- $services = $this->servicesQuestion($io);
+ $services = $this->servicesQuestion();
$input->setOption('services', $services);
}
-
- /**
- * @return \Drupal\Console\Generator\ControllerGenerator
- */
- protected function createGenerator()
- {
- return new ControllerGenerator();
- }
}