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

[5.6] Register many observers for a model #23507

Merged
merged 4 commits into from Mar 13, 2018

Conversation

Projects
None yet
6 participants
@Alymosul
Contributor

Alymosul commented Mar 12, 2018

This PR adds the ability to pass an array of observers to the observe() method for a model instead of registering each of them one by one.
Consider we have a Thread model and we will register the following observers for it: DeleteThreadRepliesObserver, GenerateThreadSlugObserver, ReputationObserver, ThreadObserver

Currently:
\App\Thread::observe(\App\Observers\DeleteThreadRepliesObserver::class);
\App\Thread::observe(\App\Observers\GenerateThreadSlugObserver::class);
\App\Thread::observe(\App\Observers\ReputationObserver::class);
\App\Thread::observe(\App\Observers\ThreadObserver::class);
Proposed:
\App\Thread::observe([
\App\Observers\DeleteThreadRepliesObserver::class,
\App\Observers\GenerateThreadSlugObserver::class,
\App\Observers\ReputationObserver::class,
\App\Observers\ThreadObserver::class
]);
@ntzm

This comment has been minimized.

Contributor

ntzm commented Mar 13, 2018

This is nice, I've definitely needed this before! However, could you possibly add tests? Thanks

@devcircus

This comment has been minimized.

Contributor

devcircus commented Mar 13, 2018

I second this. Though I personally no longer use observers in most cases. It makes sense. Tests would be nice.

@ludo237

This comment has been minimized.

ludo237 commented Mar 13, 2018

@devcircus is there a better alternative to observers?

@Alymosul I think you should add tests in order to verify that someone can actually use the old version and the new one

Alymosul added some commits Mar 13, 2018

@taylorotwell taylorotwell merged commit 4f8bc0e into laravel:5.6 Mar 13, 2018

2 checks passed

continuous-integration/styleci/pr The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@afraca

This comment has been minimized.

Contributor

afraca commented Mar 14, 2018

Nice addition.
Nitpick: I know this is already merged, but perhaps we could make the registerObserver method protected to be in line with the rest of the model and be easier to override?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment