Repository navigation
Make custom generators writable and restore the random-string prefix - #6
Merged
imanimanyara merged 1 commit intoOct 6, 2026
Merged
Conversation
The generator docs told users to extend SkuGenerator, which is final, so the documented example could not compile. Document the two seams that work, extending the non-final Generator base or implementing the interface, and run both as test fixtures; a test fails if the docs stop matching. The observer now calls render() rather than casting to string, so an interface-only generator works, the provider rejects a configured class that is not a generator with the config key named, and the shipped generators implement the per-type interface they are resolved through. Helpers::makeRandomString read a $prefix it never declared: the body was lifted out of the 2022 Str macros, where $prefix was a closure parameter. Add it as an optional third parameter, thread it through Identifiers and a new per-generator `prefix` config key, and default Configs::$prefix to null so getPrefix() no longer throws before setPrefix().
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.
Summary
Two defects, plus three latent ones of the same kind found on the way.
Custom generators could not be written as documented.
docs/tools/generators.mdsaid to extendSkuGenerator, which isfinal. The shipped generators stay final; the docs now describe the two seams that work:Generators\Services\Generatorbase (itsgetSourceString(),makeValue(),exists()andgenerate()are protected hooks, and uniqueness keeps working), orSkuGeneratorInterface/OrderNumberGeneratorInterface/TicketNumberGeneratorInterfacewith a one-argument(Model $model)constructor andrender().Both examples are test fixtures configured through
generator.<name>.generatorand used by a model with the trait. A test fails if the documented example stops matching its fixture (proven by editing the doc and watching it go red).Making the second seam real needed two fixes: the observer cast the generator to a string, which threw for any generator without
__toString(), so it now callsrender(); and the provider now rejects a configured class that does not implementGeneratorInterfacewith anInvalidOptionExceptionnaming the config key, instead of aTypeErrorfrom the observer. The shipped generators now implement the per-type interface they are resolved through.Helpers::makeRandomString()read an undefined$prefix. Commit 5a0461b (2022) lifted the body out of theStrmacros, where$prefixwas a closure parameter, and left the parameter behind.empty()of an undefined variable raises nothing, so the branch was dead. It is now?string $prefix = null, threaded throughIdentifiers::sku()/ticketNumber()and a new per-generatorprefixconfig key (defaultnull; for order numbers it replacesORD). No existing caller passed a prefix, so no existing output changes.Latent, fixed:
Configs::$prefixhad no default, sogetPrefix()threw beforesetPrefix();InvalidOptionException::invalidArgument()dropped the500code its caller passed, andrender()type-hinted theRequestfacade.Behaviour changes
prefix.render(), not__toString().generatorclass throwsInvalidOptionExceptioninstead ofTypeError.InvalidOptionExceptionfromConfigs::__getcarries code 500 instead of 0.The
Strmacros still ignore their$prefixargument for SKUs and ticket numbers, as 0.1.0 did. That is left for a separate decision.Tests
66 tests, 158 assertions, green locally. The new tests failed on the old code: 3 errors and 7 failures, covering the helper prefix, the interface-only generator, the uninitialised prefix, the per-type interfaces, the config prefix and the docs fixture.