Merge pull request #1100 from flarum/stratigility-update

Update to Zend Stratigility 1.3
This commit is contained in:
Franz Liedke 2017-02-03 22:03:55 +01:00 committed by GitHub
commit 82c84caf5a
8 changed files with 49 additions and 17 deletions

View File

@ -1,9 +1,9 @@
language: php language: php
php: php:
- 5.5
- 5.6 - 5.6
- 7.0 - 7.0
- 7.1
- hhvm - hhvm
matrix: matrix:

View File

@ -20,7 +20,7 @@
"docs": "http://flarum.org/docs" "docs": "http://flarum.org/docs"
}, },
"require": { "require": {
"php": ">=5.5.9", "php": ">=5.6.0",
"dflydev/fig-cookies": "^1.0.2", "dflydev/fig-cookies": "^1.0.2",
"doctrine/dbal": "^2.5", "doctrine/dbal": "^2.5",
"components/font-awesome": "^4.6", "components/font-awesome": "^4.6",
@ -53,7 +53,7 @@
"s9e/text-formatter": "^0.8.1", "s9e/text-formatter": "^0.8.1",
"tobscure/json-api": "^0.3.0", "tobscure/json-api": "^0.3.0",
"zendframework/zend-diactoros": "^1.1", "zendframework/zend-diactoros": "^1.1",
"zendframework/zend-stratigility": "1.2.*" "zendframework/zend-stratigility": "^1.3"
}, },
"require-dev": { "require-dev": {
"mockery/mockery": "^0.9.4", "mockery/mockery": "^0.9.4",

View File

@ -25,11 +25,16 @@ class Server extends AbstractServer
protected function getMiddleware(Application $app) protected function getMiddleware(Application $app)
{ {
$pipe = new MiddlewarePipe; $pipe = new MiddlewarePipe;
$pipe->raiseThrowables();
if ($app->isInstalled()) { if ($app->isInstalled()) {
$path = parse_url($app->url('admin'), PHP_URL_PATH); $path = parse_url($app->url('admin'), PHP_URL_PATH);
$errorDir = __DIR__.'/../../error'; $errorDir = __DIR__.'/../../error';
// All requests should first be piped through our global error handler
$debugMode = ! $app->isUpToDate() || $app->inDebugMode();
$pipe->pipe($path, new HandleErrors($errorDir, $app->make('log'), $debugMode));
if ($app->isUpToDate()) { if ($app->isUpToDate()) {
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\ParseJsonBody')); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\ParseJsonBody'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession')); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession'));
@ -41,12 +46,10 @@ class Server extends AbstractServer
event(new ConfigureMiddleware($pipe, $path, $this)); event(new ConfigureMiddleware($pipe, $path, $this));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.admin.routes')])); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.admin.routes')]));
$pipe->pipe($path, new HandleErrors($errorDir, $app->make('log'), $app->inDebugMode()));
} else { } else {
$app->register('Flarum\Update\UpdateServiceProvider'); $app->register('Flarum\Update\UpdateServiceProvider');
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.update.routes')])); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.update.routes')]));
$pipe->pipe($path, new HandleErrors($errorDir, $app->make('log'), true));
} }
} }

View File

@ -11,12 +11,12 @@
namespace Flarum\Api\Middleware; namespace Flarum\Api\Middleware;
use Exception;
use Flarum\Api\ErrorHandler; use Flarum\Api\ErrorHandler;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\ErrorMiddlewareInterface;
class HandleErrors implements ErrorMiddlewareInterface class HandleErrors
{ {
/** /**
* @var ErrorHandler * @var ErrorHandler
@ -32,10 +32,19 @@ class HandleErrors implements ErrorMiddlewareInterface
} }
/** /**
* {@inheritdoc} * Catch all errors that happen during further middleware execution.
*
* @param Request $request
* @param Response $response
* @param callable $out
* @return Response
*/ */
public function __invoke($e, Request $request, Response $response, callable $out = null) public function __invoke(Request $request, Response $response, callable $out = null)
{ {
return $this->errorHandler->handle($e); try {
return $out($request, $response);
} catch (Exception $e) {
return $this->errorHandler->handle($e);
}
} }
} }

View File

