andrewdyer/scheduler

dev-master 2018-11-23 16:09 UTC

README

License

Licensed under MIT. Totally free for private or commercial projects.

Installation

composer require andrewdyer/scheduler

Usage

use Anddye\Scheduler\Kernel;
use Anddye\Events\ExampleEvent;
    
require_once 'vendor/autoload.php';
    
$kernel = new Kernel;
    
$kernel->add(new ExampleEvent())->everyMinute();
    
$kernel->run();

Creating Events

namespace App\Events;

use Anddye\Events\AbstractEvent;

class ExampleEvent extends AbstractEvent
{
    public function handle()
    {
        var_dump($this->getExpression());
    }
}

Combining Frequencies

You can combine frequencies together in order to create more specific cron expressions;

// At every 10th minute on Friday.
$kernel->add(new ExampleEvent())->everyTenMinutes()->fridays();
// At minute 45 on Saturday and Sunday.
$kernel->add(new ExampleEvent())->hourlyAt(45)->weekends();
// At minute 0 past hour 3 and 7 on Monday.
$kernel->add(new ExampleEvent())->dailyTwice(3, 7)->mondays();
// At every minute on Tuesday, Wednesday, and Thursday.
$kernel->add(new ExampleEvent())->everyMinute()->days(2,3,4);

Expression Builder

use Anddye\Scheduler\ExpressionBuilder;

$builder = new ExpressionBuilder();
$builder->setDay($_POST['day']);
$builder->setDate($_POST['date']);
$builder->setTime($_POST['hour'], $_POST['minute']);

$expression = $builder->getExpression();

Event Methods

Method Description
at(int $hour = 0, int $minute = 0)
daily()
dailyAt(int $hour = 0, int $minute = 0)
dailyTwice(int $firstHour = 1, int $lastHour = 12)
days()
mondays()
tuesdays()
wednesdays()
thursdays()
fridays()
saturdays()
sundays()
weekdays()
weekends()
everyMinute()
everyTenMinutes()
everyThirtyMinutes()
hourly()
hourlyAt(int $minute = 1)
monthly()
monthlyOn(int $day = 1)
on(int $day = 1)
getExpression()
setExpression(string $expression)
replaceIntoExpression($position, $value)

Useful Links