DI and actions
The facade is the recommended default, not the only way in. Three equivalent entry points run the same code:
- The OpeningHours facade — the shortest form, used throughout these docs.
- The manager, RoundlyConsulting\OpeningHours\OpeningHoursManager — the facade root, injected through the constructor. Same API, an explicit dependency and no static calls. It is bound scoped, so one instance (and one memo) serves a request or job.
- Actions — single-purpose classes with execute(), for composing into your own actions, jobs and commands.
Inject the manager, or call an action
use RoundlyConsulting\OpeningHours\Actions\AddExceptionAction;
use RoundlyConsulting\OpeningHours\DataTransferObjects\ExceptionData;
use RoundlyConsulting\OpeningHours\OpeningHoursManager;
final class CloseForHoliday
{
public function __construct(private OpeningHoursManager $hours) {}
public function __invoke(Clinic $clinic, string $date): void
{
$this->hours->exceptions($clinic)->closed($date, label: 'Holiday');
}
}
// The raw action — no relation reset, no fake:
app(AddExceptionAction::class)->execute($clinic->openingHoursCalendar(), ExceptionData::make('2026-12-24'));OpeningHours::fake() swaps the container binding as well as the facade, so a constructor-injected manager and the owner trait are recorded too. An action called directly writes for real — no relation reset, and no fake.
OpeningHoursManager isn’t final: subclass it and rebind the class in the container to change its behaviour. The manager resolves actions per call, so a host rebind of an action is honoured as well.
Facade method → action
| Facade method | Action |
|---|---|
sync($owner, $data) · edit()->save() · setOpeningHours() | SyncCalendarAction::execute($owner, CalendarData, $key, ?$expectedRevision) |
exceptions($owner)->add() · closed() · open() | AddExceptionAction::execute(Calendar, ExceptionData) |
exceptions($owner)->remove($ruleId) | RemoveExceptionAction::execute(Calendar, int $ruleId) |
delete($owner, force:) | DeleteCalendarAction::execute(Calendar, bool $force) |
refresh($owner) | BumpRevisionAction::execute(int $calendarId) |
Reads — for(), calendar(), has(), exceptions()->all(), validate(), definitionData() — have no action. PruneAction and MaterializeIntervalsAction are @internal: drive them through opening-hours:prune and opening-hours:materialize (or the MaterializeIntervalsJob), not from your code. AddExceptionAction, RemoveExceptionAction and DeleteCalendarAction take the Calendar header — OpeningHours::calendar($owner) returns it.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.