-
-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add options to control method failure handling in the Router middleware #250
base: 3.x
Are you sure you want to change the base?
Changes from all commits
c954cad
c96eb00
5603689
be42761
7d730d0
303a683
0479e23
bd68624
fdd5572
8c7134c
97b76e3
48fec84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Router; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* `MethodsResponseFactoryInterface` produces a response with a list of the target resource's supported methods. | ||
*/ | ||
interface MethodsResponseFactoryInterface | ||
{ | ||
/** | ||
* Produces a response listing resource's allowed methods. | ||
* | ||
* @param array $methods a list of the HTTP methods supported by the request's resource | ||
*/ | ||
public function create(array $methods, ServerRequestInterface $request): ResponseInterface; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,16 +10,21 @@ | |
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Yiisoft\Http\Header; | ||
use Yiisoft\Http\Method; | ||
use Yiisoft\Http\Status; | ||
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher; | ||
use Yiisoft\Middleware\Dispatcher\MiddlewareFactory; | ||
use Yiisoft\Router\CurrentRoute; | ||
use Yiisoft\Router\MethodsResponseFactoryInterface; | ||
use Yiisoft\Router\UrlMatcherInterface; | ||
|
||
final class Router implements MiddlewareInterface | ||
{ | ||
private MiddlewareDispatcher $dispatcher; | ||
private bool $ignoreMethodFailureHandler = false; | ||
private ?MethodsResponseFactoryInterface $optionsResponseFactory = null; | ||
private ?MethodsResponseFactoryInterface $notAllowedResponseFactory = null; | ||
|
||
public function __construct( | ||
private UrlMatcherInterface $matcher, | ||
|
@@ -37,15 +42,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface | |
|
||
$this->currentRoute->setUri($request->getUri()); | ||
|
||
if ($result->isMethodFailure()) { | ||
if ($request->getMethod() === Method::OPTIONS) { | ||
return $this->responseFactory | ||
->createResponse(Status::NO_CONTENT) | ||
->withHeader('Allow', implode(', ', $result->methods())); | ||
} | ||
return $this->responseFactory | ||
->createResponse(Status::METHOD_NOT_ALLOWED) | ||
->withHeader('Allow', implode(', ', $result->methods())); | ||
if (!$this->ignoreMethodFailureHandler && $result->isMethodFailure()) { | ||
return $request->getMethod() === Method::OPTIONS | ||
? $this->getOptionsResponse($request, $result->methods()) | ||
: $this->getMethodNotAllowedResponse($request, $result->methods()); | ||
} | ||
|
||
if (!$result->isSuccess()) { | ||
|
@@ -58,4 +58,49 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface | |
->withDispatcher($this->dispatcher) | ||
->process($request, $handler); | ||
} | ||
|
||
public function ignoreMethodFailureHandler(): self | ||
{ | ||
$new = clone $this; | ||
$new->ignoreMethodFailureHandler = true; | ||
return $new; | ||
} | ||
|
||
public function withOptionsResponseFactory(MethodsResponseFactoryInterface $optionsResponseFactory): self | ||
{ | ||
$new = clone $this; | ||
$new->optionsResponseFactory = $optionsResponseFactory; | ||
return $new; | ||
} | ||
|
||
public function withNotAllowedResponseFactory(MethodsResponseFactoryInterface $notAllowedResponseFactory): self | ||
{ | ||
$new = clone $this; | ||
$new->notAllowedResponseFactory = $notAllowedResponseFactory; | ||
return $new; | ||
} | ||
|
||
/** | ||
* @param string[] $methods | ||
*/ | ||
private function getOptionsResponse(ServerRequestInterface $request, array $methods): ResponseInterface | ||
{ | ||
return $this->optionsResponseFactory !== null | ||
? $this->optionsResponseFactory->create($methods, $request) | ||
: $this->responseFactory | ||
->createResponse(Status::NO_CONTENT) | ||
->withHeader(Header::ALLOW, implode(', ', $methods)); | ||
Comment on lines
+90
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract it to new response factory class and use it by default for |
||
} | ||
|
||
/** | ||
* @param string[] $methods | ||
*/ | ||
private function getMethodNotAllowedResponse(ServerRequestInterface $request, array $methods): ResponseInterface | ||
{ | ||
return $this->notAllowedResponseFactory !== null | ||
? $this->notAllowedResponseFactory->create($methods, $request) | ||
: $this->responseFactory | ||
->createResponse(Status::METHOD_NOT_ALLOWED) | ||
->withHeader(Header::ALLOW, implode(', ', $methods)); | ||
Comment on lines
+102
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract it to new response factory class and use it by default for |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think some examples or explanation are needed, since it is not clear how exactly it responds. It's documented below but I think it could be moved here somehow.