PHP code example of alancole / atomic

1. Go to this page and download the library: Download alancole/atomic library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alancole / atomic example snippets


# 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();
}

$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);
}

$return = $stack->trigger();
if ($return) {
    Log::info("Oh, we fired an event. It returned: " . $return);
} else {
    Log::info("No events found.");
}

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());
}

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";
    }
}