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

Allow invokeable event listeners #600

Merged

Conversation

doekenorg
Copy link
Contributor

Hi,

There is a slight issue with the EventBus. When you add a listener to the event collection, it creates a new collection by merging with a collect($arguments[0]) call. This has some unforeseen side effects when you use an invokable event listener with an internal object.

$events->afterCollections(new class {
    private $object;

    public function __construct()
    {
        $this->object = (object) [];
    }

    public function __invoke(Jigsaw $jigsaw) {
        dump($jigsaw);
    }
});

This example will currently throw an error:

PHP Fatal error:  Uncaught Error: Call to undefined method stdClass::handle() in /Users/doekenorg/code/doekenorg/vendor/tightenco/jigsaw/src/Events/EventBus.php:33

This update will fix that problem, by just appending the first argument to the collection that already exists. No merging required.

@doekenorg
Copy link
Contributor Author

Sorry about that; I somehow completely missed the unit tests. Luckily they exists.

So the actual problem is that collect() just casts the input to an array; which is why this object is interpreted incorrectly.
To fix this, I wrap the listeners in an array, if they are not already.

Also added a unit test for this use case.

@bakerkretzmar
Copy link
Collaborator

@doekenorg thanks for updating this, I was just about to comment the same thing 😅

@doekenorg
Copy link
Contributor Author

@bakerkretzmar nice. I knew there probably was a nicer way; but I'm not thát familiar with the framework.

@bakerkretzmar bakerkretzmar merged commit 4d1b3d7 into tighten:main Nov 28, 2021
@doekenorg doekenorg deleted the allow_invokeable_event_listeners branch November 28, 2021 19:20
@bakerkretzmar
Copy link
Collaborator

Thank you! 🙌🏻

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.

2 participants