alancole / atomic
A php eventing/scheduling library.
Installs: 19
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 2
Open Issues: 0
Type:composer
Requires
- g4/cron: ^0.1.2
This package is auto-updated.
Last update: 2025-02-24 05:31:56 UTC
README
Atomic
Atomic is a PHP schedule library, events can be build and scheduled. You may use Atomic simply to schedule your events, or you can attach an adapter and use our bin provided to run your jobs based on a cron schedule.
Schedule a job
# Schedule an event on the first of december each year. $yearly = new Atomic\Schedule\Yearly(new DateTime("1st December")); $event = new Atomic\Event("dec-log-clear", $yearly, function() { Logs::clearAll(); // hypothetical event code. }); # Some check at a later time if ($event->isDue()) { $event->fire(); }
Stacks
If you like you can register all your events no a stack and have the stack fire any events as they are due.
$stack = new Atomic\Event\Stack([$event, $event2, $event3]); $evnt = $stack->getNextEvent(); if ($evnt) { Log::info("Triggering event " . $evnt->getName()); $result = $evnt->fire(); Log::info("Event Return: " . $result); }
If you dont need to care about extra event data besides its return value you can just ask the stack to trigger the events for you, if it finds an event it will run it, if not it will return false.
$return = $stack->trigger(); if ($return) { Log::info("Oh, we fired an event. It returned: " . $return); } else { Log::info("No events found."); }
Mappers
We provide a simple mapper, which will take an array of events return a stack. This is handy if you store events in a database table and you want to load that into a stack.
try { $mapper = new Atomic\Event\Mapper($events, [ 'name' => 'name', 'schedule' => 'schedule', 'callback' => 'callback', 'first_run' => 'start_time' ]); $stack = $mapper->getStack(); $stack->trigger(); } catch (Exception $e) { Log::warn($e->getMessage()); }
Custom Schedules
You can add your own schedules quite simply by using the ScheduleInterface
your schedule must accept a datetime on construct and it must return a true or false on isNow
if this function returns true a stack will fire this event.
This class would trigger your function Every Wednesday.
namespace Custom\Schedule; class EveryWednesday implements \Atomic\Schedule\ScheduleInterface { public function __construct(DateTime $time = null) { return true; } public function getName() { return "Every Wednesday"; } public function getNextRun() { return new DateTime("Wednesday"); } public function getFirstRun() { return new DateTime("Wednesday"); } public function isNow(DateTime $time = null) { return date("D") == "Wed"; } }
Install
composer require waxim/atomic
Test
phpunit