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 $module = $input->getOption('module');
144 $class = $input->getOption('class');
145 $routes = $input->getOption('routes');
146 $test = $input->getOption('test');
147 $services = $input->getOption('services');
149 $routes = $this->inlineValueAsArray($routes);
150 $input->setOption('routes', $routes);
152 // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
153 $build_services = $this->buildServices($services);
155 //$this->generator->setLearning($learning);
156 $this->generator->generate(
164 // Run cache rebuild to see changes in Web UI
165 $this->chainQueue->addCommand('router:rebuild', []);
173 protected function interact(InputInterface $input, OutputInterface $output)
175 $io = new DrupalStyle($input, $output);
178 $module = $input->getOption('module');
180 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
181 $module = $this->moduleQuestion($io);
182 $input->setOption('module', $module);
186 $class = $input->getOption('class');
189 $this->trans('commands.generate.controller.questions.class'),
192 return $this->validator->validateClassName($class);
195 $input->setOption('class', $class);
198 $routes = $input->getOption('routes');
201 $title = $io->askEmpty(
202 $this->trans('commands.generate.controller.questions.title'),
203 function ($title) use ($routes) {
204 if ($routes && empty(trim($title))) {
208 if (!$routes && empty(trim($title))) {
209 throw new \InvalidArgumentException(
211 'commands.generate.controller.messages.title-empty'
216 if (in_array($title, array_column($routes, 'title'))) {
217 throw new \InvalidArgumentException(
220 'commands.generate.controller.messages.title-already-added'
236 $this->trans('commands.generate.controller.questions.method'),
238 function ($method) use ($routes) {
239 if (in_array($method, array_column($routes, 'method'))) {
240 throw new \InvalidArgumentException(
243 'commands.generate.controller.messages.method-already-added'
255 $this->trans('commands.generate.controller.questions.path'),
256 sprintf('/%s/hello/{name}', $module),
257 function ($path) use ($routes) {
258 if (count($this->routeProvider->getRoutesByPattern($path)) > 0
259 || in_array($path, array_column($routes, 'path'))
261 throw new \InvalidArgumentException(
264 'commands.generate.controller.messages.path-already-added'
274 $classMachineName = $this->stringConverter->camelCaseToMachineName($class);
275 $routeName = $module . '.' . $classMachineName . '_' . $method;
276 if ($this->routeProvider->getRoutesByNames([$routeName])
277 || in_array($routeName, $routes)
279 $routeName .= '_' . rand(0, 100);
284 'name' => $routeName,
289 $input->setOption('routes', $routes);
293 $test = $input->getOption('test');
295 $test = $io->confirm(
296 $this->trans('commands.generate.controller.questions.test'),
300 $input->setOption('test', $test);
304 // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
305 $services = $this->servicesQuestion($io);
306 $input->setOption('services', $services);
310 * @return \Drupal\Console\Generator\ControllerGenerator
312 protected function createGenerator()
314 return new ControllerGenerator();