5 * Contains \Drupal\Console\Command\Generate\CommandCommand.
8 namespace Drupal\Console\Command\Generate;
10 use Drupal\Console\Command\Shared\ExtensionTrait;
11 use Drupal\Console\Command\Shared\ServicesTrait;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Input\InputOption;
15 use Drupal\Console\Core\Command\ContainerAwareCommand;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Drupal\Console\Command\Shared\ModuleTrait;
18 use Drupal\Console\Generator\CommandGenerator;
19 use Drupal\Console\Core\Utils\StringConverter;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Utils\Validator;
22 use Drupal\Console\Utils\Site;
24 class CommandCommand extends ContainerAwareCommand
26 use ConfirmationTrait;
32 * @var CommandGenerator
39 protected $extensionManager;
47 * @var StringConverter
49 protected $stringConverter;
57 * CommandCommand constructor.
59 * @param CommandGenerator $generator
60 * @param Manager $extensionManager
61 * @param Validator $validator
62 * @param StringConverter $stringConverter
65 public function __construct(
66 CommandGenerator $generator,
67 Manager $extensionManager,
69 StringConverter $stringConverter,
72 $this->generator = $generator;
73 $this->extensionManager = $extensionManager;
74 $this->validator = $validator;
75 $this->stringConverter = $stringConverter;
77 parent::__construct();
83 protected function configure()
86 ->setName('generate:command')
87 ->setDescription($this->trans('commands.generate.command.description'))
88 ->setHelp($this->trans('commands.generate.command.help'))
92 InputOption::VALUE_REQUIRED,
93 $this->trans('commands.common.options.extension')
98 InputOption::VALUE_REQUIRED,
99 $this->trans('commands.common.options.extension-type')
104 InputOption::VALUE_REQUIRED,
105 $this->trans('commands.generate.command.options.class')
110 InputOption::VALUE_REQUIRED,
111 $this->trans('commands.generate.command.options.name')
116 InputOption::VALUE_NONE,
117 $this->trans('commands.generate.command.options.initialize')
122 InputOption::VALUE_NONE,
123 $this->trans('commands.generate.command.options.interact')
128 InputOption::VALUE_NONE,
129 $this->trans('commands.generate.command.options.container-aware')
134 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
135 $this->trans('commands.common.options.services')
140 InputOption::VALUE_NONE,
141 $this->trans('commands.generate.command.options.generator')
143 ->setAliases(['gco']);
149 protected function execute(InputInterface $input, OutputInterface $output)
151 $extension = $input->getOption('extension');
152 $extensionType = $input->getOption('extension-type');
153 $class = $this->validator->validateCommandName($input->getOption('class'));
154 $name = $input->getOption('name');
155 $initialize = $input->getOption('initialize');
156 $interact = $input->getOption('interact');
157 $containerAware = $input->getOption('container-aware');
158 $services = $input->getOption('services');
159 $generator = $input->getOption('generator');
161 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
162 if (!$this->confirmOperation()) {
166 // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
167 $build_services = $this->buildServices($services);
169 $class_generator = null;
171 $class_generator = str_replace('Command', 'Generator', $class);
174 $this->generator->generate([
175 'extension' => $extension,
176 'extension_type' => $extensionType,
178 'initialize' => $initialize,
179 'interact' => $interact,
180 'class_name' => $class,
181 'container_aware' => $containerAware,
182 'services' => $build_services,
183 'class_generator' => $class_generator,
184 'generator' => $generator,
187 $this->site->removeCachedServicesFile();
195 protected function interact(InputInterface $input, OutputInterface $output)
197 $extension = $input->getOption('extension');
199 $extension = $this->extensionQuestion(true, true);
200 $input->setOption('extension', $extension->getName());
201 $input->setOption('extension-type', $extension->getType());
204 $extensionType = $input->getOption('extension-type');
205 if (!$extensionType) {
206 $extensionType = $this->extensionTypeQuestion();
207 $input->setOption('extension-type', $extensionType);
210 $name = $input->getOption('name');
212 $name = $this->getIo()->ask(
213 $this->trans('commands.generate.command.questions.name'),
214 sprintf('%s:default', $extension->getName())
216 $input->setOption('name', $name);
219 $initialize = $input->getOption('initialize');
221 $initialize = $this->getIo()->confirm(
222 $this->trans('commands.generate.command.questions.initialize'),
225 $input->setOption('initialize', $initialize);
228 $interact = $input->getOption('interact');
230 $interact = $this->getIo()->confirm(
231 $this->trans('commands.generate.command.questions.interact'),
234 $input->setOption('interact', $interact);
237 $class = $input->getOption('class');
239 $class = $this->getIo()->ask(
240 $this->trans('commands.generate.command.questions.class'),
243 return $this->validator->validateCommandName($class);
246 $input->setOption('class', $class);
249 $containerAware = $input->getOption('container-aware');
250 if (!$containerAware) {
251 $containerAware = $this->getIo()->confirm(
252 $this->trans('commands.generate.command.questions.container-aware'),
255 $input->setOption('container-aware', $containerAware);
258 if (!$containerAware) {
259 // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
260 $services = $this->servicesQuestion();
261 $input->setOption('services', $services);
264 $generator = $input->getOption('generator');
266 $generator = $this->getIo()->confirm(
267 $this->trans('commands.generate.command.questions.generator'),
270 $input->setOption('generator', $generator);