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 Drupal\Console\Core\Command\ContainerAwareCommand;
18 use Drupal\Core\Routing\RouteProviderInterface;
19 use Drupal\Console\Core\Utils\StringConverter;
20 use Drupal\Console\Core\Utils\ChainQueue;
21 use Drupal\Console\Core\Command\Shared\InputTrait;
22 use Drupal\Console\Extension\Manager;
23 use Drupal\Console\Utils\Validator;
25 class ControllerCommand extends ContainerAwareCommand
29 use ConfirmationTrait;
35 protected $extensionManager;
38 * @var ControllerGenerator
43 * @var StringConverter
45 protected $stringConverter;
53 * @var RouteProviderInterface
55 protected $routeProvider;
60 protected $chainQueue;
63 * ControllerCommand constructor.
65 * @param Manager $extensionManager
66 * @param ControllerGenerator $generator
67 * @param StringConverter $stringConverter
68 * @param Validator $validator
69 * @param RouteProviderInterface $routeProvider
70 * @param ChainQueue $chainQueue
72 public function __construct(
73 Manager $extensionManager,
74 ControllerGenerator $generator,
75 StringConverter $stringConverter,
77 RouteProviderInterface $routeProvider,
78 ChainQueue $chainQueue
80 $this->extensionManager = $extensionManager;
81 $this->generator = $generator;
82 $this->stringConverter = $stringConverter;
83 $this->validator = $validator;
84 $this->routeProvider = $routeProvider;
85 $this->chainQueue = $chainQueue;
86 parent::__construct();
89 protected function configure()
92 ->setName('generate:controller')
93 ->setDescription($this->trans('commands.generate.controller.description'))
94 ->setHelp($this->trans('commands.generate.controller.help'))
98 InputOption::VALUE_REQUIRED,
99 $this->trans('commands.common.options.module')
104 InputOption::VALUE_OPTIONAL,
105 $this->trans('commands.generate.controller.options.class')
110 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
111 $this->trans('commands.generate.controller.options.routes')
116 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
117 $this->trans('commands.common.options.services')
122 InputOption::VALUE_NONE,
123 $this->trans('commands.generate.controller.options.test')
125 ->setAliases(['gcon']);
131 protected function execute(InputInterface $input, OutputInterface $output)
133 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
134 if (!$this->confirmOperation()) {
138 $module = $input->getOption('module');
139 $class = $this->validator->validateControllerName($input->getOption('class'));
140 $routes = $input->getOption('routes');
141 $test = $input->getOption('test');
142 $services = $input->getOption('services');
144 $routes = $this->inlineValueAsArray($routes);
145 $input->setOption('routes', $routes);
147 // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
148 $build_services = $this->buildServices($services);
150 //$this->generator->setLearning($learning);
151 $this->generator->generate([
153 'class_name' => $class,
156 'services' => $build_services
159 // Run cache rebuild to see changes in Web UI
160 $this->chainQueue->addCommand('router:rebuild', []);
168 protected function interact(InputInterface $input, OutputInterface $output)
171 $module = $this->getModuleOption();
174 $class = $input->getOption('class');
176 $class = $this->getIo()->ask(
177 $this->trans('commands.generate.controller.questions.class'),
180 return $this->validator->validateControllerName($class);
183 $input->setOption('class', $class);
186 $routes = $input->getOption('routes');
189 $title = $this->getIo()->askEmpty(
190 $this->trans('commands.generate.controller.questions.title'),
192 function ($title) use ($routes) {
193 if ($routes && empty(trim($title))) {
197 if (!$routes && empty(trim($title))) {
198 throw new \InvalidArgumentException(
200 'commands.generate.controller.messages.title-empty'
205 if (in_array($title, array_column($routes, 'title'))) {
206 throw new \InvalidArgumentException(
209 'commands.generate.controller.messages.title-already-added'
224 $method = $this->getIo()->ask(
225 $this->trans('commands.generate.controller.questions.method'),
227 function ($method) use ($routes) {
228 if (in_array($method, array_column($routes, 'method'))) {
229 throw new \InvalidArgumentException(
232 'commands.generate.controller.messages.method-already-added'
243 $path = $this->getIo()->ask(
244 $this->trans('commands.generate.controller.questions.path'),
246 '/%s/'.($method!='hello'?$method:'hello/{name}'),
249 function ($path) use ($routes) {
250 if (count($this->routeProvider->getRoutesByPattern($path)) > 0
251 || in_array($path, array_column($routes, 'path'))
253 throw new \InvalidArgumentException(
256 'commands.generate.controller.messages.path-already-added'
266 $classMachineName = $this->stringConverter->camelCaseToMachineName($class);
267 $routeName = $module . '.' . $classMachineName . '_' . $method;
268 if ($this->routeProvider->getRoutesByNames([$routeName])
269 || in_array($routeName, $routes)
271 $routeName .= '_' . rand(0, 100);
276 'name' => $routeName,
281 $input->setOption('routes', $routes);
285 $test = $input->getOption('test');
287 $test = $this->getIo()->confirm(
288 $this->trans('commands.generate.controller.questions.test'),
292 $input->setOption('test', $test);
296 // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
297 $services = $this->servicesQuestion();
298 $input->setOption('services', $services);