From cca97398aedab545f80472cdeccd6fec07d72ea4 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 26 Jun 2015 22:34:34 +0200 Subject: [PATCH] Upgrade Zend Diactoros to 1.1 This gives us a bunch of handy helper classes for empty responses, redirects, HTML and JSON content types. Closes flarum/core#153 --- framework/core/composer.json | 1 + framework/core/composer.lock | 90 ++++++++++++++----- .../core/src/Api/Actions/DeleteAction.php | 4 +- .../core/src/Api/Actions/ForgotAction.php | 4 +- .../core/src/Api/Actions/JsonApiAction.php | 22 +---- .../core/src/Api/Actions/TokenAction.php | 5 +- .../core/src/Api/Middleware/JsonApiErrors.php | 4 +- .../core/src/Forum/Actions/LoginAction.php | 4 +- .../src/Forum/Middleware/HandleErrors.php | 4 +- framework/core/src/Support/Action.php | 7 +- 10 files changed, 92 insertions(+), 53 deletions(-) diff --git a/framework/core/composer.json b/framework/core/composer.json index dccd78c75..92e74a4a0 100644 --- a/framework/core/composer.json +++ b/framework/core/composer.json @@ -17,6 +17,7 @@ "intervention/image": "dev-master", "ezyang/htmlpurifier": "dev-master", "psr/http-message": "^1.0@dev", + "zendframework/zend-diactoros": "^1.1", "nikic/fast-route": "dev-master", "dflydev/fig-cookies": "^1.0" }, diff --git a/framework/core/composer.lock b/framework/core/composer.lock index b4092b001..4f3bba92f 100644 --- a/framework/core/composer.lock +++ b/framework/core/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "57202a56eaacefdff162f426710d1cb2", + "hash": "8ad4b2052460cb12a510ea37eab3b038", "packages": [ { "name": "danielstjules/stringy", @@ -431,7 +431,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Intervention/image/zipball/1009557c32205b077e34b2ae611e6316fa423021", + "url": "https://api.github.com/repos/Intervention/image/zipball/bfc49d8702cafa22de5912b23d82c39d9236933f", "reference": "1009557c32205b077e34b2ae611e6316fa423021", "shasum": "" }, @@ -585,7 +585,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/FastRoute/zipball/ffb3b68a3ab0df7e60cf587d2de4937915670f16", + "url": "https://api.github.com/repos/nikic/FastRoute/zipball/31fa86924556b80735f98b294a7ffdfb26789f22", "reference": "ffb3b68a3ab0df7e60cf587d2de4937915670f16", "shasum": "" }, @@ -736,7 +736,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Translation/zipball/95f9b028555f55b78b465a2488f1a38442acc412", + "url": "https://api.github.com/repos/symfony/Translation/zipball/eda5e27afa889870ad77eb32031d1f27c9deb87f", "reference": "95f9b028555f55b78b465a2488f1a38442acc412", "shasum": "" }, @@ -797,7 +797,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/tobscure/json-api/zipball/d6c82a496289569e8907f3aa980ace407a35b45e", + "url": "https://api.github.com/repos/tobscure/json-api/zipball/cfc22175c1c2fa30871dfc40b4aa7713d4246399", "reference": "d6c82a496289569e8907f3aa980ace407a35b45e", "shasum": "" }, @@ -859,6 +859,56 @@ ], "description": "Powerful, flexible, relational permissions using Eloquent.", "time": "2015-05-29 05:01:56" + }, + { + "name": "zendframework/zend-diactoros", + "version": "dev-develop", + "source": { + "type": "git", + "url": "https://github.com/zendframework/zend-diactoros.git", + "reference": "1aface914fb3a45747a1db001ffaf2830a19ae4c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/zendframework/zend-diactoros/zipball/1aface914fb3a45747a1db001ffaf2830a19ae4c", + "reference": "1aface914fb3a45747a1db001ffaf2830a19ae4c", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "psr/http-message": "~1.0" + }, + "provide": { + "psr/http-message-implementation": "~1.0.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.6", + "squizlabs/php_codesniffer": "^2.3.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev", + "dev-develop": "1.1-dev" + } + }, + "autoload": { + "psr-4": { + "Zend\\Diactoros\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "description": "PSR HTTP Message implementations", + "homepage": "https://github.com/zendframework/zend-diactoros", + "keywords": [ + "http", + "psr", + "psr-7" + ], + "time": "2015-06-25 18:49:21" } ], "packages-dev": [ @@ -872,7 +922,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Codeception/Codeception/zipball/8b3ab1c66653a18fdbe4e11416e46faee4d14f69", + "url": "https://api.github.com/repos/Codeception/Codeception/zipball/180611273eda0c428ab0fd134860d179e252ba4d", "reference": "8b3ab1c66653a18fdbe4e11416e46faee4d14f69", "shasum": "" }, @@ -1656,7 +1706,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/a02682e0fee6392379f6cc1784aa8a7b83bf4551", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/df88fdf48a61af29cc309545f9c8d425c63b8852", "reference": "a02682e0fee6392379f6cc1784aa8a7b83bf4551", "shasum": "" }, @@ -1718,7 +1768,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/a923bb15680d0089e2316f7a4af8f437046e96bb", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/6150bf2c35d3fc379e50c7602b75caceaa39dbf0", "reference": "a923bb15680d0089e2316f7a4af8f437046e96bb", "shasum": "" }, @@ -1850,7 +1900,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/1c7eb0aa0d1ca766cc7177b1709ed00b4cb352a5", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/7a9b0969488c3c54fd62b4d504b3ec758fd005d9", "reference": "1c7eb0aa0d1ca766cc7177b1709ed00b4cb352a5", "shasum": "" }, @@ -1971,7 +2021,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/9e9609457d9dee6404eb19de95fe1e78d4fd9faa", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/a1304c50e6982bf6d1fa5131ee276bbc6229d80b", "reference": "9e9609457d9dee6404eb19de95fe1e78d4fd9faa", "shasum": "" }, @@ -2070,7 +2120,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/1dd8869519a225f7f2b9eb663e225298fade819e", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/abd05e77d364138c0532a1b78a13c4d78311f546", "reference": "1dd8869519a225f7f2b9eb663e225298fade819e", "shasum": "" }, @@ -2134,7 +2184,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/863df9687835c62aa423a22412d26fa2ebde3fd3", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/6899b3e33bfbd386d88b5eea5f65f563e8793051", "reference": "863df9687835c62aa423a22412d26fa2ebde3fd3", "shasum": "" }, @@ -2186,7 +2236,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/5a8c7d31914337b69923db26c4221b81ff5a196e", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/bc66c3bbe6e5822c34395049e110566491cafa3b", "reference": "5a8c7d31914337b69923db26c4221b81ff5a196e", "shasum": "" }, @@ -2236,7 +2286,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/84839970d05254c73cde183a721c7af13aede943", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/7ae5513327cb536431847bcc0c10edba2701064e", "reference": "84839970d05254c73cde183a721c7af13aede943", "shasum": "" }, @@ -2302,7 +2352,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/1ee0113873e66eb8618381b91bca0265ebb64056", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/23af31f402993cfd94e99cbc4b782e9a78eb0e97", "reference": "1ee0113873e66eb8618381b91bca0265ebb64056", "shasum": "" }, @@ -2353,7 +2403,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/7cd333d1e240458cd5963a6b6ba6500bb1d26212", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/994d4a811bafe801fb06dccbee797863ba2792ba", "reference": "7cd333d1e240458cd5963a6b6ba6500bb1d26212", "shasum": "" }, @@ -2570,7 +2620,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Console/zipball/caba94cb0b1b9c77aa6870040ef6b1b66f588f6d", + "url": "https://api.github.com/repos/symfony/Console/zipball/2a909e48c528c7c9b22caff6ff622c6c69032bfc", "reference": "caba94cb0b1b9c77aa6870040ef6b1b66f588f6d", "shasum": "" }, @@ -2733,7 +2783,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/d91fb729673cc8587d0471bab5be02c77d608e77", + "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/d7246885b7fe4cb5a2786bda34362d2f0e40b730", "reference": "d91fb729673cc8587d0471bab5be02c77d608e77", "shasum": "" }, @@ -2791,7 +2841,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Finder/zipball/798e9a9c69f3e2e9d8766e8633e404676a09f99c", + "url": "https://api.github.com/repos/symfony/Finder/zipball/588ded81facfb6462c43e6922509e3f8c889de05", "reference": "798e9a9c69f3e2e9d8766e8633e404676a09f99c", "shasum": "" }, @@ -2840,7 +2890,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/709857e12da0f00ff2a15b3b54b75f827d591029", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/f248a72777f3fec2bcafdce3ccd94086250448e1", "reference": "709857e12da0f00ff2a15b3b54b75f827d591029", "shasum": "" }, diff --git a/framework/core/src/Api/Actions/DeleteAction.php b/framework/core/src/Api/Actions/DeleteAction.php index c4eb7e2bb..a09456966 100644 --- a/framework/core/src/Api/Actions/DeleteAction.php +++ b/framework/core/src/Api/Actions/DeleteAction.php @@ -1,7 +1,7 @@ delete($request); - return new Response('php://memory', 204); + return new EmptyResponse(204); } /** diff --git a/framework/core/src/Api/Actions/ForgotAction.php b/framework/core/src/Api/Actions/ForgotAction.php index cf1edbdcf..d51914673 100644 --- a/framework/core/src/Api/Actions/ForgotAction.php +++ b/framework/core/src/Api/Actions/ForgotAction.php @@ -1,10 +1,10 @@ json(); + return new EmptyResponse(); } } diff --git a/framework/core/src/Api/Actions/JsonApiAction.php b/framework/core/src/Api/Actions/JsonApiAction.php index cfa1abe50..589695feb 100644 --- a/framework/core/src/Api/Actions/JsonApiAction.php +++ b/framework/core/src/Api/Actions/JsonApiAction.php @@ -4,7 +4,7 @@ use Flarum\Api\Request; use Illuminate\Database\Eloquent\ModelNotFoundException; use Flarum\Core\Exceptions\ValidationFailureException; use Flarum\Core\Exceptions\PermissionDeniedException; -use Zend\Diactoros\Response; +use Zend\Diactoros\Response\JsonResponse; abstract class JsonApiAction implements ActionInterface { @@ -27,28 +27,14 @@ abstract class JsonApiAction implements ActionInterface 'path' => $field ]; } - return $this->json(['errors' => $errors], 422); + return new JsonResponse(['errors' => $errors], 422); } catch (PermissionDeniedException $e) { - return $this->json(null, 401); + return new JsonResponse(null, 401); } catch (ModelNotFoundException $e) { - return $this->json(null, 404); + return new JsonResponse(null, 404); } } - protected function json($data = null, $status = 200) - { - if ($data === null) { - $data = new \ArrayObject(); - } - - $data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); - - $response = new Response('php://memory', $status); - $response->getBody()->write($data); - - return $response; - } - /** * Handle an API request and return an API response. * diff --git a/framework/core/src/Api/Actions/TokenAction.php b/framework/core/src/Api/Actions/TokenAction.php index a937029d5..99e8c7a84 100644 --- a/framework/core/src/Api/Actions/TokenAction.php +++ b/framework/core/src/Api/Actions/TokenAction.php @@ -6,6 +6,7 @@ use Flarum\Core\Repositories\UserRepositoryInterface; use Flarum\Core\Exceptions\PermissionDeniedException; use Flarum\Core\Events\UserEmailChangeWasRequested; use Illuminate\Contracts\Bus\Dispatcher; +use Zend\Diactoros\Response\JsonResponse; class TokenAction extends JsonApiAction { @@ -39,7 +40,7 @@ class TokenAction extends JsonApiAction if (! $user->is_activated) { event(new UserEmailChangeWasRequested($user, $user->email)); - return $this->json([ + return new JsonResponse([ 'code' => 'confirm_email', 'email' => $user->email ], 401); @@ -49,7 +50,7 @@ class TokenAction extends JsonApiAction new GenerateAccessTokenCommand($user->id) ); - return $this->json([ + return new JsonResponse([ 'token' => $token->id, 'userId' => $user->id ]); diff --git a/framework/core/src/Api/Middleware/JsonApiErrors.php b/framework/core/src/Api/Middleware/JsonApiErrors.php index 8345b878e..769ad1c43 100644 --- a/framework/core/src/Api/Middleware/JsonApiErrors.php +++ b/framework/core/src/Api/Middleware/JsonApiErrors.php @@ -2,7 +2,7 @@ use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; -use Zend\Diactoros\Response\StringResponse; +use Zend\Diactoros\Response\JsonResponse; use Zend\Stratigility\ErrorMiddlewareInterface; class JsonApiErrors implements ErrorMiddlewareInterface @@ -31,6 +31,6 @@ class JsonApiErrors implements ErrorMiddlewareInterface 'errors' => [$errorObject] ]; - return StringResponse::json($data, $status); + return new JsonResponse($data, $status); } } diff --git a/framework/core/src/Forum/Actions/LoginAction.php b/framework/core/src/Forum/Actions/LoginAction.php index 9c29ef06b..a166d2b03 100644 --- a/framework/core/src/Forum/Actions/LoginAction.php +++ b/framework/core/src/Forum/Actions/LoginAction.php @@ -4,7 +4,7 @@ use Flarum\Api\Client; use Flarum\Forum\Events\UserLoggedIn; use Flarum\Core\Repositories\UserRepositoryInterface; use Psr\Http\Message\ServerRequestInterface as Request; -use Zend\Diactoros\Response; +use Zend\Diactoros\Response\EmptyResponse; class LoginAction extends BaseAction { @@ -40,7 +40,7 @@ class LoginAction extends BaseAction $data->token ); } else { - return new Response('php://memory', 401); + return new EmptyResponse(401); } } } diff --git a/framework/core/src/Forum/Middleware/HandleErrors.php b/framework/core/src/Forum/Middleware/HandleErrors.php index 1651685a1..6579d7f7f 100644 --- a/framework/core/src/Forum/Middleware/HandleErrors.php +++ b/framework/core/src/Forum/Middleware/HandleErrors.php @@ -2,7 +2,7 @@ use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; -use Zend\Diactoros\Response\StringResponse; +use Zend\Diactoros\Response\HtmlResponse; use Zend\Stratigility\ErrorMiddlewareInterface; class HandleErrors implements ErrorMiddlewareInterface @@ -30,7 +30,7 @@ class HandleErrors implements ErrorMiddlewareInterface $errorPage = $this->getErrorPage($status); - return StringResponse::html($errorPage, $status); + return new HtmlResponse($errorPage, $status); } protected function getErrorPage($status) diff --git a/framework/core/src/Support/Action.php b/framework/core/src/Support/Action.php index eff29c0f8..669db92f1 100644 --- a/framework/core/src/Support/Action.php +++ b/framework/core/src/Support/Action.php @@ -2,7 +2,8 @@ use Illuminate\Contracts\Bus\Dispatcher; use Psr\Http\Message\ServerRequestInterface as Request; -use Zend\Diactoros\Response; +use Zend\Diactoros\Response\EmptyResponse; +use Zend\Diactoros\Response\RedirectResponse; abstract class Action { @@ -22,7 +23,7 @@ abstract class Action protected function success() { - return new Response(); + return new EmptyResponse(); } /** @@ -44,7 +45,7 @@ abstract class Action ', htmlspecialchars($url, ENT_QUOTES, 'UTF-8')); - $response = new Response('php://memory', 302, ['location' => $url]); + $response = new RedirectResponse($url); $response->getBody()->write($content); return $response;