dev-master
9999999-devA Middleware Proof-Of-Concept Library For PHP
MIT
The Requires
- php ^7
- psr/http-message ^1.0
The Development Requires
Wallogit.com
2017 © Pedro Peláez
A Middleware Proof-Of-Concept Library For PHP
A PSR-7 Middleware Interface proof-of-concept for PHP., (*1)
Yes, that's the only hard requirement, (*2)
To use this runner, you need to pick a PSR-7 Library. We'll use Guzzle's., (*3)
First, install it: composer require guzzle/psr7, (*4)
Now, we need a factory instance for the PSR-7 Library;, (*5)
$factory = new Tari\Adapter\Guzzle\Factory;
Next, we boot up the "Server":, (*6)
$server = new Tari\Server($factory);
Next, append whatever middleware we want to. In this case, let's add the error handler and the HSTS middleware:, (*7)
$server->append(new Tari\ServerMiddleware\ErrorHandler); $server->append(new Tari\ServerMiddleware\HSTS(300 /* Max-age in seconds */));
We can also add middleware as closures (Notice we don't need types):, (*8)
$server->append(function($request, $frame) {
$response = $frame->next($request);
return $response->withHeader('X-Powered-By', 'Tari-PHP');
});
We also need a "default" action to take:, (*9)
$default = function($request) use ($factory) {
// Default to a 404 NOT FOUND response
return $factory->createResponse("Not Found", 404);
};
Finally, we can run out stack:, (*10)
$request = new Guzzle\Psr7\ServerRequest("http://www.example.com/foo", "GET");
$response = $server->run($request, $default);
And that's all there is to it..., (*11)
To use this middleware as a library author, simply implement the Tari\MiddlewareInterface interface. It's as easy as that:, (*12)
use Tari\ServerMiddlewareInterface;
use Tari\ServerFrameInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
class Foo implements ServerMiddlewareInterface {
public function handle(ServerRequestInterface $request, ServerFrameInterface $frame): ResponseInterface {
// Do your modifications to the request here
$response = $frame->next($request);
// Do your modifications to the response here
return $response;
}
}
It's as simple as that., (*13)
Sometimes, you don't want to continue with a request. If you detect that situation in your middleware, simply create a new response:, (*14)
use Tari\ServerMiddlewareInterface;
use Tari\ServerFrameInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
class Foo implements ServerMiddlewareInterface {
public function handle(ServerRequestInterface $request, ServerFrameInterface $frame): ResponseInterface {
if ($this->isBadRequest($request)) {
return $frame->factory()->createResponse("Bad Request", 400);
}
return $frame->next($request);
}
}
Tari defines 3 consumable interfaces:, (*15)
interface ServerMiddlewareInterface {
public function handle(ServerRequestInterface $request, ServerFrameInterface $frame): ResponseInterface;
}
Used for Server request processing, (*16)
interface ServerFrameInterface {
public function next(ServerRequestInterface $request): ResponseInterface;
public function factory(): FactoryInterface;
}
This is used for processing server requests, (*17)
interface FactoryInterface {
public function createRequest(
UriInterface $uri = null,
string $method = '',
array $headers = [],
$body = null
): RequestInterface;
public function createServerRequest(
UriInterface $uri = null,
string $method = '',
array $headers = [],
$body = null
): ServerRequestInterface;
public function createResponse(
int $status = 200,
array $headers = [],
$body = null
): ResponseInterface;
public function createStream($data = null): StreamInterface;
public function createUri(string $uri = ''): UriInterface;
public function createUploadedFile(
$data,
int $size,
int $error,
string $clientFile = '',
string $clientMediaType = ''
): UploadedFileInterface;
}
There's a lot more going on here, but it's still extremely straight forward and simple., (*18)
Each method creates a PSR-7 object, and initializes it., (*19)
MIT, (*20)
A Middleware Proof-Of-Concept Library For PHP
MIT