1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelZero\Framework\Commands\Component\Illuminate\Events; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
|
8
|
|
|
class EventGenerateCommand extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The console command name. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $name = 'event:generate'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Generate the missing events and listeners based on registration'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Execute the console command. |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function handle() |
30
|
|
|
{ |
31
|
|
|
$class = $this->laravel->getNamespace(); |
32
|
|
|
$class = $class . '\\Providers\\EventServiceProvider'; |
33
|
|
|
$provider = $this->laravel->make($class, ['app' => $this->getApplication()]); |
34
|
|
|
|
35
|
|
|
foreach ($provider->listens() as $event => $listeners) { |
36
|
|
|
$this->makeEventAndListeners($event, $listeners); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->info('Events and listeners generated successfully!'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Make the event and listeners for the given event. |
44
|
|
|
* |
45
|
|
|
* @param string $event |
46
|
|
|
* @param array $listeners |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
protected function makeEventAndListeners($event, $listeners) |
50
|
|
|
{ |
51
|
|
|
if (! Str::contains($event, '\\')) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->callSilent('make:event', ['name' => $event]); |
56
|
|
|
|
57
|
|
|
$this->makeListeners($event, $listeners); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Make the listeners for the given event. |
62
|
|
|
* |
63
|
|
|
* @param string $event |
64
|
|
|
* @param array $listeners |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
protected function makeListeners($event, $listeners) |
68
|
|
|
{ |
69
|
|
|
foreach ($listeners as $listener) { |
70
|
|
|
$listener = preg_replace('/@.+$/', '', $listener); |
71
|
|
|
|
72
|
|
|
$this->callSilent('make:listener', array_filter( |
73
|
|
|
['name' => $listener, '--event' => $event] |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|