Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ListenerMakeCommand.php #19660

Merged
merged 3 commits into from
Jun 19, 2017
Merged

Update ListenerMakeCommand.php #19660

merged 3 commits into from
Jun 19, 2017

Conversation

zanozik
Copy link
Contributor

@zanozik zanozik commented Jun 18, 2017

Allow to make listeners for the events outside of App or Illuminate namespaces, passing event class with absolute path:

php artisan make:listener UserCustomListener -e "\Vendor\Package\Events\PackageEvent"

Allow to make listeners for the events outside of App or Illuminate namespaces
@@ -54,7 +54,8 @@ protected function buildClass($name)
$event = $this->option('event');

if (! Str::startsWith($event, $this->laravel->getNamespace()) &&
! Str::startsWith($event, 'Illuminate')) {
! Str::startsWith($event, 'Illuminate') &&
! Str::startsWith($event, '\\')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why calling Str::startsWith 3 times? You can call it only once:

$prefixes = [
    $this->laravel->getNamespace(),
    'Illuminate',
    '\\',
];

if (! Str::startsWith($event, $prefixes) {
// ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was called 2 times, so I figured this is a standard practice in Laravel...
Could we call it with anonymous array, to signify that it will not be used anywhere else?

if (! Str::startsWith($event, [
    $this->laravel->getNamespace(),
    'Illuminate',
    '\\',
]) {
// ...

@taylorotwell
Copy link
Member

So where does this actually place the generated event?

@zanozik
Copy link
Contributor Author

zanozik commented Jun 18, 2017

@taylorotwell
make:listener only makes a listener for defined event. You thinking event:generate, maybe?
In any case, I find it very useful, if I create an event in my package and fire it when a package needs it letting a user create a custom listener by running php artisan make:listener UserCustomListener -e "\Vendor\Package\Events\MyPackageEvent".
I found it strange that we can make an outside listener only for Illuminate*, even though there is no restriction on listeners created manually...

Pass all prefixes to `Str::startWith()` in anonymous array. instead of calling it 3 times (even more useful if more prefixes will be added later on).
@taylorotwell taylorotwell merged commit 346d352 into laravel:master Jun 19, 2017
@zanozik zanozik deleted the patch-1 branch June 19, 2017 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants