Siler
Search…
PSRs & Middleware Pipeline
This interfaces are already (and amazingly) implemented by Laminas 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.
1
composer require laminas/laminas-diactoros
Copied!
You can create a superglobals seeded ServerRequest with Siler\Diactoros\request():
1
use Siler\Diactoros;
2
3
$request = Diactoros\request();
Copied!
And create Responses through helpers:
1
$json = Diactoros\json(['some' => 'data']);
2
$html = Diactoros\html('<p>some markup</p>');
3
$text = Diactoros\text('plain text');
Copied!
If none of them fits your needs, you can create a raw Response:
1
$response = Diactoros\response();
2
$response->getBody()->write('something');
Copied!
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:
1
composer require laminas/laminas-httphandlerrunner
Copied!
Then
1
HttpHandlerRunner\sapi_emit($response);
Copied!
As in Siler\Http\Response namespace functions, the HttpHandlerRunner\sapi_emit will output headers and text to the buffer, use it carefully.
Example:
1
<?php declare(strict_types=1);
2
3
require_once 'vendor/autoload.php';
4
5
use Siler\Diactoros;
6
use Siler\HttpHandlerRunner;
7
use Siler\Route;
8
use function Siler\array_get;
9
10
$request = Diactoros\request();
11
$response = Route\match([
12
// /greet/Leo?salute=Hello
13
Route\get('/greet/{name}', function ($params) use ($request) {
14
$salute = array_get($request->getQueryParams(), 'salute', 'Olá');
15
return Diactoros\text("{$salute} {$params['name']}");
16
}, $request),
17
18
Route\get('/', function () {
19
return Diactoros\text('hello world');
20
}, $request),
21
22
Diactoros\text('not found', 404),
23
]);
24
25
HttpHandlerRunner\sapi_emit($response);
Copied!

PSR-15 Middleware Pipelining

1
composer require laminas/laminas-stratigility
Copied!
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:
1
use function Siler\Diactoros\request;
2
use function Siler\Diactoros\text;
3
use function Siler\HttpHandlerRunner\sapi_emit;
4
use function Siler\Stratigility\handle;
5
use function Siler\Stratigility\pipe;
6
7
pipe(function ($request, $handler) {
8
return text('hello world');
9
});
10
11
sapi_emit(handle(request()));
Copied!
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.
handle
Calls handle on a MiddlewarePipe marshaling the Request.
request
Creates a Diactoros ServerRequest using PHP's Globals.

Siler's Routes

You can also run pipelines for specific routes:
1
use Siler\Diactoros;
2
use Siler\Http\Request;
3
use Siler\HttpHandlerRunner;
4
use Siler\Route;
5
use Siler\Stratigility;
6
7
$userMiddleware = function ($request, $handler) {
8
$token = Request\get('token');
9
10
if (empty($token)) {
11
return Diactoros\json('no user', 401);
12
}
13
14
$user = "get_user_by_token:$token";
15
$request = $request->withAttribute('user', $user);
16
17
return $handler->handle($request);
18
};
19
20
$homeHandler = function () {
21
return Diactoros\json('welcome');
22
};
23
24
$adminHandler = function ($request) {
25
return Diactoros\json(['user' => $request->getAttribute('user')]);
26
};
27
28
$secretHandler = function ($request) {
29
return Diactoros\json(['user' => $request->getAttribute('user')]);
30
};
31
32
Stratigility\pipe($userMiddleware, 'auth');
33
34
$request = Diactoros\request();
35
$response = Route\match([
36
Route\get('/', $homeHandler, $request),
37
Route\get('/admin', Stratigility\process($request, 'auth')($adminHandler), $request),
38
Route\get('/secret', Stratigility\process($request, 'auth')($secretHandler), $request),
39
Diactoros\json('not found', 404),
40
]);
41
42
HttpHandlerRunner\sapi_emit($response);
Copied!
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.
Last modified 1yr ago