How ludoguenet/laravel-zap works
Purpose and Runtime
Zap is a reusable Laravel 13+/PHP 8.5+ scheduling package, not a standalone application. It attaches recurring availability, appointments, blocks, and custom schedules to arbitrary Eloquent models, then evaluates conflicts and bookable time slots. Package wiring and defaults live in src/ZapServiceProvider.php, config/zap.php, and composer.json.
Public Entry Points
Consumers create schedules through the facade/helper, then attach them to schedulable Eloquent resources:
• src/Facades/Zap.php exposes the primary Zap::for($model) and conflict API.
• src/helpers.php provides the zap() helper.
• src/Models/Concerns/HasSchedules.php is mixed into resource models and exposes schedule queries plus availability/slot operations.
Persistence Model and Ownership Boundary
Schedules are polymorphically owned by any HasSchedules model and persisted with their time periods:
• src/Models/Schedule.php represents schedule identity, type, date limits, recurrence/frequency configuration, metadata, and rules.
• src/Models/SchedulePeriod.php holds one or more daily start/end time intervals for a schedule.
• database/migrations/2024_01_01_000001_create_schedules_table.php and database/migrations/2024_01_01_000002_create_schedule_periods_table.php establish the storage contract.
Applications may replace these models/migrations for UUID or ULID keys through config/zap.php.
Schedule Construction and Save Flow
Zap::for() hands off to the fluent creation layer:
• src/Builders/ScheduleBuilder.php accepts naming, type, date bounds, periods, metadata, rules, and recurrence calls.
• src/Services/ScheduleService.php coordinates persistence of the schedule and periods.
• src/Events/ScheduleCreated.php provides the post-creation extension point.
Before persistence, validation and conflict policy form a boundary that can reject invalid or exclusive overlaps.
Types, Rules, and Conflict Enforcement
Schedule semantics are centralized rather than inferred from callers:
• src/Enums/ScheduleTypes.php distinguishes availability, appointment, blocked, and custom schedules.
• src/Services/ValidationService.php enforces builder-configured constraints such as overlap, working-hours, duration, and weekend rules.
• src/Services/ConflictDetectionService.php finds intersections according to type/rule policy and config. Its results back facade conflict checks and schedule creation protection.
Recurrence Representation and Evaluation
Recurrence is stored as a frequency plus typed configuration, allowing query-time expansion rather than materializing occurrences:
• src/Enums/Frequency.php defines supported cadence families.
• src/Data/FrequencyConfig.php and src/Data/DailyFrequencyConfig.php represent recurrence payloads.
• src/Data/WeeklyFrequencyConfig/AbstractWeeklyFrequencyConfig.php and src/Data/MonthlyFrequencyConfig/AbstractMonthlyFrequencyConfig.php anchor weekly/monthly variants, including N-week/N-month, odd/even, ordinal weekday, and annual patterns.
• src/Casts/SafeFrequencyCast.php and src/Casts/SafeFrequencyConfigCast.php safely serialize model fields into these domain types.
Query and Availability Pipeline
Resource-facing queries select schedules applicable to a date/range, evaluate recurrence and periods, then derive booking outcomes:
• src/Models/Builders/ScheduleBuilder.php is the Eloquent query builder for recurrence-aware schedule filtering.
• src/Models/Builders/SchedulePeriodBuilder.php supports period-level filtering.
• src/Models/Concerns/HasSchedules.php combines availability windows with blocking/appointment exclusions to answer bookability, enumerate slots, and locate the next slot; buffer behavior comes from config/zap.php.
Infrastructure and External Integration
The package depends on Laravel’s service container, Eloquent ORM, migrations, events, facades, configuration publishing, and Artisan migration workflow. It has no required network service or frontend. Its external integration surface is the host Laravel application’s database and arbitrary polymorphic Eloquent resource models.
Main entry points: Schedule.php Schedule · ScheduleService.php Schedule service · ScheduleBuilder.php Schedule query builder · HasSchedules.php Availability and slots
Components 19
Laravel Integration
- Zap service provider · Registers package services, publishing, and defaults.
src/ZapServiceProvider.php - Zap configuration · Controls model replacements, conflict policy, and buffers.
config/zap.php - Host Eloquent resources · Arbitrary polymorphic models own schedules.
- Schedule migrations · Creates schedule and period persistence tables.
database/migrations/2024_01_01_000001_create_schedules_table.php
Public API
- Zap facade · Starts schedule creation and exposes conflict checks.
src/Facades/Zap.php - zap helper
src/helpers.php - HasSchedules · Attaches schedule queries and availability operations to resources.
src/Models/Concerns/HasSchedules.php
Scheduling Domain
- Schedule builder · Collects schedule fields, periods, rules, and recurrence.
src/Builders/ScheduleBuilder.php - Schedule service · Saves schedules and their periods after policy checks.
src/Services/ScheduleService.php - Validation service · Validates overlap, hours, duration, and weekend rules.
src/Services/ValidationService.php - Conflict detection · Finds type- and rule-aware schedule intersections.
src/Services/ConflictDetectionService.php - Schedule · Polymorphic schedule identity, rules, bounds, and recurrence.
src/Models/Schedule.php - Schedule period · Daily start/end intervals belonging to a schedule.
src/Models/SchedulePeriod.php - Schedule created
src/Events/ScheduleCreated.php - Recurrence domain types · Frequency and typed daily, weekly, and monthly recurrence payloads.
src/Data/FrequencyConfig.php - Safe recurrence casts · Serializes frequency and configuration fields safely.
src/Casts/SafeFrequencyConfigCast.php
Evaluation Pipeline
- Schedule query builder · Filters schedules using recurrence-aware date logic.
src/Models/Builders/ScheduleBuilder.php - Period query builder · Filters schedule time periods.
src/Models/Builders/SchedulePeriodBuilder.php - Availability and slots · Combines availability with exclusions to find bookable and next slots.
src/Models/Concerns/HasSchedules.php
Connections 29
- Zap service provider → Zap configuration: loads defaults
- Zap service provider → Zap facade: registers
- Host Eloquent resources → HasSchedules: uses trait
- Zap facade → Schedule builder: for(model)
- zap helper → Zap facade: accesses
- Schedule builder → Validation service: validates before save
- Schedule builder → Schedule service: submits definition
- Schedule service → Conflict detection: checks exclusivity
- Validation service → Zap configuration: reads policy
- Conflict detection → Zap configuration: reads policy
- Schedule service → Schedule: persists
- Schedule service → Schedule period: persists intervals
- Schedule service → Schedule created: dispatches
- Schedule → Host Eloquent resources: polymorphic owner
- Schedule → Schedule period: has periods
- Schedule → Recurrence domain types: stores typed recurrence
- Safe recurrence casts → Recurrence domain types: serializes
- Schedule migrations → Schedule: creates storage
- Schedule migrations → Schedule period: creates storage
- HasSchedules → Schedule query builder: queries applicable schedules
- Schedule query builder → Schedule: filters
- Schedule query builder → Recurrence domain types: evaluates dates
- Schedule query builder → Period query builder: loads intervals
- Period query builder → Schedule period: filters
- HasSchedules → Availability and slots: derives slots
- Availability and slots → Schedule query builder: uses applicable windows
- Availability and slots → Conflict detection: applies exclusions
- Availability and slots → Zap configuration: reads buffers
- Zap facade → Conflict detection: conflict API