Skip to content

Commit

Permalink
fix generate
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jul 6, 2017
1 parent 6439ae4 commit 4d557c5
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 20 deletions.
29 changes: 27 additions & 2 deletions src/Illuminate/Foundation/Console/EventGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
*
Expand Down Expand Up @@ -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]
));
}
}
}
24 changes: 7 additions & 17 deletions src/Illuminate/Foundation/Console/ListenerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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';
}
}

Expand Down Expand Up @@ -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.'],
];
Expand Down
39 changes: 39 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/listener-duck.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace DummyNamespace;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class DummyClass
{
/**
* The events handled by the listener.
*
* @var array
*/
public static $hears = [
//
];

/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
//
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace DummyNamespace;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class DummyClass implements ShouldQueue
{
use InteractsWithQueue;

/**
* The events handled by the listener.
*
* @var array
*/
public static $hears = [
//
];

/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
//
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class EventServiceProvider extends ServiceProvider
*/
protected $listen = [];

/**
* The event listeners for the application.
*
* @var array
*/
protected $listeners = [];

/**
* The subscriber classes to register.
*
Expand Down Expand Up @@ -54,12 +61,24 @@ public function register()
*/
public function listens()
{
return array_merge_recursive(collect($this->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();
})->groupBy('event')->map(function ($listeners) {
return $listeners->pluck('listeners')->flatten(1)->all();
})->all(), $this->listen);
}

/**
* Get all of the registered listeners.
*
* @return array
*/
public function listeners()
{
return $this->listeners;
}
}

0 comments on commit 4d557c5

Please sign in to comment.