eventsauce/clock
Simple clock abstraction for PHP. Use SystemClock in production and TestClock in tests to control time deterministically. Get now() as DateTimeImmutable, access timeZone(), move time forward, tick to system time, or fixate to a specific moment.
eventsauce/clock package is designed to decouple time consumption from business logic, making it a strong fit for event-sourced or CQRS architectures where deterministic replay of events is critical. It abstracts time handling, allowing for:
Illuminate\Events\Dispatcher) and queues (e.g., Illuminate\Queue). Can replace now() calls with a Clock instance for consistency.spatie/laravel-event-sourcing or eventSaucePHP/event-store.ClockInterface to a concrete implementation).created_at in Laravel models).eventsauce/event-store for snapshot testing).Carbon for dates. The package may require adapters to ensure compatibility (e.g., converting Clock\DateTime to Carbon instances).now() globally, or only in specific contexts (e.g., event publishing)?Carbon instances interact with Clock\DateTime? (e.g., via a facade or adapter).spatie/laravel-event-sourcing).laravel-cqrs packages).laravel-domain-events).now() with Clock in unit tests (e.g., mocking time for event publishing).$clock = new FixedClock(new \DateTime('2023-01-01'));
app()->bind(ClockInterface::class, fn() => $clock);
ClockInterface into event listeners/publishers.public function handle(ExampleEvent $event, ClockInterface $clock) {
$now = $clock->now(); // Use clock instead of now()
}
EventSaucePHP/event-store).$eventStore = new EventStore(
new Clock(), // Use the package's clock
new PdoStreamRepository($pdo)
);
now() (e.g., some audit log packages).created_at in Laravel models).eventsauce/event-store snapshots).FixedClock) scale well, but shared clocks (e.g., SystemClock) must be managed carefully.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Clock desynchronization | Events processed out of order | Use a central clock service or NTP. |
| Clock frozen in production | Time-based logic fails (e.g., retries) | Implement circuit breakers for clock adjustments. |
| Event replay with incorrect clock | Inconsistent state | Use snapshot testing for critical replays. |
| Package version incompatibility | Breaking changes in API | Pin versions in composer.json. |
| Carbon/Clock integration bugs | DateTime conversion errors | Write adapters for Carbon ↔ Clock\DateTime. |
How can I help you explore Laravel packages today?