Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / RouteSubscriberCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\RouteSubscriber.
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\ModuleTrait;
14 use Drupal\Console\Generator\RouteSubscriberGenerator;
15 use Drupal\Console\Command\Shared\ConfirmationTrait;
16 use Symfony\Component\Console\Command\Command;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Extension\Manager;
19 use Drupal\Console\Core\Utils\ChainQueue;
20 use Drupal\Console\Core\Command\Shared\CommandTrait;
21
22 /**
23  * Class RouteSubscriberCommand
24  *
25  * @package Drupal\Console\Command\Generate
26  */
27 class RouteSubscriberCommand extends Command
28 {
29     use ModuleTrait;
30     use ConfirmationTrait;
31     use CommandTrait;
32
33     /**
34  * @var Manager
35 */
36     protected $extensionManager;
37
38     /**
39  * @var RouteSubscriberGenerator
40 */
41     protected $generator;
42
43     /**
44      * @var ChainQueue
45      */
46     protected $chainQueue;
47
48     /**
49      * RouteSubscriberCommand constructor.
50      *
51      * @param Manager                  $extensionManager
52      * @param RouteSubscriberGenerator $generator
53      * @param ChainQueue               $chainQueue
54      */
55     public function __construct(
56         Manager $extensionManager,
57         RouteSubscriberGenerator $generator,
58         ChainQueue $chainQueue
59     ) {
60         $this->extensionManager = $extensionManager;
61         $this->generator = $generator;
62         $this->chainQueue = $chainQueue;
63         parent::__construct();
64     }
65
66     /**
67      * {@inheritdoc}
68      */
69     protected function configure()
70     {
71         $this
72             ->setName('generate:routesubscriber')
73             ->setDescription($this->trans('commands.generate.routesubscriber.description'))
74             ->setHelp($this->trans('commands.generate.routesubscriber.description'))
75             ->addOption(
76                 'module',
77                 null,
78                 InputOption::VALUE_REQUIRED,
79                 $this->trans('commands.common.options.module')
80             )
81             ->addOption(
82                 'name',
83                 null,
84                 InputOption::VALUE_REQUIRED,
85                 $this->trans('commands.generate.routesubscriber.options.name')
86             )
87             ->addOption(
88                 'class',
89                 null,
90                 InputOption::VALUE_REQUIRED,
91                 $this->trans('commands.generate.routesubscriber.options.class')
92             );
93     }
94
95     /**
96      * {@inheritdoc}
97      */
98     protected function execute(InputInterface $input, OutputInterface $output)
99     {
100         $output = new DrupalStyle($input, $output);
101
102         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
103         if (!$this->confirmGeneration($output)) {
104             return 1;
105         }
106
107         $module = $input->getOption('module');
108         $name = $input->getOption('name');
109         $class = $input->getOption('class');
110
111         $this->generator->generate($module, $name, $class);
112
113         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
114
115         return 0;
116     }
117
118     /**
119      * {@inheritdoc}
120      */
121     protected function interact(InputInterface $input, OutputInterface $output)
122     {
123         $io = new DrupalStyle($input, $output);
124
125         // --module option
126         $module = $input->getOption('module');
127         if (!$module) {
128             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
129             $module = $this->moduleQuestion($io);
130             $input->setOption('module', $module);
131         }
132
133         // --name option
134         $name = $input->getOption('name');
135         if (!$name) {
136             $name = $io->ask(
137                 $this->trans('commands.generate.routesubscriber.questions.name'),
138                 $module.'.route_subscriber'
139             );
140             $input->setOption('name', $name);
141         }
142
143         // --class option
144         $class = $input->getOption('class');
145         if (!$class) {
146             $class = $io->ask(
147                 $this->trans('commands.generate.routesubscriber.questions.class'),
148                 'RouteSubscriber'
149             );
150             $input->setOption('class', $class);
151         }
152     }
153
154     protected function createGenerator()
155     {
156         return new RouteSubscriberGenerator();
157     }
158 }