dev-master
9999999-dev https://github.com/adlogix/php-event-schedulerSolution in php to schedule recurring events based on Martin Fowlers paper
MIT
The Requires
- php ^7.1
The Development Requires
Solution in php to schedule recurring events based on Martin Fowlers paper
The Adlogix Event Scheduler library provides a way to manage recurring events using Martin Fowler's Recurring Event pattern. The base of the code and documentation was initially forked from RiskioFr's solution and completely refactored to be more lightweight and with less dependencies!, (*1)
The documentation will help you to understand how to use and extend Schedule., (*3)
The schedule represented by Adlogix\EventScheduler\Scheduler
class allows you to know if any event occurs at a given date., (*4)
To start, you must instantiate Adlogix\EventScheduler\Scheduler
and schedule some events
using Adlogix\EventScheduler\Scheduler::schedule
method., (*5)
This example schedule an event with DayInMonth
temporal expression. So, the event will occur at 15th day of each coming months., (*6)
use Adlogix\EventScheduler\Scheduler; use Adlogix\EventScheduler\TemporalExpression; $scheduler = new Scheduler(); $scheduledEvent = $scheduler->schedule(new BasicEvent('event_name'), new TemporalExpression\DayInMonth(15));
If you want to cancel this event, you can provide the returned instance of Adlogix\EventScheduler\SchedulableEvent
to the Adlogix\EventScheduler\Scheduler::cancel
method., (*7)
$scheduler->cancel($scheduledEvent);
A temporal expression implements Adlogix\EventScheduler\TemporalExpression\TemporalExpressionInterface
that provides useful isOccurring
method to
check if an event occur or not at a given date represented by an instance of DateTimeInterface
., (*8)
$temporalExpression = new TemporalExpression(); $isOccuring = $temporalExpression->isOccuring(new BasicEvent('event_name'), $date);
By default, there are some temporal expressions that you can use to define event recurrence., (*9)
use Adlogix\EventScheduler\TemporalExpression\EachDay; $expression = new EachDay();
use Adlogix\EventScheduler\TemporalExpression\DayInWeek; use Adlogix\EventScheduler\ValueObject\WeekDay; $expression = new DayInWeek(WeekDay::MONDAY); $expression = DayInWeek::monday();
use Adlogix\EventScheduler\TemporalExpression\DayInMonth; $expression = new DayInMonth(15);
use Adlogix\EventScheduler\TemporalExpression\WeekInYear; use Adlogix\EventScheduler\ValueObject\Month; $expression = new WeekInYear(15);
use Adlogix\EventScheduler\TemporalExpression\MonthInYear; use Adlogix\EventScheduler\ValueObject\Month; $expression = new MonthInYear(Month::JANUARY); $expression = MonthInYear::january();
use Adlogix\EventScheduler\TemporalExpression\Semester; $expression = new Semester(1);
use Adlogix\EventScheduler\TemporalExpression\Trimester; $expression = new Trimester(1);
use Adlogix\EventScheduler\TemporalExpression\Year; $expression = new Year(2015);
use Adlogix\EventScheduler\TemporalExpression\LeapYear; $expression = new LeapYear();
DateTimeInterface
instanceuse DateTime; use Adlogix\EventScheduler\TemporalExpression\From; $date = new DateTime(); $expression = new From($date);
DateTimeInterface
instanceuse DateTime; use Adlogix\EventScheduler\TemporalExpression\Until; $date = new DateTime(); $expression = new Until($date);
use Adlogix\EventScheduler\TemporalExpression\RangeEachYear; // From January to March inclusive $expression = new RangeEachYear(1, 3); // From January 10 to March 20 $expression = new RangeEachYear(1, 3, 10, 20);
In order to create complex temporal expressions, you can use composite temporal expressions that allow to build combinaisons of previous ones., (*10)
An event is occuring at a given date if it lies within all temporal expressions., (*11)
use DateTime; use Adlogix\EventScheduler\TemporalExpression\Collection\Intersection; use Adlogix\EventScheduler\TemporalExpression\DayInMonth; use Adlogix\EventScheduler\TemporalExpression\MonthInYear; $intersection = new Intersection(); $intersection->addElement(new DayInMonth(15)); $intersection->addElement(MonthInYear::january()); $intersection->isOccuring('event', new DateTime('2015-01-15')); // returns true $intersection->isOccuring('event', new DateTime('2015-01-16')); // returns false $intersection->isOccuring('event', new DateTime('2015-02-15')); // returns false
An event is occuring at a given date if it lies within at least one temporal expression., (*12)
use DateTime; use Adlogix\EventScheduler\TemporalExpression\Collection\Union; use Adlogix\EventScheduler\TemporalExpression\DayInMonth; use Adlogix\EventScheduler\TemporalExpression\MonthInYear; $union = new Union(); $intersection->addElement(new DayInMonth(15)); $intersection->addElement(MonthInYear::january()); $intersection->isOccuring('event', new DateTime('2015-01-15')); // returns true $intersection->isOccuring('event', new DateTime('2015-01-16')); // returns false $intersection->isOccuring('event', new DateTime('2015-02-15')); // returns true
An event is occuring at a given date if it lies within first temporal expression and not within the second one., (*13)
use DateTime; use Adlogix\EventScheduler\TemporalExpression\DayInMonth; use Adlogix\EventScheduler\TemporalExpression\Difference; use Adlogix\EventScheduler\TemporalExpression\MonthInYear; $difference = new Difference(MonthInYear::january(), new DayInMonth(15)); $intersection->isOccuring('event', new DateTime('2015-01-15')); // returns false $intersection->isOccuring('event', new DateTime('2015-01-16')); // returns true $intersection->isOccuring('event', new DateTime('2015-02-15')); // returns false
You can create temporal expressions that match your special needs by implementing Adlogix\EventScheduler\TemporalExpression\TemporalExpressionInterface
., (*14)
After detailing the different temporal expressions available, consider a concrete case with complex temporal expression that could be used in real life., (*15)
In the example below, we include every Saturday and Sunday except on July and August., (*16)
use Adlogix\EventScheduler\TemporalExpression\Collection\Union; use Adlogix\EventScheduler\TemporalExpression\DayInWeek; use Adlogix\EventScheduler\TemporalExpression\Difference; use Adlogix\EventScheduler\TemporalExpression\MonthInYear; $includedWeekDays = new Union(); $union->addElement(DayInWeek::saturday()); $union->addElement(DayInWeek::sunday()); $excludedMonths = new Union(); $union->addElement(MonthInYear::july()); $union->addElement(MonthInYear::august()); $expression = new Difference($includedWeekDays, $excludedMonths);
Solution in php to schedule recurring events based on Martin Fowlers paper
MIT