kennyyuen/shark-sequential-command

There is no license information available for the latest version (0.1.0) of this package.

0.1.0 2025-07-24 17:17 UTC

This package is auto-updated.

Last update: 2025-07-24 17:29:24 UTC


README

Sequential Command is a lightweight PHP library for building and managing chains of commands that execute sequentially. Ideal for workflow orchestration, task pipelines, and scenarios where ordered execution is required.

Installation

You can install the library via Composer:

composer require kennyyuen/shark-sequential-command

Features

  • Chain commands together for sequential execution
  • Waterfall and invoke execution modes
  • Easy to extend and integrate
  • Exception handling for completed commands

Usage

Basic Example

use Shark\Extensions\SequentialCommand\Command;
use function Shark\Extensions\SequentialCommand\chain;

$cmd1 = new Command(function () {
    echo "First command\n";
    return 'first';
});
$cmd2 = new Command(function () {
    echo "Second command\n";
    return 'second';
});
$cmd3 = new Command(function () {
    echo "Third command\n";
    return 'third';
});

chain($cmd1, $cmd2);
chain($cmd2, $cmd3);

$cmd3->invoke();
$cmd2->invoke();
$cmd1->invoke();

// Commands are executed after all in the chain has been invoked.
// Execution order: cmd1 -> cmd2 -> cmd3

Waterfall Execution

$cmd1->waterfall(); // Only runs if all downstream commands are invoked

Exception Handling

try {
    $cmd1->invoke();
    $cmd1->invoke(); // Throws RuntimeException: already completed
} catch (RuntimeException $e) {
    echo $e->getMessage();
}

Methods

  • Command::__construct(callable $callback) - Create a command with a callback
  • Command::invoke() - Run the command chain from the head
  • Command::waterfall() - Run only if all downstream commands are invoked
  • chain(Command $first, Command $second) - Chain two commands together
  • Command::isCompleted() - Check if command is completed
  • Command::getCallbackResult() - Get the result of the callback

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.

License

This project is licensed under the MIT License.