Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / EventSubscriberCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\EventSubscriberCommand.
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\ModuleTrait;
15 use Drupal\Console\Generator\EventSubscriberGenerator;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Drupal\Console\Command\Shared\EventsTrait;
18 use Symfony\Component\Console\Command\Command;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
21 use Drupal\Console\Core\Utils\StringConverter;
22 use Drupal\Console\Extension\Manager;
23 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24 use Drupal\Console\Core\Utils\ChainQueue;
25
26 class EventSubscriberCommand extends Command
27 {
28     use EventsTrait;
29     use ServicesTrait;
30     use ModuleTrait;
31     use ConfirmationTrait;
32     use ContainerAwareCommandTrait;
33
34     /**
35      * @var Manager
36      */
37     protected $extensionManager;
38
39     /**
40      * @var EventSubscriberGenerator
41      */
42     protected $generator;
43
44     /**
45      * @var StringConverter
46      */
47     protected $stringConverter;
48
49     /**
50      * @var EventDispatcherInterface
51      */
52     protected $eventDispatcher;
53
54     /**
55      * @var ChainQueue
56      */
57     protected $chainQueue;
58
59     /**
60      * EventSubscriberCommand constructor.
61      *
62      * @param Manager                  $extensionManager
63      * @param EventSubscriberGenerator $generator
64      * @param StringConverter          $stringConverter
65      * @param EventDispatcherInterface $eventDispatcher
66      * @param ChainQueue               $chainQueue
67      */
68     public function __construct(
69         Manager $extensionManager,
70         EventSubscriberGenerator $generator,
71         StringConverter $stringConverter,
72         EventDispatcherInterface $eventDispatcher,
73         ChainQueue $chainQueue
74     ) {
75         $this->extensionManager = $extensionManager;
76         $this->generator = $generator;
77         $this->stringConverter = $stringConverter;
78         $this->eventDispatcher = $eventDispatcher;
79         $this->chainQueue = $chainQueue;
80         parent::__construct();
81     }
82
83     /**
84      * {@inheritdoc}
85      */
86     protected function configure()
87     {
88         $this
89             ->setName('generate:event:subscriber')
90             ->setDescription($this->trans('commands.generate.event.subscriber.description'))
91             ->setHelp($this->trans('commands.generate.event.subscriber.description'))
92             ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
93             ->addOption(
94                 'name',
95                 null,
96                 InputOption::VALUE_OPTIONAL,
97                 $this->trans('commands.generate.service.options.name')
98             )
99             ->addOption(
100                 'class',
101                 null,
102                 InputOption::VALUE_OPTIONAL,
103                 $this->trans('commands.generate.service.options.class')
104             )
105             ->addOption(
106                 'events',
107                 null,
108                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
109                 $this->trans('commands.common.options.events')
110             )
111             ->addOption(
112                 'services',
113                 null,
114                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
115                 $this->trans('commands.common.options.services')
116             );
117     }
118
119     /**
120      * {@inheritdoc}
121      */
122     protected function execute(InputInterface $input, OutputInterface $output)
123     {
124         $io = new DrupalStyle($input, $output);
125
126         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
127         if (!$this->confirmGeneration($io)) {
128             return 1;
129         }
130
131         $module = $input->getOption('module');
132         $name = $input->getOption('name');
133         $class = $input->getOption('class');
134         $events = $input->getOption('events');
135         $services = $input->getOption('services');
136
137         // @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
138         $buildServices = $this->buildServices($services);
139
140         $this->generator->generate($module, $name, $class, $events, $buildServices);
141
142         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
143     }
144
145     /**
146      * {@inheritdoc}
147      */
148     protected function interact(InputInterface $input, OutputInterface $output)
149     {
150         $io = new DrupalStyle($input, $output);
151
152         // --module option
153         $module = $input->getOption('module');
154         if (!$module) {
155             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
156             $module = $this->moduleQuestion($io);
157             $input->setOption('module', $module);
158         }
159
160         // --service-name option
161         $name = $input->getOption('name');
162         if (!$name) {
163             $name = $io->ask(
164                 $this->trans('commands.generate.service.questions.service-name'),
165                 sprintf('%s.default', $module)
166             );
167             $input->setOption('name', $name);
168         }
169
170         // --class option
171         $class = $input->getOption('class');
172         if (!$class) {
173             $class = $io->ask(
174                 $this->trans('commands.generate.event.subscriber.questions.class'),
175                 'DefaultSubscriber'
176             );
177             $input->setOption('class', $class);
178         }
179
180         // --events option
181         $events = $input->getOption('events');
182         if (!$events) {
183             // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
184             $events = $this->eventsQuestion($io);
185             $input->setOption('events', $events);
186         }
187
188         // --services option
189         $services = $input->getOption('services');
190         if (!$services) {
191             // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
192             $services = $this->servicesQuestion($io);
193             $input->setOption('services', $services);
194         }
195     }
196 }