bb8ce10ea0db2c37762e37aee7f04d6d84a90239
[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 /**
11  * Class EventsTrait
12  *
13  * @package Drupal\Console\Command
14  */
15 trait EventsTrait
16 {
17     /**
18      * @return mixed
19      */
20     public function eventsQuestion()
21     {
22         $eventCollection = [];
23         $this->getIo()->info($this->trans('commands.common.questions.events.message'));
24
25         $events = $this->getEvents();
26
27         while (true) {
28             $event = $this->getIo()->choiceNoList(
29                 $this->trans('commands.common.questions.events.name'),
30                 $events,
31                 '',
32                 true
33             );
34
35             if (empty($event) || is_numeric($event)) {
36                 break;
37             }
38
39             $callbackSuggestion = str_replace('.', '_', $event);
40             $callback = $this->getIo()->ask(
41                 $this->trans('commands.generate.event.subscriber.questions.callback-name'),
42                 $callbackSuggestion
43             );
44
45             $eventCollection[$event] = $callback;
46             $eventKey = array_search($event, $events, true);
47
48             if ($eventKey >= 0) {
49                 unset($events[$eventKey]);
50             }
51         }
52
53         return $eventCollection;
54     }
55
56     public function getEvents()
57     {
58         if (null === $this->events) {
59             $this->events = [];
60             $this->events = array_keys($this->eventDispatcher->getListeners());
61         }
62         return $this->events;
63     }
64 }