5 * Contains Drupal\Console\Command\Generate\ControllerCommand.
8 namespace Drupal\Console\Command\Generate;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Command\Shared\ServicesTrait;
14 use Drupal\Console\Command\Shared\ConfirmationTrait;
15 use Drupal\Console\Command\Shared\ModuleTrait;
16 use Drupal\Console\Generator\ControllerGenerator;
17 use Symfony\Component\Console\Command\Command;
18 use Drupal\Core\Routing\RouteProviderInterface;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Console\Core\Utils\StringConverter;
21 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
22 use Drupal\Console\Core\Utils\ChainQueue;
23 use Drupal\Console\Core\Command\Shared\InputTrait;
24 use Drupal\Console\Extension\Manager;
25 use Drupal\Console\Utils\Validator;
27 class ControllerCommand extends Command
31 use ConfirmationTrait;
33 use ContainerAwareCommandTrait;
38 protected $extensionManager;
41 * @var ControllerGenerator
46 * @var StringConverter
48 protected $stringConverter;
56 * @var RouteProviderInterface
58 protected $routeProvider;
63 protected $chainQueue;
66 * ControllerCommand constructor.
68 * @param Manager $extensionManager
69 * @param ControllerGenerator $generator
70 * @param StringConverter $stringConverter
71 * @param Validator $validator
72 * @param RouteProviderInterface $routeProvider
73 * @param ChainQueue $chainQueue
75 public function __construct(
76 Manager $extensionManager,
77 ControllerGenerator $generator,
78 StringConverter $stringConverter,
80 RouteProviderInterface $routeProvider,
81 ChainQueue $chainQueue
83 $this->extensionManager = $extensionManager;
84 $this->generator = $generator;
85 $this->stringConverter = $stringConverter;
86 $this->validator = $validator;
87 $this->routeProvider = $routeProvider;
88 $this->chainQueue = $chainQueue;
89 parent::__construct();
92 protected function configure()
95 ->setName('generate:controller')
96 ->setDescription($this->trans('commands.generate.controller.description'))
97 ->setHelp($this->trans('commands.generate.controller.help'))
101 InputOption::VALUE_REQUIRED,
102 $this->trans('commands.common.options.module')
107 InputOption::VALUE_OPTIONAL,
108 $this->trans('commands.generate.controller.options.class')
113 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
114 $this->trans('commands.generate.controller.options.routes')
119 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
120 $this->trans('commands.common.options.services')
125 InputOption::VALUE_NONE,
126 $this->trans('commands.generate.controller.options.test')
133 protected function execute(InputInterface $input, OutputInterface $output)
135 $io = new DrupalStyle($input, $output);
136 $yes = $input->hasOption('yes')?$input->getOption('yes'):false;
138 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
139 if (!$this->confirmGeneration($io, $yes)) {
143 $learning = $input->hasOption('learning')?$input->getOption('learning'):false;
144 $module = $input->getOption('module');
145 $class = $input->getOption('class');
146 $routes = $input->getOption('routes');
147 $test = $input->getOption('test');
148 $services = $input->getOption('services');
150 $routes = $this->inlineValueAsArray($routes);
151 $input->setOption('routes', $routes);
153 // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
154 $build_services = $this->buildServices($services);
156 //$this->generator->setLearning($learning);
157 $this->generator->generate(
165 // Run cache rebuild to see changes in Web UI
166 $this->chainQueue->addCommand('router:rebuild', []);
172 protected function interact(InputInterface $input, OutputInterface $output)
174 $io = new DrupalStyle($input, $output);
177 $module = $input->getOption('module');
179 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
180 $module = $this->moduleQuestion($io);
181 $input->setOption('module', $module);
185 $class = $input->getOption('class');
188 $this->trans('commands.generate.controller.questions.class'),
191 return $this->validator->validateClassName($class);
194 $input->setOption('class', $class);
197 $routes = $input->getOption('routes');
200 $title = $io->askEmpty(
201 $this->trans('commands.generate.controller.questions.title'),
202 function ($title) use ($routes) {
203 if ($routes && empty(trim($title))) {
207 if (!$routes && empty(trim($title))) {
208 throw new \InvalidArgumentException(
210 'commands.generate.controller.messages.title-empty'
215 if (in_array($title, array_column($routes, 'title'))) {
216 throw new \InvalidArgumentException(
219 'commands.generate.controller.messages.title-already-added'
235 $this->trans('commands.generate.controller.questions.method'),
237 function ($method) use ($routes) {
238 if (in_array($method, array_column($routes, 'method'))) {
239 throw new \InvalidArgumentException(
242 'commands.generate.controller.messages.method-already-added'
254 $this->trans('commands.generate.controller.questions.path'),
255 sprintf('/%s/hello/{name}', $module),
256 function ($path) use ($routes) {
257 if (count($this->routeProvider->getRoutesByPattern($path)) > 0
258 || in_array($path, array_column($routes, 'path'))
260 throw new \InvalidArgumentException(
263 'commands.generate.controller.messages.path-already-added'
273 $classMachineName = $this->stringConverter->camelCaseToMachineName($class);
274 $routeName = $module . '.' . $classMachineName . '_' . $method;
275 if ($this->routeProvider->getRoutesByNames([$routeName])
276 || in_array($routeName, $routes)
278 $routeName .= '_' . rand(0, 100);
283 'name' => $routeName,
288 $input->setOption('routes', $routes);
292 $test = $input->getOption('test');
294 $test = $io->confirm(
295 $this->trans('commands.generate.controller.questions.test'),
299 $input->setOption('test', $test);
303 // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
304 $services = $this->servicesQuestion($io);
305 $input->setOption('services', $services);
309 * @return \Drupal\Console\Generator\ControllerGenerator
311 protected function createGenerator()
313 return new ControllerGenerator();