PHP code example of gamringer / pipe

1. Go to this page and download the library: Download gamringer/pipe library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gamringer / pipe example snippets




declare(strict_types=1);

st;
use GuzzleHttp\Psr7\Response;
use gamringer\Pipe\Pipe;
use gamringer\Pipe\Example\StaticMiddleware;

// Request to be handled later
$request = new ServerRequest('GET', '/');

// For use in this example, we build a few Middleware objects

// One that simply returns a predetermined response in all cases
$staticResponse = new Response();
$staticResponseMiddleware = new StaticMiddleware($staticResponse);

// One that echoes it's constructor argument before passing to the next one.
$fooMiddleware = new FooMiddleware('1');


// Build Pipe from an array of Middlewares
$pipe = new Pipe([
    $staticResponseMiddleware,
]);

// Use Pipe via RequestHandlerInterface
$response = $pipe->handle($request);

// Build empty Pipe
$pipe = new Pipe();

// Add Middlewares dynamically
$pipe->push($staticResponseMiddleware);

// Use Pipe via RequestHandlerInterface
$response = $pipe->handle($request);

// Build empty Pipe
$pipe = new Pipe();

// Add Middlewares dynamically
$pipe->push($fooMiddleware);
$pipe->push($staticResponseMiddleware);

// Use Pipe via RequestHandlerInterface
$response = $pipe->handle($request);

// Build empty Pipe
$pipe = new Pipe();

// Add Middlewares dynamically
$pipe->push($fooMiddleware);
$pipe->push(new Pipe([
    new FooMiddleware('2') 
]));
$pipe->push($staticResponseMiddleware);

// Use Pipe via RequestHandlerInterface
$response = $pipe->handle($request);

$pipe = new Pipe();
$pipe->handle($request); // Throws new \gamringer\Pipe\TerminalException()

// Build empty Pipe
$pipe = new Pipe();

// Add Middlewares dynamically
$pipe->push($fooMiddleware);
$pipe->push(new Pipe([
    new FooMiddleware('2')
]));
$pipe->push(new Pipe());
$pipe->push($staticResponseMiddleware);

// Use Pipe via RequestHandlerInterface
$response = $pipe->handle($request);