From 38695cdee3ee27e1c4c6f41eedc0dc03e86b496d Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Mon, 2 Jan 2017 22:54:55 +0100 Subject: [PATCH 1/3] Update to Zend Stratigility 1.3 * Fix dependency version constraint. (Reverts #1066.) * Allow exceptions to be raised when dispatching middleware. * Fix our error handler middleware (do not implement Stratigility's error handler interface, catch exceptions instead). See https://docs.zendframework.com/zend-stratigility/migration/to-v2/. Closes #1069. --- framework/core/composer.json | 2 +- framework/core/src/Admin/Server.php | 7 ++++-- .../core/src/Api/Middleware/HandleErrors.php | 19 +++++++++++----- framework/core/src/Api/Server.php | 4 +++- framework/core/src/Forum/Server.php | 7 ++++-- framework/core/src/Http/FullStackServer.php | 1 + .../core/src/Http/Middleware/HandleErrors.php | 22 +++++++++++++++---- 7 files changed, 47 insertions(+), 15 deletions(-) diff --git a/framework/core/composer.json b/framework/core/composer.json index 9fe23f9a5..42dc78a11 100644 --- a/framework/core/composer.json +++ b/framework/core/composer.json @@ -53,7 +53,7 @@ "s9e/text-formatter": "^0.8.1", "tobscure/json-api": "^0.3.0", "zendframework/zend-diactoros": "^1.1", - "zendframework/zend-stratigility": "1.2.*" + "zendframework/zend-stratigility": "^1.3" }, "require-dev": { "mockery/mockery": "^0.9.4", diff --git a/framework/core/src/Admin/Server.php b/framework/core/src/Admin/Server.php index 69b844ba3..43aa38116 100644 --- a/framework/core/src/Admin/Server.php +++ b/framework/core/src/Admin/Server.php @@ -25,11 +25,16 @@ class Server extends AbstractServer protected function getMiddleware(Application $app) { $pipe = new MiddlewarePipe; + $pipe->raiseThrowables(); if ($app->isInstalled()) { $path = parse_url($app->url('admin'), PHP_URL_PATH); $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()) { $pipe->pipe($path, $app->make('Flarum\Http\Middleware\ParseJsonBody')); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession')); @@ -41,12 +46,10 @@ class Server extends AbstractServer event(new ConfigureMiddleware($pipe, $path, $this)); $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 { $app->register('Flarum\Update\UpdateServiceProvider'); $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)); } } diff --git a/framework/core/src/Api/Middleware/HandleErrors.php b/framework/core/src/Api/Middleware/HandleErrors.php index 58e257d6f..b650d06a5 100644 --- a/framework/core/src/Api/Middleware/HandleErrors.php +++ b/framework/core/src/Api/Middleware/HandleErrors.php @@ -11,12 +11,12 @@ namespace Flarum\Api\Middleware; +use Exception; use Flarum\Api\ErrorHandler; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; -use Zend\Stratigility\ErrorMiddlewareInterface; -class HandleErrors implements ErrorMiddlewareInterface +class HandleErrors { /** * @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); + } } } diff --git a/framework/core/src/Api/Server.php b/framework/core/src/Api/Server.php index 59c3090f0..79dbb03b6 100644 --- a/framework/core/src/Api/Server.php +++ b/framework/core/src/Api/Server.php @@ -25,10 +25,13 @@ class Server extends AbstractServer protected function getMiddleware(Application $app) { $pipe = new MiddlewarePipe; + $pipe->raiseThrowables(); $path = parse_url($app->url('api'), PHP_URL_PATH); 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\Api\Middleware\FakeHttpMethods')); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession')); @@ -40,7 +43,6 @@ class Server extends AbstractServer 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\Api\Middleware\HandleErrors')); } else { $pipe->pipe($path, function () { $document = new Document; diff --git a/framework/core/src/Forum/Server.php b/framework/core/src/Forum/Server.php index ab56e6372..893ddc52c 100644 --- a/framework/core/src/Forum/Server.php +++ b/framework/core/src/Forum/Server.php @@ -26,6 +26,7 @@ class Server extends AbstractServer protected function getMiddleware(Application $app) { $pipe = new MiddlewarePipe; + $pipe->raiseThrowables(); $path = parse_url($app->url(), PHP_URL_PATH); $errorDir = __DIR__.'/../../error'; @@ -33,10 +34,13 @@ class Server extends AbstractServer if (! $app->isInstalled()) { $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\DispatchRoute', ['routes' => $app->make('flarum.install.routes')])); - $pipe->pipe($path, new HandleErrors($errorDir, $app->make('log'), true)); } 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\StartSession')); $pipe->pipe($path, $app->make('Flarum\Http\Middleware\RememberFromCookie')); @@ -46,7 +50,6 @@ class Server extends AbstractServer event(new ConfigureMiddleware($pipe, $path, $this)); $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 { $pipe->pipe($path, function () use ($errorDir) { return new HtmlResponse(file_get_contents($errorDir.'/503.html', 503)); diff --git a/framework/core/src/Http/FullStackServer.php b/framework/core/src/Http/FullStackServer.php index 8da323ab2..70b331aba 100644 --- a/framework/core/src/Http/FullStackServer.php +++ b/framework/core/src/Http/FullStackServer.php @@ -26,6 +26,7 @@ class FullStackServer extends AbstractServer protected function getMiddleware(Application $app) { $pipe = new MiddlewarePipe; + $pipe->raiseThrowables(); $pipe->pipe(new ApiServer); $pipe->pipe(new AdminServer); diff --git a/framework/core/src/Http/Middleware/HandleErrors.php b/framework/core/src/Http/Middleware/HandleErrors.php index 30e6135d2..0c8897d9d 100644 --- a/framework/core/src/Http/Middleware/HandleErrors.php +++ b/framework/core/src/Http/Middleware/HandleErrors.php @@ -11,14 +11,14 @@ namespace Flarum\Http\Middleware; +use Exception; use Franzl\Middleware\Whoops\ErrorMiddleware as WhoopsMiddleware; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Log\LoggerInterface; use Zend\Diactoros\Response\HtmlResponse; -use Zend\Stratigility\ErrorMiddlewareInterface; -class HandleErrors implements ErrorMiddlewareInterface +class HandleErrors { /** * @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; $errorCode = $error->getCode(); From 8cc04cfaec88a4caadda86895f9b1ca24a7410b1 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 3 Feb 2017 21:53:20 +0100 Subject: [PATCH 2/3] Require PHP 5.6 --- framework/core/.travis.yml | 1 - framework/core/composer.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/framework/core/.travis.yml b/framework/core/.travis.yml index eaf4af6fd..c00e19d52 100644 --- a/framework/core/.travis.yml +++ b/framework/core/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.5 - 5.6 - 7.0 - hhvm diff --git a/framework/core/composer.json b/framework/core/composer.json index 42dc78a11..efdb49182 100644 --- a/framework/core/composer.json +++ b/framework/core/composer.json @@ -20,7 +20,7 @@ "docs": "http://flarum.org/docs" }, "require": { - "php": ">=5.5.9", + "php": ">=5.6.0", "dflydev/fig-cookies": "^1.0.2", "doctrine/dbal": "^2.5", "components/font-awesome": "^4.6", From 6b30a37b954972b83e76a8d71f220103c5dbc546 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 3 Feb 2017 21:53:39 +0100 Subject: [PATCH 3/3] Travis: Run tests on PHP 7.1 as well --- framework/core/.travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/core/.travis.yml b/framework/core/.travis.yml index c00e19d52..2226ea1a2 100644 --- a/framework/core/.travis.yml +++ b/framework/core/.travis.yml @@ -3,6 +3,7 @@ language: php php: - 5.6 - 7.0 + - 7.1 - hhvm matrix: