knot-lib/pipeline

Pipeline library for kNot Framework.

0.3.0 2021-06-06 08:11 UTC

This package is auto-updated.

Last update: 2022-05-06 09:58:55 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Code Climate Total Downloads

Description

pipeline library for kNot Framework.

Feature

Demo

Example 1: how to use middleware and pipeline

use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use knotlib\kernel\Request\PhpArrayRequest;
use knotlib\pipeline\pipeline;

class FirstMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
    {
        echo 'FirstMiddleware: 1' . PHP_EOL;
        $response = $handler->handle($request);
        echo 'FirstMiddleware: 2' . PHP_EOL;
        return $response;
    }
}
class SecondMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
    {
        echo 'SecondMiddleware: 1' . PHP_EOL;
        $response = $handler->handle($request);
        echo 'SecondMiddleware: 2' . PHP_EOL;
        return $response;
    }
}
class ThirdMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
    {
        echo 'ThirdMiddleware: 1' . PHP_EOL;
        $response = $handler->handle($request);
        echo 'ThirdMiddleware: 2' . PHP_EOL;
        return $response;
    }
}

$pipeline = new pipeline();
$pipeline->push(new FirstMiddleware());
$pipeline->push(new SecondMiddleware());
$pipeline->push(new ThirdMiddleware());

$pipeline->run(new PhpArrayRequest);

Output:

ThirdMiddleware: 1
SecondMiddleware: 1
FirstMiddleware: 1
FirstMiddleware: 2
SecondMiddleware: 2
ThirdMiddleware: 2

Usage

Requirement

PHP 7.2 or later

Installing knot-lib/pipeline

The recommended way to install knot-lib/pipeline is through Composer.

composer require knot-lib/pipeline

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

License

MIT

Author

stk2k

Disclaimer

This software is no warranty.

We are not responsible for any results caused by the use of this software.

Please use the responsibility of the your self.