Skip to content

[12.x] Add scoped macros to Macroable #56214

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

Closed
wants to merge 3 commits into from

Conversation

Perturbatio
Copy link
Contributor

@Perturbatio Perturbatio commented Jul 4, 2025

This adds the ability to scope macros to the class on which they were defined.

I've added separate scopedMacro and hasScopedMacro methods to make it clearly distinguishable when used and hasGlobalMacro as a method to check the contents of the $macros property specifically.

hasMacro will check both the default macros and $scopedMacros for the given macro

If a scopedMacro is present 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 on EditPage results in it being invoked on ViewPage also since that extends BasePage and uses the same form for display.

* adding scoped macros to Macroable
@Perturbatio
Copy link
Contributor Author

I ran pint on the codebase prior to pushing and it wanted to change more files than just mine, I reverted all but the two I had modified to keep the PR restricted to them. Is there a tool I should be running rather than pint for code style?

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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