namespace Drupal\Console\Command\Generate;
+use Drupal\Console\Utils\Validator;
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\Generator\ServiceGenerator;
use Drupal\Console\Command\Shared\ConfirmationTrait;
-use Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Style\DrupalStyle;
-use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
+use Drupal\Console\Core\Command\ContainerAwareCommand;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Core\Utils\ChainQueue;
use Drupal\Console\Core\Utils\StringConverter;
*
* @package Drupal\Console\Command\Generate
*/
-class ServiceCommand extends Command
+class ServiceCommand extends ContainerAwareCommand
{
use ServicesTrait;
use ModuleTrait;
use ConfirmationTrait;
- use ContainerAwareCommandTrait;
/**
- * @var Manager
-*/
+ * @var Manager
+ */
protected $extensionManager;
/**
- * @var ServiceGenerator
-*/
+ * @var ServiceGenerator
+ */
protected $generator;
/**
*/
protected $stringConverter;
+ /**
+ * @var Validator
+ */
+ protected $validator;
+
/**
* @var ChainQueue
*/
* @param Manager $extensionManager
* @param ServiceGenerator $generator
* @param StringConverter $stringConverter
+ * @param Validator $validator
* @param ChainQueue $chainQueue
*/
public function __construct(
Manager $extensionManager,
ServiceGenerator $generator,
StringConverter $stringConverter,
+ Validator $validator,
ChainQueue $chainQueue
) {
$this->extensionManager = $extensionManager;
$this->generator = $generator;
$this->stringConverter = $stringConverter;
+ $this->validator = $validator;
$this->chainQueue = $chainQueue;
parent::__construct();
}
'name',
null,
InputOption::VALUE_REQUIRED,
- $this->trans('commands.generate.service.options.name')
+ $this->trans('commands.generate.service.options.service-name')
)
->addOption(
'class',
'interface',
null,
InputOption::VALUE_NONE,
- $this->trans('commands.common.service.options.interface')
+ $this->trans('commands.generate.service.options.interface')
)
->addOption(
- 'interface_name',
+ 'interface-name',
null,
InputOption::VALUE_OPTIONAL,
- $this->trans('commands.common.service.options.interface_name')
+ $this->trans('commands.generate.service.options.interface-name')
)
->addOption(
'services',
$this->trans('commands.common.options.services')
)
->addOption(
- 'path_service',
+ 'path-service',
null,
InputOption::VALUE_OPTIONAL,
- $this->trans('commands.generate.service.options.path')
- );
+ $this->trans('commands.generate.service.options.path-service')
+ )
+ ->setAliases(['gs']);
}
/**
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
-
- // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
- if (!$this->confirmGeneration($io)) {
+ // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
+ if (!$this->confirmOperation()) {
return 1;
}
$module = $input->getOption('module');
$name = $input->getOption('name');
- $class = $input->getOption('class');
+ $class = $this->validator->validateClassName($input->getOption('class'));
$interface = $input->getOption('interface');
- $interface_name = $input->getOption('interface_name');
+ $interface_name = $input->getOption('interface-name');
$services = $input->getOption('services');
- $path_service = $input->getOption('path_service');
+ $path_service = $input->getOption('path-service');
$available_services = $this->container->getServiceIds();
// @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
$build_services = $this->buildServices($services);
- $this->generator->generate($module, $name, $class, $interface, $interface_name, $build_services, $path_service);
+ $this->generator->generate([
+ 'module' => $module,
+ 'name' => $name,
+ 'class' => $class,
+ 'interface' => $interface,
+ 'services' => $build_services,
+ 'path_service' => $path_service,
+ ]);
$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
*/
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();
//--name option
$name = $input->getOption('name');
if (!$name) {
- $name = $io->ask(
+ $name = $this->getIo()->ask(
$this->trans('commands.generate.service.questions.service-name'),
$module.'.default'
);
// --class option
$class = $input->getOption('class');
if (!$class) {
- $class = $io->ask(
+ $class = $this->getIo()->ask(
$this->trans('commands.generate.service.questions.class'),
- 'DefaultService'
+ 'DefaultService',
+ function ($class) {
+ return $this->validator->validateClassName($class);
+ }
);
$input->setOption('class', $class);
}
// --interface option
$interface = $input->getOption('interface');
if (!$interface) {
- $interface = $io->confirm(
+ $interface = $this->getIo()->confirm(
$this->trans('commands.generate.service.questions.interface'),
true
);
}
// --interface_name option
- $interface_name = $input->getOption('interface_name');
+ $interface_name = $input->getOption('interface-name');
if ($interface && !$interface_name) {
- $interface_name = $io->askEmpty(
- $this->trans('commands.generate.service.questions.interface_name')
+ $interface_name = $this->getIo()->askEmpty(
+ $this->trans('commands.generate.service.questions.interface-name')
);
- $input->setOption('interface_name', $interface_name);
+ $input->setOption('interface-name', $interface_name);
}
// --services option
$services = $input->getOption('services');
if (!$services) {
// @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
- $services = $this->servicesQuestion($io);
+ $services = $this->servicesQuestion();
$input->setOption('services', $services);
}
// --path_service option
- $path_service = $input->getOption('path_service');
+ $path_service = $input->getOption('path-service');
if (!$path_service) {
- $path_service = $io->ask(
- $this->trans('commands.generate.service.questions.path'),
+ $path_service = $this->getIo()->ask(
+ $this->trans('commands.generate.service.questions.path-service'),
'/modules/custom/' . $module . '/src/'
);
- $input->setOption('path_service', $path_service);
+ $input->setOption('path-service', $path_service);
}
}
}