@ -25,10 +25,13 @@ class Server extends AbstractServer
protected function getMiddleware(Application $app) protected function getMiddleware(Application $app)
{ {
$pipe = new MiddlewarePipe; $pipe = new MiddlewarePipe;
$pipe->raiseThrowables();
$path = parse_url($app->url('api'), PHP_URL_PATH); $path = parse_url($app->url('api'), PHP_URL_PATH);
if ($app->isInstalled() && $app->isUpToDate()) { if ($app->isInstalled() && $app->isUpToDate()) {
$pipe->pipe($path, $app->make('Flarum\Api\Middleware\HandleErrors'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\ParseJsonBody')); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\ParseJsonBody'));
$pipe->pipe($path, $app->make('Flarum\Api\Middleware\FakeHttpMethods')); $pipe->pipe($path, $app->make('Flarum\Api\Middleware\FakeHttpMethods'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession')); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession'));
@ -40,7 +43,6 @@ class Server extends AbstractServer
event(new ConfigureMiddleware($pipe, $path, $this)); event(new ConfigureMiddleware($pipe, $path, $this));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.api.routes')])); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.api.routes')]));
$pipe->pipe($path, $app->make('Flarum\Api\Middleware\HandleErrors'));
} else { } else {
$pipe->pipe($path, function () { $pipe->pipe($path, function () {
$document = new Document; $document = new Document;

View File

@ -26,6 +26,7 @@ class Server extends AbstractServer
protected function getMiddleware(Application $app) protected function getMiddleware(Application $app)
{ {
$pipe = new MiddlewarePipe; $pipe = new MiddlewarePipe;
$pipe->raiseThrowables();
$path = parse_url($app->url(), PHP_URL_PATH); $path = parse_url($app->url(), PHP_URL_PATH);
$errorDir = __DIR__.'/../../error'; $errorDir = __DIR__.'/../../error';
@ -33,10 +34,13 @@ class Server extends AbstractServer
if (! $app->isInstalled()) { if (! $app->isInstalled()) {
$app->register('Flarum\Install\InstallServiceProvider'); $app->register('Flarum\Install\InstallServiceProvider');
$pipe->pipe($path, new HandleErrors($errorDir, $app->make('log'), true));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession')); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.install.routes')])); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.install.routes')]));
$pipe->pipe($path, new HandleErrors($errorDir, $app->make('log'), true));
} elseif ($app->isUpToDate() && ! $app->isDownForMaintenance()) { } elseif ($app->isUpToDate() && ! $app->isDownForMaintenance()) {
$pipe->pipe($path, new HandleErrors($errorDir, $app->make('log'), $app->inDebugMode()));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\ParseJsonBody')); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\ParseJsonBody'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession')); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\RememberFromCookie')); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\RememberFromCookie'));
@ -46,7 +50,6 @@ class Server extends AbstractServer
event(new ConfigureMiddleware($pipe, $path, $this)); event(new ConfigureMiddleware($pipe, $path, $this));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.forum.routes')])); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.forum.routes')]));
$pipe->pipe($path, new HandleErrors($errorDir, $app->make('log'), $app->inDebugMode()));
} else { } else {
$pipe->pipe($path, function () use ($errorDir) { $pipe->pipe($path, function () use ($errorDir) {
return new HtmlResponse(file_get_contents($errorDir.'/503.html', 503)); return new HtmlResponse(file_get_contents($errorDir.'/503.html', 503));

View File

@ -26,6 +26,7 @@ class FullStackServer extends AbstractServer
protected function getMiddleware(Application $app) protected function getMiddleware(Application $app)
{ {
$pipe = new MiddlewarePipe; $pipe = new MiddlewarePipe;
$pipe->raiseThrowables();
$pipe->pipe(new ApiServer); $pipe->pipe(new ApiServer);
$pipe->pipe(new AdminServer); $pipe->pipe(new AdminServer);

View File

@ -11,14 +11,14 @@
namespace Flarum\Http\Middleware; namespace Flarum\Http\Middleware;
use Exception;
use Franzl\Middleware\Whoops\ErrorMiddleware as WhoopsMiddleware; use Franzl\Middleware\Whoops\ErrorMiddleware as WhoopsMiddleware;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Zend\Diactoros\Response\HtmlResponse; use Zend\Diactoros\Response\HtmlResponse;
use Zend\Stratigility\ErrorMiddlewareInterface;
class HandleErrors implements ErrorMiddlewareInterface class HandleErrors
{ {
/** /**
* @var string * @var string
@ -48,9 +48,23 @@ class HandleErrors implements ErrorMiddlewareInterface
} }
/** /**
* {@inheritdoc} * Catch all errors that happen during further middleware execution.
*
* @param Request $request
* @param Response $response
* @param callable $out
* @return Response
*/ */
public function __invoke($error, Request $request, Response $response, callable $out = null) public function __invoke(Request $request, Response $response, callable $out = null)
{
try {
return $out($request, $response);
} catch (Exception $e) {
return $this->formatException($e);
}
}
protected function formatException(Exception $error)
{ {
$status = 500; $status = 500;
$errorCode = $error->getCode(); $errorCode = $error->getCode();