schubu/cronify

为您的模型添加cron功能。

1.1.1 2021-03-25 17:46 UTC

This package is auto-updated.

Last update: 2024-08-29 05:31:26 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

将cron模式添加到您的模型中以检查其有效性。例如:您可以检查一条给定的帖子是否应该被查看。

安装

您可以通过composer安装此包

composer require schubu/cronify

安装包后,请运行安装程序。它发布了迁移文件。

php artisan cronify:install

之后,您需要运行迁移

php artisan migrate

数据库中添加了一个新的cron表。它存储了您的模型cron条目。

将cron模式应用于您的模型

下一步是向您的模型添加Cronable特质。

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Schubu\Cronify\Traits\Cronable;

class Post extends Model
{
    use Cronable;

    // ...
}

用法

假设您有一个包含标题和正文字段的帖子模型,以下是如何添加具有cron模式的帖子

$post = Post::create([
    "title"=>"Your title", 
    "body" => "Lorem ipsum"
]);

$post->crons()->create([
    'pattern' => '* * 1 1 *'
]);

在这种情况下,您可以询问模式是否到期

App\Models\Post::latest()->first()->isDue()

在我们的示例中,它仅在1月1日返回true。

提示

您可以添加多个cron模式。如果至少有一个规则匹配,则返回true。

您可以为方法->isDue($timestamp)提供日期,以便您可以在给定日期检查模型的有效期。

验证

您可以使用CronPatternRule验证提交的cron模式。

['cron' => ['array', new CronPatternRule]]

将cron模式作为包含键minute, hour, day, month, weekday的关联数组提交。如果您将模式作为字符串提交,请将其拆分为数组。

路线图

  • 添加对提供的字符串和提供的数组进行验证的不同规则array: new CronPatternArrayRulestring: new CronPatternStringRule
  • 集成表单组件
  • 集成测试

安全

如果您发现任何安全相关的问题,请通过电子邮件peter@schu-bu.de而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件