814316b8b4dd134196a1711e2e58cd622ca4ff6b
[yaffs-website] / vendor / chi-teck / drupal-code-generator / src / Command / Drupal_8 / Controller.php
1 <?php
2
3 namespace DrupalCodeGenerator\Command\Drupal_8;
4
5 use DrupalCodeGenerator\Command\BaseGenerator;
6 use DrupalCodeGenerator\Utils;
7 use Symfony\Component\Console\Input\InputInterface;
8 use Symfony\Component\Console\Output\OutputInterface;
9 use Symfony\Component\Console\Question\ConfirmationQuestion;
10 use Symfony\Component\Console\Question\Question;
11
12 /**
13  * Implements d8:controller command.
14  */
15 class Controller extends BaseGenerator {
16
17   protected $name = 'd8:controller';
18   protected $description = 'Generates a controller';
19   protected $alias = 'controller';
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function interact(InputInterface $input, OutputInterface $output) {
25     $questions = Utils::defaultQuestions();
26
27     $default_class = function ($vars) {
28       return Utils::camelize($vars['name']) . 'Controller';
29     };
30     $questions['class'] = new Question('Class', $default_class);
31
32     $vars = $this->collectVars($input, $output, $questions);
33
34     $di_question = new ConfirmationQuestion('Would you like to inject dependencies?', FALSE);
35     if ($this->ask($input, $output, $di_question)) {
36       $this->collectServices($input, $output);
37     }
38
39     $route_question = new ConfirmationQuestion('Would you like to create a route for this controller?');
40     if ($this->ask($input, $output, $route_question)) {
41       $route_path = '/' . str_replace('_', '-', $vars['machine_name']) . '/example';
42       $route_questions['route_name'] = new Question('Route name', '{machine_name}.example');
43       $route_questions['route_path'] = new Question('Route path', $route_path);
44       $route_questions['route_title'] = new Question('Route title', 'Example');
45       $route_questions['route_permission'] = new Question('Route permission', 'access content');
46       $this->collectVars($input, $output, $route_questions, $vars);
47       $this->addFile()
48         ->path('{machine_name}.routing.yml')
49         ->template('d8/controller-route.twig')
50         ->action('append');
51     }
52
53     $this->addFile()
54       ->path('src/Controller/{class}.php')
55       ->template('d8/controller.twig');
56   }
57
58 }