Stretcher - PHP Library
, (*1)
PSR-7 stretchable middleware dispatcher, (*2)
Installation
Install using composer:, (*3)
$ composer require assouan/stretcher
Introduction
, (*4)
Usage
Stretcher:, (*5)
<?php
$app = new Stretcher();
// = new Stretcher($resolver);
$app->add($callable1);
$app->add($callable2);
$app->add($callable3);
$response = $app->dispatch($request, $response);
Add middlewares in the queue priority in runetime:, (*6)
<?php
function middleware($request, $response, $next)
{
// Write response header
...
// Call middleware 'render_html_nav' and 'MyBlog::lastNews' in priority and continue next
$response = $next($request, $response, 'render_html_nav', 'MyBlog::lastNews')
// Write response footer
...
return $response;
}
Default resolver:, (*7)
<?php
// Middleware = class name
$app->add('@Middleware');
// Controller = class name
// action = method name
$app->add('@Controller.action');
Write a middleware:, (*8)
<?php
class MyMiddleware
{
public function __invoke($request, $response, $next)
{
return $response;
}
}
Write a controller:, (*9)
<?php
class MyController
{
public function myActionA()
{
return new Response;
}
public function myActionB($request)
{
return new Response;
}
public function myActionC($request, $response, $next)
{
return new Response;
}
}
Example
Bootstrap file:, (*10)
<?php
$app = new Stretcher();
$app->add(IsHttpsMiddleware::class);
$app->add('@AppKernelMiddleware'); // middleware callable on default resolver
$app->add([$router, 'run']);
$app->add('display_error_404');
$response = $app->dispatch($request, $response);
echo $response->getBody();
Routing file:, (*11)
<?php
$router = new Router();
$router->map('/', '@HomeMiddleware');
$router->map('/blog', '@BlogMiddleware.showNews');
$router->map('/blog/edit', '@BlogMiddleware.addNews');
BlogMiddleware file:, (*12)
<?php
class BlogMiddleware
{
public function showNews($request)
{
...
return $response;
}
public function addNews($request, $response, $next)
{
return $next($request, $response, '@BlogEdit.hasAccess', '@BlogEdit.adminNav', '@BlogEdit.adminEditor');
}
}