Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/Services/Cache/SettingsCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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).');
Expand Down Expand Up @@ -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)));
}

Expand All @@ -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];
Expand Down
8 changes: 8 additions & 0 deletions tests/Arch/ArchitectureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading