<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
noglitchyo / middleware-collection-request-handler example snippets
use NoGlitchYo\MiddlewareCollectionRequestHandler\RequestHandler;
use NoGlitchYo\MiddlewareCollectionRequestHandler\Collection\SplStackMiddlewareCollection;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
// Instantiate a collection of middlewares with an anonymous middleware class.
// In this example, we are using a "stack" implementation of the collection.
$middlewareCollection = new SplStackMiddlewareCollection([
new class implements MiddlewareInterface{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface{
return $handler->handle($request);
}
}
]);
// Let's add one more middleware in the collection (this time, from a callable)
$middlewareCollection->addFromCallable(function(ServerRequestInterface $request, RequestHandlerInterface $handler){
return $handler->handle($request);
});
// Instantiate a new request handler with a default handler and the middleware collection.
$requestHandler = RequestHandler::fromCallable(
function (ServerRequestInterface $serverRequest){
return new /* instance of ResponseInterface */;
},
$middlewareCollection
);
// As a RequestHandler:
// Pass the request to the request handler which will dispatch the request to the middlewares.
$response = $requestHandler->handle(/* ServerRequestInterface */);
interface MiddlewareCollectionInterface
{
/**
* Must return true if there is no middleware in the collection to process.
* @return bool
*/
public function isEmpty(): bool;
/**
* Must return the next middleware to process in the collection.
* Depending on the implemented strategy, the middleware MAY not be removed from the collection.
* @return MiddlewareInterface
*/
public function next(): MiddlewareInterface;
/**
* Add a middleware instance of MiddlewareInterface to the collection.
*
* @param MiddlewareInterface $middleware
*
* @return MiddlewareCollectionInterface
*/
public function add(MiddlewareInterface $middleware): MiddlewareCollectionInterface;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.