diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ec7432..d3ec03f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- The settings cache reads the time through `Support\Clock`, like the rest of the package, instead of + the global `now()` helper. Behaviour is unchanged: both read Laravel's date seam, so `travelTo()` + still applies. An architecture test now refuses `now()` and `today()` outside the providers and + facades. + ## [0.1.0] - 2026-11-14 The first public release. `Added` describes the package; `Changed`, `Removed`, `Fixed` and diff --git a/src/Services/Cache/SettingsCache.php b/src/Services/Cache/SettingsCache.php index 32894f9..ddb521f 100644 --- a/src/Services/Cache/SettingsCache.php +++ b/src/Services/Cache/SettingsCache.php @@ -10,6 +10,7 @@ use Illuminate\Cache\Repository; use Simtabi\Laranail\Settings\Data\Scope; use Simtabi\Laranail\Settings\Data\Record; +use Simtabi\Laranail\Settings\Support\Clock; use Simtabi\Laranail\Settings\Data\RecordSet; use Simtabi\Laranail\Settings\Data\CacheTarget; use Simtabi\Laranail\Settings\Support\BagCodec; @@ -44,6 +45,7 @@ public function __construct( private readonly ?TenantDiscriminator $tenant = null, private readonly ?OperationalLogger $log = null, private readonly BagCodec $codec = new BagCodec, + private readonly Clock $clock = new Clock, ) { if ($prefix === '' || $ttl < 1 || $stale < 0) { throw new InvalidConfiguration('laranail.settings.cache: `prefix` must be a non-empty string, `ttl` at least 1 and `stale` at least 0 (seconds).'); @@ -158,7 +160,7 @@ private function lookup(CacheTarget $target, array $scopes, Closure $load): arra } // Stale: flexible() answers it and refreshes under this key's tokens, never over a newer write. - if ($created + $this->ttl <= now()->getTimestamp()) { + if ($created + $this->ttl <= $this->clock->now()->getTimestamp()) { $entry = $this->cache->flexible($key, [$this->ttl, $this->lifetime()], fn (): array => $this->codec->encode($this->only($load([$scope]), $scope))); } @@ -181,7 +183,7 @@ private function lookup(CacheTarget $target, array $scopes, Closure $load): arra private function put(array $keys, array $bags): void { $entries = []; - $now = now()->getTimestamp(); + $now = $this->clock->now()->getTimestamp(); foreach ($keys as $index => $key) { $this->bags[$key] = $bags[$index]; diff --git a/tests/Arch/ArchitectureTest.php b/tests/Arch/ArchitectureTest.php index c2dda8d..1450d4e 100644 --- a/tests/Arch/ArchitectureTest.php +++ b/tests/Arch/ArchitectureTest.php @@ -44,6 +44,14 @@ 'Simtabi\Laranail\Settings\Facades', ]); +arch('the time is read through Support\Clock, never the global helpers') + ->expect('Simtabi\Laranail\Settings') + ->not->toUse(['now', 'today']) + ->ignoring([ + 'Simtabi\Laranail\Settings\Providers', + 'Simtabi\Laranail\Settings\Facades', + ]); + arch('no debugging calls ship') ->expect(['dd', 'dump', 'ray', 'var_dump', 'print_r']) ->not->toBeUsed();