caseyamcl/sortable-tasks
Composer 安装命令:
composer require caseyamcl/sortable-tasks
包简介
Abstraction to compile and sort tasks on the fly
README 文档
README
A simple, un-opinionated PHP 7.4+ abstraction library to allow for the ordering of tasks. Features:
- Tasks are service class instances that can define other tasks as dependencies
- Useful for libraries like setup routines, ensuring that HTTP middleware runs in-order, and other libraries where the ordering of tasks is determined in an arbitrary order, but need to be run deterministically
- Builds on the
marcj/topsortlibrary to enable sorting of tasks, each defined in their own class - Un-opinionated about how tasks actually run; just concerned with sorting them based on their dependency and running them in-order
- PSR-4 and PSR-12 compliant
Structure
This library only contains two files:
| SortableTask.php | Interface for a task; contains two methods to define dependencies, depeondsOn and mustRunBefore |
| SortableTasksIterator.php | Iterator class that does the sorting and other work; implements Iterable and Countable |
Install
Via Composer
$ composer require caseyamcl/sortable-tasks
Usage
Refer to the
tests/Fixturedirectory for a fully functional example.
The best way to demonstrate this library is with an example, so we'll use a setup application. In our hypothetical application, setup tasks can be registered in any order, but they will run in a particular order based on a set of explicitly defined dependencies.
First, we must define a class that implements the SortableTask interface:
use SortableTasks\SortableTask; abstract class SetupStep implements SortableTask { abstract public function __invoke(): bool; }
Notice that we do not implement the dependsOn() and mustRunBefore() methods in the abstract SetupStep class. This
means that each concrete implementation step must define its dependencies. This is optional, and is up to the library
that implements SortableTasks.
If most steps do not require ordering, then we could do the following:
use SortableTasks\SortableTask; abstract class SetupStep implements SortableTask { abstract public function __invoke(): bool; // Provide default implementations of `dependsOn` and `mustRunBefore` that return empty arrays public static function dependsOn() : iterable { return []; } public static function mustRunBefore() : iterable { return []; } }
Let's create some setup steps now:
class CheckConfigStep extends SetupStep { public static function dependsOn(): iterable { return []; // depends on nothing; can run anytime in the order of operations } public function __invoke(): bool { // do stuff, then.. return true; } } class CheckDbConnectionStep extends SetupStep { private DbConnector $dbConnector; public function __construct(DbConnector $dbConnector) { $this->dbConnector = $dbConnector; } public static function dependsOn(): iterable { return [CheckConfigStep::class]; } public static function mustRunBefore(): iterable { return [BuildContainerStep::class]; } public function __invoke(): bool { // do stuff, then.. return $this->dbConnector->checkConnection(); } } class BuildContainerStep extends SetupStep { private ContainerBuilder $containerBuilder; public function __construct(ContainerBuilder $containerBuilder) { $this->containerBuilder = $containerBuilder; } public static function dependsOn(): iterable { return [CheckConfigStep::class, CheckDbConnection::class]; } public function __invoke(): bool { // do stuff, then.. return $this->containerBuilder->buildContainer(); } }
Now that we have some concrete classes, let's add them to a SortableTasksIterator:
use SortableTasks\SortableTasksIterator; $iterator = new SortableTasksIterator(); // Notice that it doesn't matter in what order we add the steps; they will get sorted at runtime $iterator->add(new BuildContainerStep()); $iterator->add(new CheckDbConnectionStep()); $iterator->add(new CheckConfigStep()); // Tasks are sorted upon calling the iterator // Class names are the keys foreach ($iterator as $setupStepClassName => $setupStep) { if (! $setupStep()->__invoke()) { throw new SetupFailedException('Setup failed on step: ' . $setupStepClassName); } }
Handling errors
There are two issues that are likely to occur during task execution, both of them throw excpetions:
- Circular dependencies; e.g., Task "A" depends on Task "B", which depends on Task "A". In this situation, a
MJS\TopSort\CircularDependecyExceptionis thrown. - Non-existent dependencies; e.g., Task "A" depends on Task "B", but task "B" is not defined. In this situation, a
MJS\TopSort\ElementNotFoundExceptionis thrown.
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email me@caseymclaughlin.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
caseyamcl/sortable-tasks 适用场景与选型建议
caseyamcl/sortable-tasks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「caseyamcl」 「sortable-tasks」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 caseyamcl/sortable-tasks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 caseyamcl/sortable-tasks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 caseyamcl/sortable-tasks 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Guzzle v6+ retry middleware that handles 429/503 status codes and connection timeouts
A list of dangerous usernames
Settings Manager
A portable PHP client library to access data in a DDN (WOS) Web Object Scalar API
Guzzle v6+ Request Logging Middleware
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-31