PSRs & Middlewares Pipelines

This interfaces are already (and amazingly) implemented by Zend at projects: Diactoros and Stratigility. Siler wraps them and exposes a function-friendly API handling state internally while achieving a fully-featured and declarative way for: Middleware Pipelining.

PSR-7 HTTP Messages

Siler doesn't have direct dependencies, to stay fit, it favors peer dependencies, which means you have to explicitly declare a diactoros dependency in your project in order to use it.

composer require zendframework/zend-diactoros

You can create a superglobals seeded ServerRequest with Siler\Diactoros\request():

useSiler\Diactoros;

​

$request= Diactoros\request();

And create Responses through helpers:

$json= Diactoros\json(['some'=>'data']);

$html= Diactoros\html('<p>some markup</p>');

$text= Diactoros\text('plain text');

If none of them fits your needs, you can create a raw Response:

$response= Diactoros\response();

$response->getBody()->write('something');

To emit a Response, there is no big deal, if you got Siler, you already imagined that is about one or two function calls, but this time we get the help from HttpHandlerRunner:

composer require zendframework/zend-httphandlerrunner

Then

HttpHandlerRunner\sapi_emit($response);

As in Siler\Http\Response namespace functions, the HttpHandlerRunner\sapi_emit will output headers and text to the buffer, use it carefully.

Example:

<?phpdeclare(strict_types=1);

​

require_once'vendor/autoload.php';

​

useSiler\Diactoros;

useSiler\HttpHandlerRunner;

useSiler\Route;

usefunction Siler\array_get;

​

$request= Diactoros\request();

$response= Route\match([

// /greet/Leo?salute=Hello

Route\get('/greet/{name}',function($params)use($request){

$salute=array_get($request->getQueryParams(),'salute','Olá');

return Diactoros\text("{$salute}{$params['name']}");

},$request),

​

Route\get('/',function(){

return Diactoros\text('hello world');

},$request),

​

Diactoros\text('not found',404),

]);

​

HttpHandlerRunner\sapi_emit($response);

PSR-15 Middleware Pipelining

composer require zendframework/zend-stratigility

Siler doesn't have direct dependencies, to stay fit, it favors peer dependencies, which means you have to explicitly declare a stratigility dependency in your project in order to use it.

A very simple Hello World example:

usefunction Siler\Diactoros\request;

usefunction Siler\Diactoros\text;

usefunction Siler\HttpHandlerRunner\sapi_emit;

usefunction Siler\Stratigility\handle;

usefunction Siler\Stratigility\pipe;

​

pipe(function($request,$handler){

returntext('hello world');

});

​

sapi_emit(handle(request()));

It's more uses than actual code because Siler is abstracting all the way down for you.

API

Description

pipe

Creates a Stratigility MiddlewarePipe with a default name and pipes the given Clousure to it already wrapping it inside a MiddlewareInterface decorator, or you can pass any implementation MiddlewareInterface to it.

text

Creates a Diactoros TextResponse. The Diactoros namespace in Siler is basically just helper functions for Responses.

sapi_emit

Creates and immediately calls emit method on a HttpHandlerRunner SapiEmitter.

The second argument on pipe here is a Pipeline name, you can pipe middlewares to any number of pipelines, then in Stratigility\process we marshal it, from the given $request and returns a Closure to be called on a final handler.