schubu / cronify
Adds cron functionality to your models.
Requires
- php: ^7.1
- dragonmantank/cron-expression: ^v3.1
Requires (Dev)
- orchestra/testbench: ^v5.16|^v6.12
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-10-29 06:00:17 UTC
README
Add cron-patterns to your models to check for validity. For example: you can check if a given post should be viewed.
Installation
You can install the package via composer:
composer require schubu/cronify
After you've installed the package please run the installer. It published the migration file.
php artisan cronify:install
After that you've got to run your migration
php artisan migrate
A new crons table is added to your database. It stores your cron entries for your models.
Cronify your models
Next step is to add the Cronable trait to your models.
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Schubu\Cronify\Traits\Cronable; class Post extends Model { use Cronable; // ... }
Usage
Given you've a post model with a title and a body field here is how to add a post with a cron pattern:
$post = Post::create([ "title"=>"Your title", "body" => "Lorem ipsum" ]); $post->crons()->create([ 'pattern' => '* * 1 1 *' ]);
In this case you can ask for your pattern if it is due:
App\Models\Post::latest()->first()->isDue()
In our example it returns true only for the first of january.
Hints
You can add multiple cron patterns. If at least one rule matches you get true.
You can provide a date for the method ->isDue($timestamp)
so you can check the due of your model at a given date.
Validation
You can validate a submitted cron pattern by using CronPatternRule
['cron' => ['array', new CronPatternRule]]
Submit your cron pattern as an associative array with the keys minute, hour, day, month, weekday
. If you submit the pattern as a string please split it into an array.
Roadmap
- add different rules for validation of a provided string and a provided array
array: new CronPatternArrayRule
andstring: new CronPatternStringRule
- integrate a form component
- integrate testing
Security
If you discover any security related issues, please email peter@schu-bu.de instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.