83cb54b4a3e5806bee7def21768a4842ac95dfa1
[yaffs-website] / vendor / drupal / console / src / Command / Shared / EventsTrait.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\Console\Command\Shared\EventsTrait.
6  */
7
8 namespace Drupal\Console\Command\Shared;
9
10 use Drupal\Console\Core\Style\DrupalStyle;
11
12 /**
13  * Class EventsTrait
14  *
15  * @package Drupal\Console\Command
16  */
17 trait EventsTrait
18 {
19     /**
20      * @param DrupalStyle $io
21      *
22      * @return mixed
23      */
24     public function eventsQuestion(DrupalStyle $io)
25     {
26         $eventCollection = [];
27         $io->info($this->trans('commands.common.questions.events.message'));
28
29         $events = $this->getEvents();
30
31         while (true) {
32             $event = $io->choiceNoList(
33                 $this->trans('commands.common.questions.events.name'),
34                 $events,
35                 null,
36                 true
37             );
38
39             if (empty($event)) {
40                 break;
41             }
42
43             $callbackSuggestion = str_replace('.', '_', $event);
44             $callback = $io->ask(
45                 $this->trans('commands.generate.event.subscriber.questions.callback-name'),
46                 $callbackSuggestion
47             );
48
49             $eventCollection[$event] = $callback;
50             $eventKey = array_search($event, $events, true);
51
52             if ($eventKey >= 0) {
53                 unset($events[$eventKey]);
54             }
55         }
56
57         return $eventCollection;
58     }
59
60     public function getEvents()
61     {
62         if (null === $this->events) {
63             $this->events = [];
64             $this->events = array_keys($this->eventDispatcher->getListeners());
65         }
66         return $this->events;
67     }
68 }