6ab08915abbec4cc95bc9b38d77139c8713f52c3
[yaffs-website] / vendor / drupal / console / src / Command / Generate / ControllerCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\Console\Command\Generate\ControllerCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
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;
26
27 class ControllerCommand extends Command
28 {
29     use ModuleTrait;
30     use ServicesTrait;
31     use ConfirmationTrait;
32     use InputTrait;
33     use ContainerAwareCommandTrait;
34
35     /**
36  * @var Manager
37 */
38     protected $extensionManager;
39
40     /**
41  * @var ControllerGenerator
42 */
43     protected $generator;
44
45     /**
46      * @var StringConverter
47      */
48     protected $stringConverter;
49
50     /**
51  * @var Validator
52 */
53     protected $validator;
54
55     /**
56  * @var RouteProviderInterface
57 */
58     protected $routeProvider;
59
60     /**
61      * @var ChainQueue
62      */
63     protected $chainQueue;
64
65     /**
66      * ControllerCommand constructor.
67      *
68      * @param Manager                $extensionManager
69      * @param ControllerGenerator    $generator
70      * @param StringConverter        $stringConverter
71      * @param Validator              $validator
72      * @param RouteProviderInterface $routeProvider
73      * @param ChainQueue             $chainQueue
74      */
75     public function __construct(
76         Manager $extensionManager,
77         ControllerGenerator $generator,
78         StringConverter $stringConverter,
79         Validator $validator,
80         RouteProviderInterface $routeProvider,
81         ChainQueue $chainQueue
82     ) {
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();
90     }
91
92     protected function configure()
93     {
94         $this
95             ->setName('generate:controller')
96             ->setDescription($this->trans('commands.generate.controller.description'))
97             ->setHelp($this->trans('commands.generate.controller.help'))
98             ->addOption(
99                 'module',
100                 '',
101                 InputOption::VALUE_REQUIRED,
102                 $this->trans('commands.common.options.module')
103             )
104             ->addOption(
105                 'class',
106                 '',
107                 InputOption::VALUE_OPTIONAL,
108                 $this->trans('commands.generate.controller.options.class')
109             )
110             ->addOption(
111                 'routes',
112                 '',
113                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
114                 $this->trans('commands.generate.controller.options.routes')
115             )
116             ->addOption(
117                 'services',
118                 '',
119                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
120                 $this->trans('commands.common.options.services')
121             )
122             ->addOption(
123                 'test',
124                 '',
125                 InputOption::VALUE_NONE,
126                 $this->trans('commands.generate.controller.options.test')
127             );
128     }
129
130     /**
131      * {@inheritdoc}
132      */
133     protected function execute(InputInterface $input, OutputInterface $output)
134     {
135         $io = new DrupalStyle($input, $output);
136         $yes = $input->hasOption('yes')?$input->getOption('yes'):false;
137
138         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
139         if (!$this->confirmGeneration($io, $yes)) {
140             return;
141         }
142
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');
149
150         $routes = $this->inlineValueAsArray($routes);
151         $input->setOption('routes', $routes);
152
153         // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
154         $build_services = $this->buildServices($services);
155
156         //$this->generator->setLearning($learning);
157         $this->generator->generate(
158             $module,
159             $class,
160             $routes,
161             $test,
162             $build_services
163         );
164
165         // Run cache rebuild to see changes in Web UI
166         $this->chainQueue->addCommand('router:rebuild', []);
167     }
168
169     /**
170      * {@inheritdoc}
171      */
172     protected function interact(InputInterface $input, OutputInterface $output)
173     {
174         $io = new DrupalStyle($input, $output);
175
176         // --module option
177         $module = $input->getOption('module');
178         if (!$module) {
179             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
180             $module = $this->moduleQuestion($io);
181             $input->setOption('module', $module);
182         }
183
184         // --class option
185         $class = $input->getOption('class');
186         if (!$class) {
187             $class = $io->ask(
188                 $this->trans('commands.generate.controller.questions.class'),
189                 'DefaultController',
190                 function ($class) {
191                     return $this->validator->validateClassName($class);
192                 }
193             );
194             $input->setOption('class', $class);
195         }
196
197         $routes = $input->getOption('routes');
198         if (!$routes) {
199             while (true) {
200                 $title = $io->askEmpty(
201                     $this->trans('commands.generate.controller.questions.title'),
202                     function ($title) use ($routes) {
203                         if ($routes && empty(trim($title))) {
204                             return false;
205                         }
206
207                         if (!$routes && empty(trim($title))) {
208                             throw new \InvalidArgumentException(
209                                 $this->trans(
210                                     'commands.generate.controller.messages.title-empty'
211                                 )
212                             );
213                         }
214
215                         if (in_array($title, array_column($routes, 'title'))) {
216                             throw new \InvalidArgumentException(
217                                 sprintf(
218                                     $this->trans(
219                                         'commands.generate.controller.messages.title-already-added'
220                                     ),
221                                     $title
222                                 )
223                             );
224                         }
225
226                         return $title;
227                     }
228                 );
229
230                 if ($title === '') {
231                     break;
232                 }
233
234                 $method = $io->ask(
235                     $this->trans('commands.generate.controller.questions.method'),
236                     'hello',
237                     function ($method) use ($routes) {
238                         if (in_array($method, array_column($routes, 'method'))) {
239                             throw new \InvalidArgumentException(
240                                 sprintf(
241                                     $this->trans(
242                                         'commands.generate.controller.messages.method-already-added'
243                                     ),
244                                     $method
245                                 )
246                             );
247                         }
248
249                         return $method;
250                     }
251                 );
252
253                 $path = $io->ask(
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'))
259                         ) {
260                             throw new \InvalidArgumentException(
261                                 sprintf(
262                                     $this->trans(
263                                         'commands.generate.controller.messages.path-already-added'
264                                     ),
265                                     $path
266                                 )
267                             );
268                         }
269
270                         return $path;
271                     }
272                 );
273                 $classMachineName = $this->stringConverter->camelCaseToMachineName($class);
274                 $routeName = $module . '.' . $classMachineName . '_' . $method;
275                 if ($this->routeProvider->getRoutesByNames([$routeName])
276                     || in_array($routeName, $routes)
277                 ) {
278                     $routeName .= '_' . rand(0, 100);
279                 }
280
281                 $routes[] = [
282                     'title' => $title,
283                     'name' => $routeName,
284                     'method' => $method,
285                     'path' => $path
286                 ];
287             }
288             $input->setOption('routes', $routes);
289         }
290
291         // --test option
292         $test = $input->getOption('test');
293         if (!$test) {
294             $test = $io->confirm(
295                 $this->trans('commands.generate.controller.questions.test'),
296                 true
297             );
298
299             $input->setOption('test', $test);
300         }
301
302         // --services option
303         // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
304         $services = $this->servicesQuestion($io);
305         $input->setOption('services', $services);
306     }
307
308     /**
309      * @return \Drupal\Console\Generator\ControllerGenerator
310      */
311     protected function createGenerator()
312     {
313         return new ControllerGenerator();
314     }
315 }