This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/result-as-middleware' into release-3.0.0
Close #48
- Loading branch information
Showing
12 changed files
with
296 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-expressive-router for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com) | ||
* @license https://github.com/zendframework/zend-expressive-router/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zend\Expressive\Router; | ||
|
||
use Fig\Http\Message\StatusCodeInterface as StatusCode; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
/** | ||
* Emit a 405 Method Not Allowed response | ||
* | ||
* If the request composes a route result, and the route result represents a | ||
* failure due to request method, this middleware will emit a 405 response, | ||
* along with an Allow header indicating allowed methods, as reported by the | ||
* route result. | ||
* | ||
* If no route result is composed, and/or it's not the result of a method | ||
* failure, it passes handling to the provided handler. | ||
*/ | ||
class MethodNotAllowedMiddleware implements MiddlewareInterface | ||
{ | ||
/** | ||
* Response prototype for 405 responses. | ||
* | ||
* @var ResponseInterface | ||
*/ | ||
private $responsePrototype; | ||
|
||
public function __construct(ResponseInterface $responsePrototype) | ||
{ | ||
$this->responsePrototype = $responsePrototype; | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface | ||
{ | ||
$routeResult = $request->getAttribute(RouteResult::class); | ||
if (! $routeResult || ! $routeResult->isMethodFailure()) { | ||
return $handler->handle($request); | ||
} | ||
|
||
return $this->responsePrototype | ||
->withStatus(StatusCode::STATUS_METHOD_NOT_ALLOWED) | ||
->withHeader('Allow', implode(',', $routeResult->getAllowedMethods())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.