承接 thesis/cron-parser 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thesis/cron-parser

最新稳定版本:0.2.0

Composer 安装命令:

composer require thesis/cron-parser

包简介

Easily extensible modern cron parser.

README 文档

README

Installation

composer require thesis/cron-parser

Basic usage

Matching date

<?php

declare(strict_types=1);

use Thesis\Cron;

$parser = Cron\Parser::standard();
$time = $parser->parse('* * * * *'); 
echo $time->match(new \DateTimeImmutable('2025-01-29 00:00:00')); // true

Getting next date

<?php

declare(strict_types=1);

use Thesis\Cron;

$parser = Cron\Parser::standard();
$time = $parser->parse('* * * * *');
echo $time->tick(new \DateTimeImmutable('2025-01-29 00:00:00')); // 2025-01-29 00:01:00 

Iterating on Thesis\Cron\Time

<?php

declare(strict_types=1);

use Thesis\Cron;

$parser = Cron\Parser::standard();
$time = $parser->parse('* * * * *');

foreach ($time->iterator(new \DateTimeImmutable('2025-01-29 00:00:00'), iterations: 10) as $it) {
    echo $it->format('Y-m-d H:i:s');
}

Configure parser

<?php

declare(strict_types=1);

use Thesis\Cron;

$parser = new Cron\Parser(
    extension: new Cron\StandardParserExtension(),
    replacers: [new Cron\AliasExpressionReplacer(['@secondly' => '* * * * * *'])],
    normalizers: [new Cron\SundayExpressionNormalizer(), new Cron\WeekdayExpressionNormalizer()],
);

$time = $parser->parse('@secondly');

echo $time->match(new \DateTimeImmutable('2025-01-29 00:00:00')); // true
echo $time->match(new \DateTimeImmutable('2025-01-29 00:00:01')); // true
echo $time->match(new \DateTimeImmutable('2025-01-29 00:00:02')); // true

Parser architecture

ExpressionReplacer works with a cron string and is needed to replace the entire string. For example, AliasExpressionReplacer implements this interface and replaces popular expressions like @hourly, @monthly with the corresponding cron expressions. This is the first step in parsing the cron expression.

The next step is normalization, and it is performed using ExpressionNormalizer, which works with an Expression object, which is some intermediate representation of the cron string. At this point, you can replace expressions atomically. For example, WeekdayExpressionNormalizer replaces only the weekdays from expressions like MON, TUE, FRI with numeric ones.

In the last step, called the parsing step, the expression is finally processed and transformed from an Expression object to a Time object. At this stage you can use StandardParserExtension or add your own.

统计信息

  • 总下载量: 328
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-30