From dfef23e50cbf94eef5e4e5199837849f58c6bc78 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 25 Sep 2015 00:31:31 +0200 Subject: [PATCH] Implement middleware for faking HTTP methods Refs #502. --- .../src/Api/Middleware/FakeHttpMethods.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 framework/core/src/Api/Middleware/FakeHttpMethods.php diff --git a/framework/core/src/Api/Middleware/FakeHttpMethods.php b/framework/core/src/Api/Middleware/FakeHttpMethods.php new file mode 100644 index 000000000..faae246e7 --- /dev/null +++ b/framework/core/src/Api/Middleware/FakeHttpMethods.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Api\Middleware; + +use Psr\Http\Message\ResponseInterface as Response; +use Psr\Http\Message\ServerRequestInterface as Request; +use Zend\Stratigility\MiddlewareInterface; + +class FakeHttpMethods implements MiddlewareInterface +{ + /** + * {@inheritdoc} + */ + public function __invoke(Request $request, Response $response, callable $out = null) + { + if ($request->getMethod() === 'POST' && $request->hasHeader('x-http-method')) { + $fakeMethod = $request->getHeaderLine('x-http-method'); + + $request = $request->withMethod(strtoupper($fakeMethod)); + } + + return $out ? $out($request, $response) : $response; + } +}