Skip to content

Fluent configuration API and object-argument cache keys - #11

Merged
trm42 merged 1 commit into
masterfrom
otwell-improvements
Jun 12, 2026
Merged

trm42 merged 1 commit into
masterfrom
otwell-improvements

Conversation

@trm42

@trm42 trm42 commented Jun 12, 2026

Copy link
Copy Markdown
Owner

What

Two Otwell-flavored improvements to the public surface of CacheDecorator:

1. Fluent, chainable configuration API

  • Removed the void setters setTtl() / setEnabled(bool).
  • Added a chainable, Laravel-style grammar — all returning static:
    • ttl($ttl) — TTL in seconds / DateInterval / DateTimeInterface / null to bypass
    • enable() / disable() — toggle caching readably
    • prefix(?string), withTags(array), tagCleaners(array), exclude(string ...)
  • Each new method is added to the initExcludes() defaults so __call() resolves it on the decorator instead of forwarding to the inner object.
$cached = (new CachedReportingService(new ReportingService))
    ->ttl(600)
    ->prefix('reports')
    ->withTags(['reports'])
    ->exclude('debugDump');

2. Object arguments in cache keys

Resolves the long-standing @todo / "coming in v1.0.0" caveat. Arguments may now be scalars, arrays, or objects, folded into the key through a new overridable normalizeArgument(mixed): string seam:

  1. scalars / null / bool — cast as-is (existing keys stay byte-identical)
  2. UrlRoutablegetRouteKey()
  3. BackedEnum->value; Stringable / __toString — string cast
  4. anything else — json_encode, falling back to md5(serialize(...))

Subclasses can override normalizeArgument() to customize identity.

Why

The previous surface read like a 2015-era package: void setters that don't chain, and unsupported object arguments. These changes make configuration expressive and self-documenting and let models/enums/value objects be passed as cached method arguments.

Notes

  • Breaking: setTtl() / setEnabled() are removed (the package isn't in production use yet). Migration is documented in the README upgrade notes.
  • README and CLAUDE.md updated in the same change, per the repo's README-sync rule.

Tests

Full suite green (40 tests / 66 assertions), pint --test passes, phpstan (larastan) clean. New tests cover the fluent setters returning the decorator, enable()/disable()/ttl() behavior, chained-call resolution (regression guard on the exclude-list), and object-argument keying by identity.

🤖 Generated with Claude Code

Replace the void setTtl()/setEnabled() setters with a chainable,
Laravel-style grammar (ttl/enable/disable/prefix/withTags/tagCleaners/
exclude, all returning static) and support objects as method arguments
via a new overridable normalizeArgument() seam.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@trm42
trm42 merged commit 3f7070b into master Jun 12, 2026
19 checks passed
@trm42
trm42 deleted the otwell-improvements branch June 12, 2026 09:26
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.

1 participant