Skip to content

Comments

Add extendable.afterConstruct event#112

Closed
mjauvin wants to merge 4 commits intodevelopfrom
add-extendable-after-construct-event
Closed

Add extendable.afterConstruct event#112
mjauvin wants to merge 4 commits intodevelopfrom
add-extendable-after-construct-event

Conversation

@mjauvin
Copy link
Member

@mjauvin mjauvin commented Sep 7, 2022

The event fires once the behaviors have been loaded.

@mjauvin
Copy link
Member Author

mjauvin commented Sep 7, 2022

@bennothommo can we add a fireEvent() method to the mockup or something?

@mjauvin mjauvin requested a review from LukeTowers September 7, 2022 21:56
@LukeTowers
Copy link
Member

@bennothommo @jaxwilko any input on this?

@bennothommo
Copy link
Member

@mjauvin @LukeTowers I just want to be sure that you are wanting this to fire on every single instance of an extendable class (not just models)?

@LukeTowers
Copy link
Member

@bennothommo yes, in my case that I needed it for I wanted to be able to call addAssets() on a widget implementing a behaviour to implement the required assets for that behaviour so long as it's conditions for enabling itself were met. However, I was unable to call the methods that behaviour added to the widget (which were required to determine if it was eligible to run and thus add the assets in the constructor of the behaviour.

Perhaps we could also implement this by calling init() or boot() on the behavior after all the behaviours are registered instead of an event? @mjauvin would that work for your use case?

@bennothommo
Copy link
Member

bennothommo commented Sep 8, 2022

@LukeTowers personally, I'd introduce another parameter to extend to specify that a callback should be run after the implements have been applied. That way, we're not doing an event on every single instance.

@LukeTowers
Copy link
Member

@bennothommo I was thinking more along the lines of Laravel's bootable Eloquent traits, i.e. the behaviour would add a method called bootNameOfClass() that gets called after all the behaviours have been registered (see https://andy-carter.com/blog/using-laravel-s-eloquent-traits). That way there's no added complexity to the API and there's no extra overhead of the events system.

@bennothommo
Copy link
Member

@LukeTowers that's a much more wild change to the current API than my suggestion though 😜

@LukeTowers
Copy link
Member

Is it? It's fairly unobtrusive IMO, it's an optional method that be implemented by the behaviour to hook into after all of the behaviours have been registered.

@bennothommo
Copy link
Member

How would you propose that someone leverages it? Would it be part of an extend callback, or another method altogether?

My suggestion is simply an extra parameter to extend, with a default that keeps BC. I'd argue it's more graceful.

@LukeTowers
Copy link
Member

I don't remember what @mjauvin's use case of it was, but it wasn't so much external usage of it but instead internal usage by the behaviour itself in order to do setup tasks that require the method proxying to be in place on the object that's implementing them before they can do certain tasks.

@mjauvin
Copy link
Member Author

mjauvin commented Sep 8, 2022

My use case is to be able to call behavior's methods within the extend() callback. So @bennothommo's suggestion would work for me I believe.

@mjauvin
Copy link
Member Author

mjauvin commented Sep 8, 2022

I'd like to be able to do the following:

class MyModel extends Model {
    public $implement = ['MyBehavior'];
}

MyModel::extend(function ($model) {
    $model->myBeaviorMethod();
})

@mjauvin
Copy link
Member Author

mjauvin commented Sep 8, 2022

Replaced by #113

@mjauvin mjauvin closed this Sep 8, 2022
@mjauvin mjauvin deleted the add-extendable-after-construct-event branch September 8, 2022 04:22
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.

3 participants