Skip to content

Commit

Permalink
Use PSR-15 middleware standard
Browse files Browse the repository at this point in the history
This finally adopts the new standardized interfaces instead of the
work-in-progress ones with the `Interop\` prefix.

Since we have now updated to PHP 7.1, we can also use Stratigility
3.0 as the middleware dispatcher.
  • Loading branch information
franzliedke committed May 28, 2018
1 parent 5d0ebde commit 3680d88
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 96 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@
"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",
"symfony/translation": "^3.3",
"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",
Expand Down
11 changes: 6 additions & 5 deletions src/Admin/Middleware/RequireAdministrateAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 6 additions & 5 deletions src/Api/Middleware/FakeHttpMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@

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);

$request = $request->withMethod(strtoupper($fakeMethod));
}

return $delegate->process($request);
return $handler->handle($request);
}
}
14 changes: 5 additions & 9 deletions src/Api/Middleware/HandleErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

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
Expand All @@ -35,15 +35,11 @@ public function __construct(ErrorHandler $errorHandler)

/**
* 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);
}
Expand Down
11 changes: 6 additions & 5 deletions src/Http/Middleware/AuthenticateWithHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
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');

Expand Down Expand Up @@ -50,7 +51,7 @@ public function process(Request $request, DelegateInterface $delegate)
}
}

return $delegate->process($request);
return $handler->handle($request);
}

private function getUser($string)
Expand Down
11 changes: 6 additions & 5 deletions src/Http/Middleware/AuthenticateWithSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
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');

Expand All @@ -30,7 +31,7 @@ public function process(Request $request, DelegateInterface $delegate)

$request = $request->withAttribute('actor', $actor);

return $delegate->process($request);
return $handler->handle($request);
}

private function getActor(Session $session)
Expand Down
11 changes: 6 additions & 5 deletions src/Http/Middleware/CollectGarbage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
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
Expand All @@ -39,11 +40,11 @@ public function __construct(SessionHandlerInterface $handler, ConfigRepository $
$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()
Expand Down
11 changes: 4 additions & 7 deletions src/Http/Middleware/DispatchRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
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
Expand All @@ -45,13 +45,10 @@ public function __construct(RouteCollection $routes)
/**
* 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() ?: '/';
Expand Down
14 changes: 5 additions & 9 deletions src/Http/Middleware/HandleErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
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
Expand Down Expand Up @@ -68,15 +68,11 @@ public function __construct(ViewFactory $view, LoggerInterface $logger, Translat

/**
* 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);
Expand Down
11 changes: 6 additions & 5 deletions src/Http/Middleware/ParseJsonBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@

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);

$request = $request->withParsedBody($input ?: []);
}

return $delegate->process($request);
return $handler->handle($request);
}
}
11 changes: 6 additions & 5 deletions src/Http/Middleware/RememberFromCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

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
Expand All @@ -32,7 +33,7 @@ public function __construct(CookieFactory $cookie)
$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'));

Expand All @@ -48,6 +49,6 @@ public function process(Request $request, DelegateInterface $delegate)
}
}

return $delegate->process($request);
return $handler->handle($request);
}
}
11 changes: 6 additions & 5 deletions src/Http/Middleware/SetLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +32,7 @@ public function __construct(LocaleManager $locales)
$this->locales = $locales;
}

public function process(Request $request, DelegateInterface $delegate)
public function process(Request $request, Handler $handler): Response
{
$actor = $request->getAttribute('actor');

Expand All @@ -45,6 +46,6 @@ public function process(Request $request, DelegateInterface $delegate)
$this->locales->setLocale($locale);
}

return $delegate->process($request);
return $handler->handle($request);
}
}
11 changes: 6 additions & 5 deletions src/Http/Middleware/ShareErrorsFromSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@

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
Expand All @@ -37,7 +38,7 @@ public function __construct(ViewFactory $view)
$this->view = $view;
}

public function process(Request $request, DelegateInterface $delegate)
public function process(Request $request, Handler $handler): Response
{
$session = $request->getAttribute('session');

Expand All @@ -54,6 +55,6 @@ public function process(Request $request, DelegateInterface $delegate)

$session->remove('errors');

return $delegate->process($request);
return $handler->handle($request);
}
}
Loading

0 comments on commit 3680d88

Please sign in to comment.