02f09391b09eed52e26937d2072af69492498732
[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                 null,
101                 InputOption::VALUE_REQUIRED,
102                 $this->trans('commands.common.options.module')
103             )
104             ->addOption(
105                 'class',
106                 null,
107                 InputOption::VALUE_OPTIONAL,
108                 $this->trans('commands.generate.controller.options.class')
109             )
110             ->addOption(
111                 'routes',
112                 null,
113                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
114                 $this->trans('commands.generate.controller.options.routes')
115             )
116             ->addOption(
117                 'services',
118                 null,
119                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
120                 $this->trans('commands.common.options.services')
121             )
122             ->addOption(
123                 'test',
124                 null,
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 1;
141         }
142
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');
148
149         $routes = $this->inlineValueAsArray($routes);
150         $input->setOption('routes', $routes);
151
152         // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
153         $build_services = $this->buildServices($services);
154
155         //$this->generator->setLearning($learning);
156         $this->generator->generate(
157             $module,
158             $class,
159             $routes,
160             $test,
161             $build_services
162         );
163
164         // Run cache rebuild to see changes in Web UI
165         $this->chainQueue->addCommand('router:rebuild', []);
166
167         return 0;
168     }
169
170     /**
171      * {@inheritdoc}
172      */
173     protected function interact(InputInterface $input, OutputInterface $output)
174     {
175         $io = new DrupalStyle($input, $output);
176
177         // --module option
178         $module = $input->getOption('module');
179         if (!$module) {
180             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
181             $module = $this->moduleQuestion($io);
182             $input->setOption('module', $module);
183         }
184
185         // --class option
186         $class = $input->getOption('class');
187         if (!$class) {
188             $class = $io->ask(
189                 $this->trans('commands.generate.controller.questions.class'),
190                 'DefaultController',
191                 function ($class) {
192                     return $this->validator->validateClassName($class);
193                 }
194             );
195             $input->setOption('class', $class);
196         }
197
198         $routes = $input->getOption('routes');
199         if (!$routes) {
200             while (true) {
201                 $title = $io->askEmpty(
202                     $this->trans('commands.generate.controller.questions.title'),
203                     function ($title) use ($routes) {
204                         if ($routes && empty(trim($title))) {
205                             return false;
206                         }
207
208                         if (!$routes && empty(trim($title))) {
209                             throw new \InvalidArgumentException(
210                                 $this->trans(
211                                     'commands.generate.controller.messages.title-empty'
212                                 )
213                             );
214                         }
215
216                         if (in_array($title, array_column($routes, 'title'))) {
217                             throw new \InvalidArgumentException(
218                                 sprintf(
219                                     $this->trans(
220                                         'commands.generate.controller.messages.title-already-added'
221                                     ),
222                                     $title
223                                 )
224                             );
225                         }
226
227                         return $title;
228                     }
229                 );
230
231                 if ($title === '') {
232                     break;
233                 }
234
235                 $method = $io->ask(
236                     $this->trans('commands.generate.controller.questions.method'),
237                     'hello',
238                     function ($method) use ($routes) {
239                         if (in_array($method, array_column($routes, 'method'))) {
240                             throw new \InvalidArgumentException(
241                                 sprintf(
242                                     $this->trans(
243                                         'commands.generate.controller.messages.method-already-added'
244                                     ),
245                                     $method
246                                 )
247                             );
248                         }
249
250                         return $method;
251                     }
252                 );
253
254                 $path = $io->ask(
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'))
260                         ) {
261                             throw new \InvalidArgumentException(
262                                 sprintf(
263                                     $this->trans(
264                                         'commands.generate.controller.messages.path-already-added'
265                                     ),
266                                     $path
267                                 )
268                             );
269                         }
270
271                         return $path;
272                     }
273                 );
274                 $classMachineName = $this->stringConverter->camelCaseToMachineName($class);
275                 $routeName = $module . '.' . $classMachineName . '_' . $method;
276                 if ($this->routeProvider->getRoutesByNames([$routeName])
277                     || in_array($routeName, $routes)
278                 ) {
279                     $routeName .= '_' . rand(0, 100);
280                 }
281
282                 $routes[] = [
283                     'title' => $title,
284                     'name' => $routeName,
285                     'method' => $method,
286                     'path' => $path
287                 ];
288             }
289             $input->setOption('routes', $routes);
290         }
291
292         // --test option
293         $test = $input->getOption('test');
294         if (!$test) {
295             $test = $io->confirm(
296                 $this->trans('commands.generate.controller.questions.test'),
297                 true
298             );
299
300             $input->setOption('test', $test);
301         }
302
303         // --services option
304         // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
305         $services = $this->servicesQuestion($io);
306         $input->setOption('services', $services);
307     }
308
309     /**
310      * @return \Drupal\Console\Generator\ControllerGenerator
311      */
312     protected function createGenerator()
313     {
314         return new ControllerGenerator();
315     }
316 }