Download the PHP package caseyamcl/sortable-tasks without Composer
On this page you can find all versions of the php package caseyamcl/sortable-tasks. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download caseyamcl/sortable-tasks
More information about caseyamcl/sortable-tasks
Files in caseyamcl/sortable-tasks
Package sortable-tasks
Short Description Abstraction to compile and sort tasks on the fly
License MIT
Homepage https://github.com/caseyamcl/sortable-tasks
Informations about the package sortable-tasks
Sortable Tasks
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/topsort
library 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
Usage
Refer to the
tests/Fixture
directory 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:
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:
Let's create some setup steps now:
Now that we have some concrete classes, let's add them to a SortableTasksIterator:
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\CircularDependecyException
is thrown. - Non-existent dependencies; e.g., Task "A" depends on Task "B", but task "B" is not defined. In this situation, a
MJS\TopSort\ElementNotFoundException
is thrown.
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Casey McLaughlin
- All Contributors
License
The MIT License (MIT). Please see License File for more information.