承接 alancole/atomic 相关项目开发

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

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

alancole/atomic

Composer 安装命令:

composer require alancole/atomic

包简介

A php eventing/scheduling library.

README 文档

README

StyleCI Build Status

Atomic

Atomic is a PHP schedule library, events can be build and scheduled. You may use Atomic simply to schedule your events, or you can attach an adapter and use our bin provided to run your jobs based on a cron schedule.

Schedule a job

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

Stacks

If you like you can register all your events no a stack and have the stack fire any events as they are due.

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

If you dont need to care about extra event data besides its return value you can just ask the stack to trigger the events for you, if it finds an event it will run it, if not it will return false.

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

Mappers

We provide a simple mapper, which will take an array of events return a stack. This is handy if you store events in a database table and you want to load that into a stack.

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

Custom Schedules

You can add your own schedules quite simply by using the ScheduleInterface your schedule must accept a datetime on construct and it must return a true or false on isNow if this function returns true a stack will fire this event.

This class would trigger your function Every Wednesday.

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

Install

composer require waxim/atomic

Test

phpunit

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-03-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固