Repository navigation
Vendor-scope the Str macros and add a registry-free Identifiers API - #5
Merged
Merged
Conversation
Str's macro registry is one flat static map keyed by name, so the bare
Str::sku(), Str::orderNumber() and Str::ticketNumber() could be silently
replaced by any other package or the host application. Worse, the model
generators called those macros, so such a registration changed every SKU,
order number and ticket number the traits wrote.
Supports\Identifiers now holds the logic as a final class with no registry
in the path, and the shipped generators call it directly. The macros are
registered as Str::simtabiLacommerce{Sku,OrderNumber,TicketNumber}() with
the bare signatures. The bare names stay registered as deprecated aliases
that forward with the same arguments and output and raise one
E_USER_DEPRECATED per name per boot; register_legacy_macros (default true)
lets a host stop registering them. Earliest removal: 0.2.0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Why
Str's macro registry is a flat static map (Macroable::macro()isstatic::$macros[$name] = $macro;), so the bareStr::sku(),Str::orderNumber()andStr::ticketNumber()registered in 0.1.0 are silently replaced by any other package or application registering the same names, and vice versa. The model generators also resolved their values through those macros (Str::{$this->strMixin}()), so such a registration changed what everyHasSku/HasOrderNumber/HasTicketNumbermodel wrote.What
Simtabi\Lacommerce\Supports\Identifiers: a final class with staticsku(),orderNumber()andticketNumber(), with no registry in the path. It is now the primary API, and the shipped generators call it directly. A custom generator naming another$strMixinstill has that macro called.Str::simtabiLacommerceSku(),Str::simtabiLacommerceOrderNumber()andStr::simtabiLacommerceTicketNumber(), with the bare signatures, so migrating is a rename. Vendor and package are both in the name, and camel case is used because a macro name is called as a PHP method and cannot hold/or-.E_USER_DEPRECATEDper name per boot. Earliest removal: 0.2.0.register_legacy_macros(defaulttrue) lets a host stop registering the bare names.Supports\StrMacros. No name collides with a realStrmethod on Laravel 12.69 (a test asserts this).Behaviour
XXX-+ 10-digit shape, the configured-separator fallback for an empty or null separator,orderNumberignoring$sourceand defaulting toORD, and the ignored$prefixonsku/ticketNumber.Str::sku()etc. An application that deliberately overrode a bare macro to customise generated values loses that effect. The documented customisation point, thegeneratorconfig class, is unaffected.Tests
48 tests (up from 22). The new ones cover the pinned bare behaviour,
Identifiers, the scoped macros, alias delegation (scoped macros replaced, bare follow), one deprecation per name per boot, the opt-out flag (traits still generate with the bare names unregistered), and that a hijackedStr::skuno longer changes trait output. I checked that two guards fail under mutation: pointing the generator back atStr::skufails the hijack and opt-out tests, and defaulting the flag tofalsefails 12 tests.