Skip to content

[5.8] Fix incorrect event namespace in generated listener.#28007

Merged
taylorotwell merged 1 commit intolaravel:5.8from
devcircus:fix-event-generate-namespace-in-listener
Mar 26, 2019
Merged

[5.8] Fix incorrect event namespace in generated listener.#28007
taylorotwell merged 1 commit intolaravel:5.8from
devcircus:fix-event-generate-namespace-in-listener

Conversation

@devcircus
Copy link
Copy Markdown
Contributor

@devcircus devcircus commented Mar 25, 2019

Problem Summary

See #27468
The issue arises when running event:generate for events in third-party namespaces (non-App and non-Illuminate).
The FQCN of the event (in the "use" statement at the top of the Listener class) has a preceding '\'.

Before

<?php

namespace App\Listeners;

use \GreatLaravelPackages\GreatePackage\GreatEvent;

class MyGreatListener
{
}

After

<?php

namespace App\Listeners;

use GreatLaravelPackages\GreatePackage\GreatEvent;

class MyGreatListener
{
}

Details

In ListenerMakeCommand, there is a check to see if the given event namespace starts with:
(1) your project namespace(App)
(2)"Illuminate"
(3)"\"

So it looks like the event:generate command expects 3 situations:

  1. Your event is somewhere in your project's namespace (App).
  2. Your event is in the Illuminate namespace.
  3. Your event is elsewhere and you are showing that by starting with a '\'.

If those assumptions are true, then you should be able to generate listeners from events in third party namespaces by adding a starting '\' to your event in the $listen array of your EventServiceProvider.

However, if you do that, then the '\' doesn't get dropped and the namespace at the top of the generated listener for your event, will still have the starting '\'.

Solution

Simply trim the full event name when replacing the DummyFullEvent.

@devcircus devcircus changed the title Fix incorrect event namespace in generated listener. [5.8] Fix incorrect event namespace in generated listener. Mar 25, 2019
@devcircus
Copy link
Copy Markdown
Contributor Author

devcircus commented Mar 25, 2019

The reason for assumption # 3 above is that if you have a third party namespaced event, and you include it in your EventServiceProvider without the starting '\', it will fallback to using the "App\Events" namespace for this event, which of course is incorrect as well. So adding the "\" to the beginning of the namespace for third-party events, is the only route that made sense to me.

@taylorotwell taylorotwell merged commit 2832d2f into laravel:5.8 Mar 26, 2019
@devcircus devcircus deleted the fix-event-generate-namespace-in-listener branch March 26, 2019 17:25
@TBlindaruk
Copy link
Copy Markdown
Contributor

@devcircus
#28027 - I have created issue for the tests

@devcircus
Copy link
Copy Markdown
Contributor Author

Thanks. I’ll look into some tests asap.

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.

4 participants