A package to setup a nexus of stops which are processed based on the result of the previous stop.
Note: Passing an object that is already initialized is not supported. That type of functionality should be handled by the container.
A container to resolve class instance such as: https://github.com/illuminate/container
Send a traveler (payload) through an array of jobs.
public function send(TravelerContract $traveler);
Set the destinations the traveler (payload) will travel to. Not all destinations will be executed. See "destinations" below.
public function to(array $destinations);
Set the method the destinations should execute.
public function via(string $method);
Set the final destination of the traveler (payload).
public function arrive(Closure $destination);
Handle the traveler at the destination.
public function handle(TravelerContract $traveler, Closure $next);
Each destination should return either true or false. This return value will determine which destination is executed next.
$destinations = [
Addition::class => [Nexus::UNINHABITED, Multiplication::class],
Multiplication::class => [Subtraction::class, Nexus::UNINHABITED],
Subtraction::class => [Nexus::UNINHABITED, Division::class],
Division::class => [Nexus::STOP, Nexus::STOP]
];
Use the constant Nexus::STOP
in order for the execution to jump to the final destination.
Use the constant Nexus::UNINHABITED
to fill the job array in unreachable locations, but the array must be filled.
$rules = new DestinationRules(false);
$container = new Container();
$nexus = new Nexus($rules, $container);
$traveler = new Math(5); // starts with initial value of 5
/**
* Destination path is as follows:
* - start with 5
* - add 3, then take truthy path
* - multiply by 6, then take falsy path
* - subtract 3, then take truthy path
* - divide 4, then stop
*/
$destinations = [
// Class to process // False // True
// Container returns new Addition(3) - adds 3
Addition::class => [Nexus::UNINHABITED, Multiplication::class],
// Container returns new Multiplication(6, false) - multiplies by 6
Multiplication::class => [Subtraction::class, Nexus::UNINHABITED],
// Container returns new Subtraction(2) - subtracts 2
Subtraction::class => [Nexus::UNINHABITED, Division::class],
// Container returns new Division(4) - divides by 4
Division::class => [Nexus::STOP, Nexus::STOP]
];
$nexus->send($traveler)
->to($destinations)
->arrive(function(Math $traveler) {
Assert::assertEquals(11.5, $traveler->getValue());
});
Laravel's pipeline contract for a common API interface.