header('HTTP/1.1 404 Not Found');
header('Content-type: text/html');
echo 'Not found';
namespace Symfony\Component\HttpKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
interface HttpKernelInterface
{
public function handle(Request $request): Response;
}
function (ServerRequestInterface $request): ResponseInterface
function (
ServerRequestInterface $request,
ResponseInterface $response
): ResponseInterface
function (
ServerRequestInterface $request,
ResponseInterface $response,
callable $next
): ResponseInterface
namespace Psr\Http\ServerMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
interface MiddlewareInterface
{
public function process(
ServerRequestInterface $request,
DelegateInterface $delegate
): ResponseInterface;
}
namespace Psr\Http\ServerMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
interface DelegateInterface
{
public function process(
ServerRequestInterface $request
): ResponseInterface;
}
$a = function ($request, $response, $next) {
$response = $response->withHeader('X-Foo', 'foo');
return $next(
$request->withAttribute('yay', 'woo'),
$response
);
};
$b = function ($request, $response, $next) {
$yay = $request->getAttribute('yay'); // woo
return new Response();
}
$a(new Request(), new Response(), $b); // fail :(
https://gist.github.com/asgrim/0d8b1cb7a4fa362db8fed06444d104ae
require __DIR__ . '/../vendor/autoload.php';
$container = require __DIR__ . '/../config/container.php';
$app = $container->get(Application::class);
$app->pipe(ErrorHandler::class);
$app->pipe(Session::class);
$app->pipe(Authentication::class);
$app->pipeRoutingMiddleware();
$app->pipe(Authorization::class);
$app->pipe(InputValidation::class);
$app->pipeDispatchMiddleware();
$app->pipe(NotFoundHandler::class);
$app->run();
// ...Bootstrap $app
$before = serialize($app);
$app->run();
$after = serialize($app);
$this->assertSame($before, $after);
use PHPFastCGI\FastCGIDaemon\ApplicationFactory;
use PHPFastCGI\Adapter\Expressive\ApplicationWrapper;
// ...Bootstrap $app
// Create the kernel for FastCGIDaemon (from Expressive app)
$kernel = new ApplicationWrapper($app);
// Create the symfony console application
$daemon = (new ApplicationFactory)->createApplication($kernel);
// Run the symfony console application
$daemon->run();
https://github.com/PHPFastCGI/ExpressiveAdapter
class ErrorHandler implements MiddlewareInterface
{
public function process($request, $delegate)
{
try {
return $delegate->process($request);
} catch (Throwable $e) {
return $this->createErrorResponse($request, $e);
}
}
}
https://zendframework.github.io/zend-expressive/features/error-handling/
class Session implements MiddlewareInterface
{
public function process($request, $delegate)
{
$session = Session::fromRequest($request);
$response = $delegate->process(
$request->withAttribute('session', $session)
);
return $session->writeToResponse($response);
}
}
https://github.com/psr7-sessions/storageless
class OAuth implements MiddlewareInterface
{
public function process($request, $delegate)
{
$user = $this->oAuthService->authenticate($request);
return $delegate->process(
$request->withAttribute('user', $user)
);
}
}
https://github.com/zf-fr/zfr-oauth2-server
class Router implements MiddlewareInterface
{
public function process($request, $delegate)
{
$matchedRoute = $this->router->match($request);
return $delegate->process(
$request->withAttribute('route', $matchedRoute)
);
}
}
https://zendframework.github.io/zend-expressive/features/router/intro/
class Authorization implements MiddlewareInterface
{
public function process($request, $delegate)
{
$user = $request->getAttribtue('user');
$route = $request->getAttribtue('route');
$isAuthorized = $this->isAuthorized($user, $route);
if ($isAuthorized) {
return $delegate->process($request);
}
return $this->createUnauthorizedResponse($user, $route);
}
}
https://github.com/ZF-Commons/zfc-rbac
class JsonParser implements MiddlewareInterface
{
public function process($request, $delegate)
{
$body = (string) $request->getBody();
$data = json_decode($body, true);
return $delegate->process(
$request->withParsedBody($data)
);
}
}
https://zendframework.github.io/zend-expressive/features/helpers/body-parse/
class JsonSchemaValidator implements MiddlewareInterface
{
public function process($request, $delegate)
{
$validator = new JsonSchema\Validator();
$data = $request->getParsedBody();
$validator->validate($data, '/my/schema.json');
if ($validator->isValid()) {
return $delegate->process($request);
}
return $this->createErrorResponse($validator);
}
https://github.com/oscarotero/psr7-middlewares#jsonschema
class DispatchMiddleware implements MiddlewareInterface
{
public function process($request, $delegate)
{
$routeResult = $request->getAttribute(RouteResult::class);
if (null === $routeResult) {
return $delegate->process($request);
}
$middleware = $routeResult->getMatchedMiddleware();
return $middleware->process($request, $delegate);
}
}
https://zendframework.github.io/zend-expressive/features/router/intro/
class NotFoundHandler implements MiddlewareInterface
{
public function process($request, $delegate)
{
return new Response(404);
}
}
https://zendframework.github.io/zend-expressive/cookbook/custom-404-page-handling/
Daniel Gimenes
Twitter: @Danizord
Github: @Danizord
Feedback: https://joind.in/talk/1bca0