mirror of
https://github.com/flarum/framework.git
synced 2024-12-11 13:05:50 +08:00
Merge pull request #1441 from flarum/fl/psr-15
Use PSR-15 middleware standard
This commit is contained in:
commit
85d18097b4
|
@ -47,6 +47,8 @@
|
|||
"nikic/fast-route": "^0.6",
|
||||
"oyejorge/less.php": "~1.5",
|
||||
"psr/http-message": "^1.0",
|
||||
"psr/http-server-handler": "^1.0",
|
||||
"psr/http-server-middleware": "^1.0",
|
||||
"symfony/config": "^3.3",
|
||||
"symfony/console": "^3.3",
|
||||
"symfony/http-foundation": "^3.3",
|
||||
|
@ -54,9 +56,8 @@
|
|||
"symfony/yaml": "^3.3",
|
||||
"s9e/text-formatter": "^0.8.1",
|
||||
"tobscure/json-api": "^0.3.0",
|
||||
"zendframework/zend-diactoros": "^1.6",
|
||||
"zendframework/zend-stratigility": "^2.2",
|
||||
"http-interop/http-middleware": "^0.4.0"
|
||||
"zendframework/zend-diactoros": "^1.7",
|
||||
"zendframework/zend-stratigility": "^3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"mockery/mockery": "^0.9.4",
|
||||
|
|
|
@ -12,18 +12,19 @@
|
|||
namespace Flarum\Admin\Middleware;
|
||||
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
class RequireAdministrateAbility implements MiddlewareInterface
|
||||
class RequireAdministrateAbility implements Middleware
|
||||
{
|
||||
use AssertPermissionTrait;
|
||||
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
$this->assertAdmin($request->getAttribute('actor'));
|
||||
|
||||
return $delegate->process($request);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,15 +11,16 @@
|
|||
|
||||
namespace Flarum\Api\Middleware;
|
||||
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
class FakeHttpMethods implements MiddlewareInterface
|
||||
class FakeHttpMethods implements Middleware
|
||||
{
|
||||
const HEADER_NAME = 'x-http-method-override';
|
||||
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
if ($request->getMethod() === 'POST' && $request->hasHeader(self::HEADER_NAME)) {
|
||||
$fakeMethod = $request->getHeaderLine(self::HEADER_NAME);
|
||||
|
@ -27,6 +28,6 @@ class FakeHttpMethods implements MiddlewareInterface
|
|||
$request = $request->withMethod(strtoupper($fakeMethod));
|
||||
}
|
||||
|
||||
return $delegate->process($request);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,12 +13,12 @@ namespace Flarum\Api\Middleware;
|
|||
|
||||
use Exception;
|
||||
use Flarum\Api\ErrorHandler;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
class HandleErrors implements MiddlewareInterface
|
||||
class HandleErrors implements Middleware
|
||||
{
|
||||
/**
|
||||
* @var ErrorHandler
|
||||
|
@ -35,15 +35,11 @@ class HandleErrors implements MiddlewareInterface
|
|||
|
||||
/**
|
||||
* Catch all errors that happen during further middleware execution.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param DelegateInterface $delegate
|
||||
* @return Response
|
||||
*/
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
try {
|
||||
return $delegate->process($request);
|
||||
return $handler->handle($request);
|
||||
} catch (Exception $e) {
|
||||
return $this->errorHandler->handle($e);
|
||||
}
|
||||
|
|
|
@ -14,15 +14,16 @@ namespace Flarum\Http\Middleware;
|
|||
use Flarum\Api\ApiKey;
|
||||
use Flarum\Http\AccessToken;
|
||||
use Flarum\User\User;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
class AuthenticateWithHeader implements MiddlewareInterface
|
||||
class AuthenticateWithHeader implements Middleware
|
||||
{
|
||||
const TOKEN_PREFIX = 'Token ';
|
||||
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
$headerLine = $request->getHeaderLine('authorization');
|
||||
|
||||
|
@ -50,7 +51,7 @@ class AuthenticateWithHeader implements MiddlewareInterface
|
|||
}
|
||||
}
|
||||
|
||||
return $delegate->process($request);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
private function getUser($string)
|
||||
|
|
|
@ -14,13 +14,14 @@ namespace Flarum\Http\Middleware;
|
|||
use Flarum\User\Guest;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
class AuthenticateWithSession implements MiddlewareInterface
|
||||
class AuthenticateWithSession implements Middleware
|
||||
{
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
$session = $request->getAttribute('session');
|
||||
|
||||
|
@ -30,7 +31,7 @@ class AuthenticateWithSession implements MiddlewareInterface
|
|||
|
||||
$request = $request->withAttribute('actor', $actor);
|
||||
|
||||
return $delegate->process($request);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
private function getActor(Session $session)
|
||||
|
|
|
@ -16,12 +16,13 @@ use Flarum\User\AuthToken;
|
|||
use Flarum\User\EmailToken;
|
||||
use Flarum\User\PasswordToken;
|
||||
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
use SessionHandlerInterface;
|
||||
|
||||
class CollectGarbage implements MiddlewareInterface
|
||||
class CollectGarbage implements Middleware
|
||||
{
|
||||
/**
|
||||
* @var SessionHandlerInterface
|
||||
|
@ -39,11 +40,11 @@ class CollectGarbage implements MiddlewareInterface
|
|||
$this->sessionConfig = $config->get('session');
|
||||
}
|
||||
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
$this->collectGarbageSometimes();
|
||||
|
||||
return $delegate->process($request);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
private function collectGarbageSometimes()
|
||||
|
|
|
@ -15,12 +15,12 @@ use FastRoute\Dispatcher;
|
|||
use Flarum\Http\Exception\MethodNotAllowedException;
|
||||
use Flarum\Http\Exception\RouteNotFoundException;
|
||||
use Flarum\Http\RouteCollection;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
class DispatchRoute implements MiddlewareInterface
|
||||
class DispatchRoute implements Middleware
|
||||
{
|
||||
/**
|
||||
* @var RouteCollection
|
||||
|
@ -45,13 +45,10 @@ class DispatchRoute implements MiddlewareInterface
|
|||
/**
|
||||
* Dispatch the given request to our route collection.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param DelegateInterface $delegate
|
||||
* @return Response
|
||||
* @throws MethodNotAllowedException
|
||||
* @throws RouteNotFoundException
|
||||
*/
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
$method = $request->getMethod();
|
||||
$uri = $request->getUri()->getPath() ?: '/';
|
||||
|
|
|
@ -15,15 +15,15 @@ use Exception;
|
|||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Franzl\Middleware\Whoops\WhoopsRunner;
|
||||
use Illuminate\Contracts\View\Factory as ViewFactory;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
|
||||
class HandleErrors implements MiddlewareInterface
|
||||
class HandleErrors implements Middleware
|
||||
{
|
||||
/**
|
||||
* @var ViewFactory
|
||||
|
@ -68,15 +68,11 @@ class HandleErrors implements MiddlewareInterface
|
|||
|
||||
/**
|
||||
* Catch all errors that happen during further middleware execution.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param DelegateInterface $delegate
|
||||
* @return Response
|
||||
*/
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
try {
|
||||
return $delegate->process($request);
|
||||
return $handler->handle($request);
|
||||
} catch (Exception $e) {
|
||||
if ($this->debug) {
|
||||
return WhoopsRunner::handle($e, $request);
|
||||
|
|
|
@ -11,13 +11,14 @@
|
|||
|
||||
namespace Flarum\Http\Middleware;
|
||||
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
class ParseJsonBody implements MiddlewareInterface
|
||||
class ParseJsonBody implements Middleware
|
||||
{
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
if (str_contains($request->getHeaderLine('content-type'), 'json')) {
|
||||
$input = json_decode($request->getBody(), true);
|
||||
|
@ -25,6 +26,6 @@ class ParseJsonBody implements MiddlewareInterface
|
|||
$request = $request->withParsedBody($input ?: []);
|
||||
}
|
||||
|
||||
return $delegate->process($request);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,11 +13,12 @@ namespace Flarum\Http\Middleware;
|
|||
|
||||
use Flarum\Http\AccessToken;
|
||||
use Flarum\Http\CookieFactory;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
class RememberFromCookie implements MiddlewareInterface
|
||||
class RememberFromCookie implements Middleware
|
||||
{
|
||||
/**
|
||||
* @var CookieFactory
|
||||
|
@ -32,7 +33,7 @@ class RememberFromCookie implements MiddlewareInterface
|
|||
$this->cookie = $cookie;
|
||||
}
|
||||
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
$id = array_get($request->getCookieParams(), $this->cookie->getName('remember'));
|
||||
|
||||
|
@ -48,6 +49,6 @@ class RememberFromCookie implements MiddlewareInterface
|
|||
}
|
||||
}
|
||||
|
||||
return $delegate->process($request);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,11 +12,12 @@
|
|||
namespace Flarum\Http\Middleware;
|
||||
|
||||
use Flarum\Locale\LocaleManager;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
class SetLocale implements MiddlewareInterface
|
||||
class SetLocale implements Middleware
|
||||
{
|
||||
/**
|
||||
* @var LocaleManager
|
||||
|
@ -31,7 +32,7 @@ class SetLocale implements MiddlewareInterface
|
|||
$this->locales = $locales;
|
||||
}
|
||||
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
$actor = $request->getAttribute('actor');
|
||||
|
||||
|
@ -45,6 +46,6 @@ class SetLocale implements MiddlewareInterface
|
|||
$this->locales->setLocale($locale);
|
||||
}
|
||||
|
||||
return $delegate->process($request);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,16 +13,17 @@ namespace Flarum\Http\Middleware;
|
|||
|
||||
use Illuminate\Contracts\View\Factory as ViewFactory;
|
||||
use Illuminate\Support\ViewErrorBag;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
/**
|
||||
* Inspired by Illuminate\View\Middleware\ShareErrorsFromSession.
|
||||
*
|
||||
* @author Taylor Otwell
|
||||
*/
|
||||
class ShareErrorsFromSession implements MiddlewareInterface
|
||||
class ShareErrorsFromSession implements Middleware
|
||||
{
|
||||
/**
|
||||
* @var ViewFactory
|
||||
|
@ -37,7 +38,7 @@ class ShareErrorsFromSession implements MiddlewareInterface
|
|||
$this->view = $view;
|
||||
}
|
||||
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
$session = $request->getAttribute('session');
|
||||
|
||||
|
@ -54,6 +55,6 @@ class ShareErrorsFromSession implements MiddlewareInterface
|
|||
|
||||
$session->remove('errors');
|
||||
|
||||
return $delegate->process($request);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,13 +16,13 @@ use Flarum\Http\CookieFactory;
|
|||
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Illuminate\Session\Store;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
use SessionHandlerInterface;
|
||||
|
||||
class StartSession implements MiddlewareInterface
|
||||
class StartSession implements Middleware
|
||||
{
|
||||
/**
|
||||
* @var SessionHandlerInterface
|
||||
|
@ -51,7 +51,7 @@ class StartSession implements MiddlewareInterface
|
|||
$this->config = $config->get('session');
|
||||
}
|
||||
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
$request = $request->withAttribute(
|
||||
'session',
|
||||
|
@ -59,7 +59,7 @@ class StartSession implements MiddlewareInterface
|
|||
);
|
||||
|
||||
$session->start();
|
||||
$response = $delegate->process($request);
|
||||
$response = $handler->handle($request);
|
||||
$session->save();
|
||||
|
||||
$response = $this->withCsrfTokenHeader($response, $session);
|
||||
|
|
|
@ -18,16 +18,17 @@ use Flarum\Http\Middleware\HandleErrors;
|
|||
use Flarum\Http\Middleware\StartSession;
|
||||
use Flarum\Install\InstallServiceProvider;
|
||||
use Flarum\Update\UpdateServiceProvider;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
use Zend\Diactoros\Server as DiactorosServer;
|
||||
use Zend\Stratigility\MiddlewarePipe;
|
||||
use Zend\Stratigility\NoopFinalHandler;
|
||||
use function Zend\Stratigility\middleware;
|
||||
use function Zend\Stratigility\path;
|
||||
|
||||
class Server
|
||||
class Server implements Middleware, Handler
|
||||
{
|
||||
/**
|
||||
* @param Site $site
|
||||
|
@ -46,33 +47,38 @@ class Server
|
|||
public function listen()
|
||||
{
|
||||
DiactorosServer::createServer(
|
||||
$this,
|
||||
[$this, 'handle'],
|
||||
$_SERVER,
|
||||
$_GET,
|
||||
$_POST,
|
||||
$_COOKIE,
|
||||
$_FILES
|
||||
)->listen(new NoopFinalHandler());
|
||||
)->listen();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use as PSR-7 middleware.
|
||||
*
|
||||
* @param ServerRequestInterface $request
|
||||
* @param ResponseInterface $response
|
||||
* @param callable $out
|
||||
* @return ResponseInterface
|
||||
* Use as PSR-15 middleware.
|
||||
*/
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out)
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
$middleware = $this->getMiddleware($request->getUri()->getPath());
|
||||
|
||||
return $middleware($request, $response, $out);
|
||||
return $middleware->process($request, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use as PSR-15 request handler.
|
||||
*/
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
$middleware = $this->getMiddleware($request->getUri()->getPath());
|
||||
|
||||
return $middleware->handle($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $requestPath
|
||||
* @return MiddlewareInterface
|
||||
* @return MiddlewarePipe
|
||||
*/
|
||||
protected function getMiddleware($requestPath)
|
||||
{
|
||||
|
@ -126,9 +132,9 @@ class Server
|
|||
|
||||
protected function getMaintenanceMiddleware(MiddlewarePipe $pipe)
|
||||
{
|
||||
$pipe->pipe(function () {
|
||||
$pipe->pipe(middleware(function () {
|
||||
return new HtmlResponse(file_get_contents($this->getErrorDir().'/503.html', 503));
|
||||
});
|
||||
}));
|
||||
|
||||
// TODO: FOR API render JSON-API error document for HTTP 503
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user