Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Console;
4
 
5
use Closure;
6
use Illuminate\Console\Command;
7
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
8
use Illuminate\Contracts\Queue\ShouldQueue;
9
use ReflectionFunction;
10
use Symfony\Component\Console\Attribute\AsCommand;
11
 
12
#[AsCommand(name: 'event:list')]
13
class EventListCommand extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'event:list {--event= : Filter the events by name}';
21
 
22
    /**
23
     * The name of the console command.
24
     *
25
     * This name is used to identify the command during lazy loading.
26
     *
27
     * @var string|null
28
     *
29
     * @deprecated
30
     */
31
    protected static $defaultName = 'event:list';
32
 
33
    /**
34
     * The console command description.
35
     *
36
     * @var string
37
     */
38
    protected $description = "List the application's events and listeners";
39
 
40
    /**
41
     * The events dispatcher resolver callback.
42
     *
43
     * @var \Closure|null
44
     */
45
    protected static $eventsResolver;
46
 
47
    /**
48
     * Execute the console command.
49
     *
50
     * @return void
51
     */
52
    public function handle()
53
    {
54
        $events = $this->getEvents()->sortKeys();
55
 
56
        if ($events->isEmpty()) {
57
            $this->components->info("Your application doesn't have any events matching the given criteria.");
58
 
59
            return;
60
        }
61
 
62
        $this->newLine();
63
 
64
        $events->each(function ($listeners, $event) {
65
            $this->components->twoColumnDetail($this->appendEventInterfaces($event));
66
            $this->components->bulletList($listeners);
67
        });
68
 
69
        $this->newLine();
70
    }
71
 
72
    /**
73
     * Get all of the events and listeners configured for the application.
74
     *
75
     * @return \Illuminate\Support\Collection
76
     */
77
    protected function getEvents()
78
    {
79
        $events = collect($this->getListenersOnDispatcher());
80
 
81
        if ($this->filteringByEvent()) {
82
            $events = $this->filterEvents($events);
83
        }
84
 
85
        return $events;
86
    }
87
 
88
    /**
89
     * Get the event / listeners from the dispatcher object.
90
     *
91
     * @return array
92
     */
93
    protected function getListenersOnDispatcher()
94
    {
95
        $events = [];
96
 
97
        foreach ($this->getRawListeners() as $event => $rawListeners) {
98
            foreach ($rawListeners as $rawListener) {
99
                if (is_string($rawListener)) {
100
                    $events[$event][] = $this->appendListenerInterfaces($rawListener);
101
                } elseif ($rawListener instanceof Closure) {
102
                    $events[$event][] = $this->stringifyClosure($rawListener);
103
                } elseif (is_array($rawListener) && count($rawListener) === 2) {
104
                    if (is_object($rawListener[0])) {
105
                        $rawListener[0] = get_class($rawListener[0]);
106
                    }
107
 
108
                    $events[$event][] = $this->appendListenerInterfaces(implode('@', $rawListener));
109
                }
110
            }
111
        }
112
 
113
        return $events;
114
    }
115
 
116
    /**
117
     * Add the event implemented interfaces to the output.
118
     *
119
     * @param  string  $event
120
     * @return string
121
     */
122
    protected function appendEventInterfaces($event)
123
    {
124
        if (! class_exists($event)) {
125
            return $event;
126
        }
127
 
128
        $interfaces = class_implements($event);
129
 
130
        if (in_array(ShouldBroadcast::class, $interfaces)) {
131
            $event .= ' <fg=bright-blue>(ShouldBroadcast)</>';
132
        }
133
 
134
        return $event;
135
    }
136
 
137
    /**
138
     * Add the listener implemented interfaces to the output.
139
     *
140
     * @param  string  $listener
141
     * @return string
142
     */
143
    protected function appendListenerInterfaces($listener)
144
    {
145
        $listener = explode('@', $listener);
146
 
147
        $interfaces = class_implements($listener[0]);
148
 
149
        $listener = implode('@', $listener);
150
 
151
        if (in_array(ShouldQueue::class, $interfaces)) {
152
            $listener .= ' <fg=bright-blue>(ShouldQueue)</>';
153
        }
154
 
155
        return $listener;
156
    }
157
 
158
    /**
159
     * Get a displayable string representation of a Closure listener.
160
     *
161
     * @param  \Closure  $rawListener
162
     * @return string
163
     */
164
    protected function stringifyClosure(Closure $rawListener)
165
    {
166
        $reflection = new ReflectionFunction($rawListener);
167
 
168
        $path = str_replace([base_path(), DIRECTORY_SEPARATOR], ['', '/'], $reflection->getFileName() ?: '');
169
 
170
        return 'Closure at: '.$path.':'.$reflection->getStartLine();
171
    }
172
 
173
    /**
174
     * Filter the given events using the provided event name filter.
175
     *
176
     * @param  \Illuminate\Support\Collection  $events
177
     * @return \Illuminate\Support\Collection
178
     */
179
    protected function filterEvents($events)
180
    {
181
        if (! $eventName = $this->option('event')) {
182
            return $events;
183
        }
184
 
185
        return $events->filter(
186
            fn ($listeners, $event) => str_contains($event, $eventName)
187
        );
188
    }
189
 
190
    /**
191
     * Determine whether the user is filtering by an event name.
192
     *
193
     * @return bool
194
     */
195
    protected function filteringByEvent()
196
    {
197
        return ! empty($this->option('event'));
198
    }
199
 
200
    /**
201
     * Gets the raw version of event listeners from the event dispatcher.
202
     *
203
     * @return array
204
     */
205
    protected function getRawListeners()
206
    {
207
        return $this->getEventsDispatcher()->getRawListeners();
208
    }
209
 
210
    /**
211
     * Get the event dispatcher.
212
     *
213
     * @return Illuminate\Events\Dispatcher
214
     */
215
    public function getEventsDispatcher()
216
    {
217
        return is_null(self::$eventsResolver)
218
            ? $this->getLaravel()->make('events')
219
            : call_user_func(self::$eventsResolver);
220
    }
221
 
222
    /**
223
     * Set a callback that should be used when resolving the events dispatcher.
224
     *
225
     * @param  \Closure|null  $resolver
226
     * @return void
227
     */
228
    public static function resolveEventsUsing($resolver)
229
    {
230
        static::$eventsResolver = $resolver;
231
    }
232
}