diff --git a/src/Illuminate/Foundation/Console/EventGenerateCommand.php b/src/Illuminate/Foundation/Console/EventGenerateCommand.php index c5dfc1ae49b4..241b0993f8c4 100644 --- a/src/Illuminate/Foundation/Console/EventGenerateCommand.php +++ b/src/Illuminate/Foundation/Console/EventGenerateCommand.php @@ -29,7 +29,9 @@ class EventGenerateCommand extends Command */ public function handle() { - $provider = $this->laravel->getProvider(EventServiceProvider::class); + $this->makeDynamicListeners( + $provider = $this->laravel->getProvider(EventServiceProvider::class) + ); foreach ($provider->listens() as $event => $listeners) { $this->makeEventAndListeners($event, $listeners); @@ -38,6 +40,27 @@ public function handle() $this->info('Events and listeners generated successfully!'); } + /** + * Generate the dynamic listeners which have "hears" properties. + * + * @param object $provider + * @return void + */ + protected function makeDynamicListeners($provider) + { + foreach ($provider->listeners() as $listener) { + if (! class_exists($listener)) { + $this->makeListeners(null, [$listener]); + + continue; + } + + foreach ($listener::$hears as $event) { + $this->makeEventAndListeners($event, [$listener]); + } + } + } + /** * Make the event and listeners for the given event. * @@ -68,7 +91,9 @@ protected function makeListeners($event, $listeners) foreach ($listeners as $listener) { $listener = preg_replace('/@.+$/', '', $listener); - $this->callSilent('make:listener', ['name' => $listener, '--event' => $event]); + $this->callSilent('make:listener', array_filter( + ['name' => $listener, '--event' => $event] + )); } } } diff --git a/src/Illuminate/Foundation/Console/ListenerMakeCommand.php b/src/Illuminate/Foundation/Console/ListenerMakeCommand.php index c92a0e17b0e0..bcf6892c7119 100644 --- a/src/Illuminate/Foundation/Console/ListenerMakeCommand.php +++ b/src/Illuminate/Foundation/Console/ListenerMakeCommand.php @@ -29,20 +29,6 @@ class ListenerMakeCommand extends GeneratorCommand */ protected $type = 'Listener'; - /** - * Execute the console command. - * - * @return void - */ - public function handle() - { - if (! $this->option('event')) { - return $this->error('Missing required option: --event'); - } - - parent::handle(); - } - /** * Build the class with the given name. * @@ -78,9 +64,13 @@ protected function buildClass($name) protected function getStub() { if ($this->option('queued')) { - return __DIR__.'/stubs/listener-queued.stub'; + return $this->option('event') + ? __DIR__.'/stubs/listener-queued.stub' + : __DIR__.'/stubs/listener-queued-duck.stub'; } else { - return __DIR__.'/stubs/listener.stub'; + return $this->option('event') + ? __DIR__.'/stubs/listener.stub' + : __DIR__.'/stubs/listener-duck.stub'; } } @@ -114,7 +104,7 @@ protected function getDefaultNamespace($rootNamespace) protected function getOptions() { return [ - ['event', 'e', InputOption::VALUE_REQUIRED, 'The event class being listened for.'], + ['event', 'e', InputOption::VALUE_OPTIONAL, 'The event class being listened for.'], ['queued', null, InputOption::VALUE_NONE, 'Indicates the event listener should be queued.'], ]; diff --git a/src/Illuminate/Foundation/Console/stubs/listener-duck.stub b/src/Illuminate/Foundation/Console/stubs/listener-duck.stub new file mode 100644 index 000000000000..a8b88de19879 --- /dev/null +++ b/src/Illuminate/Foundation/Console/stubs/listener-duck.stub @@ -0,0 +1,39 @@ +listeners)->flatMap(function ($listener) { + return array_merge_recursive(collect($this->listeners)->filter(function ($listener) { + return class_exists($listener, false); + })->flatMap(function ($listener) { return collect($listener::$hears)->map(function ($event) use ($listener) { return ['event' => $event, 'listeners' => [$listener]]; })->all(); @@ -62,4 +71,14 @@ public function listens() return $listeners->pluck('listeners')->flatten(1)->all(); })->all(), $this->listen); } + + /** + * Get all of the registered listeners. + * + * @return array + */ + public function listeners() + { + return $this->listeners; + } }