Giter Site home page Giter Site logo

sequence's Introduction

Continuous Integration

Sequence

Sequence your tasks and make complex workflows more readable

[TOC]

Summary

This repository contains a Chain of Responsability design pattern implementation built with PHP.

Requirements

This library requires PHP^8.3

Installation

Install Sequence using Composer:

composer require fonil/sequence

Usage

Create a Sequence instance and attach any type of payload through a simple interface:

$result = Sequence::run(FirstTask::class)
	->then(SecondTask::class)
	...
	->then(LastTask::class)
	->startWith('payload');

Tasks

Sequence requires at least a task to be run. You can attach any of the following entities as a task:

Invokable Class

class InvokableIncrementCounter
{
    public function __invoke(array $payload): array
    {
        $payload['counter']++;
        return $payload;
    }
}

$result = Sequence::run(InvokableIncrementCounter::class)
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":1}

Explicit Task

class IncrementTask implements TaskInterface
{
    //...
    
    public function handle(mixed $payload = null): mixed
    {
        $payload['counter']++;
        return $payload;
    }
}

$result = Sequence::run(IncrementTask::class)
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":1}

Custom Class

class IncrementCounter
{
    public function add(array $payload): array
    {
        $payload['counter']++;
        return $payload;
    }
}

$result = Sequence::run([IncrementCounter::class, 'add'])
    ->startWith(['counter' => 0]);

// OR

$result = Sequence::run([new IncrementCounter(), 'add'])
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":1}

Static Method

class IncrementCounter
{
    public static function add(array $payload): array
    {
        $payload['counter']++;
        return $payload;
    }
}

$result = Sequence::run([IncrementCounter::class, 'add'])
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":1}

Closure / Callback / Callable

$closure = function (array $payload): array {
    $payload['counter']++;
    return $payload;
};

$result = Sequence::run($closure)
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":1}

Sequence Instance

$closure = function (array $payload): array {
    $payload['counter']++;
    return $payload;
};

$sequence = Sequence::run($closure)->then($closure);

$result = Sequence::run($sequence)
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":2}

Examples

$result = Sequence::run($closure)			// 1st execution => $counter is 1
	->then(InvokableIncrementCounter::class)	// 2nd execution => $counter is 2
	->then([IncrementCounter::class, 'increment'])	// 3rd execution => $counter is 3
	->then(IncrementTask::class)			// 4th execution => $counter is 4
	->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":4}

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities:

PLEASE DON'T DISCLOSE SECURITY-RELATED ISSUES PUBLICLY

Supported Versions

Only the latest major version receives security fixes.

Reporting a Vulnerability

If you discover a security vulnerability within this project, please open an issue here. All security vulnerabilities will be promptly addressed.

License

The MIT License (MIT). Please see LICENSE file for more information.

sequence's People

Contributors

alcidesrc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.