[12.x] Add scoped macros to Macroable #56214
Open
+105
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds the ability to scope macros to the class on which they were defined.
I've added separate
scopedMacro
andhasScopedMacro
methods to make it clearly distinguishable when used andhasGlobalMacro
as a method to check the contents of the$macros
property specifically.hasMacro
will check both the default macros and$scopedMacros
for the given macroIf a
scopedMacro
is when the method is invoked, it will be preferred over a global one (which has the effect of also allowing a user to override a macro for a given implementation without breaking others).The benefit of this is the ability to add a macro to a class and have it not invokable on another class which extends it.
For example:
A package "Ape" provides a base class which extends
Livewire/Component
(which is macroable)Another package "Baboon" extends this package's base class to provide its own functionality and implements a method whereby macros defined using a specific name e.g.
afterRender
If I define
afterRender
on a class which extends "Ape", that macro is also available on "Baboon" which may be unintended.A more real world example can bee seen in the FilamentPHP PR here filamentphp/filament#16763 where implementing the
beforeFill
macro onEditPage
results in it being invoked onViewPage
also since that extendsBasePage
and uses the same form for display.