Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/MethodFailureHandler/MethodFailureHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\MethodFailureHandler;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

interface MethodFailureHandlerInterface
{
/**
* @param string[] $allowedMethods
*/
public function handle(ServerRequestInterface $request, array $allowedMethods): ResponseInterface;
}
55 changes: 55 additions & 0 deletions src/MethodFailureHandler/StandardMethodFailureHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\MethodFailureHandler;

use InvalidArgumentException;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Http\Header;
use Yiisoft\Http\Method;
use Yiisoft\Http\Status;

final class StandardMethodFailureHandler implements MethodFailureHandlerInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final class StandardMethodFailureHandler implements MethodFailureHandlerInterface
final class DefaultMethodFailureHandler implements MethodFailureHandlerInterface

{
public function __construct(
private readonly ResponseFactoryInterface $responseFactory,
) {
}

/**
* @param string[] $allowedMethods
*/
public function handle(ServerRequestInterface $request, array $allowedMethods): ResponseInterface
{
if (empty($allowedMethods)) {
throw new InvalidArgumentException("Allowed methods can't be empty array.");
}

return $request->getMethod() === Method::OPTIONS
? $this->createAllowedMethodsResponse($allowedMethods)
: $this->createMethodNotAllowedResponse($allowedMethods);
}

/**
* @param string[] $allowedMethods
*/
private function createAllowedMethodsResponse(array $allowedMethods): ResponseInterface
{
return $this->responseFactory
->createResponse(Status::NO_CONTENT)
->withHeader(Header::ALLOW, implode(', ', $allowedMethods));
}

/**
* @param string[] $allowedMethods
*/
private function createMethodNotAllowedResponse(array $allowedMethods): ResponseInterface
{
return $this->responseFactory
->createResponse(Status::METHOD_NOT_ALLOWED)
->withHeader(Header::ALLOW, implode(', ', $allowedMethods));
}
}
24 changes: 11 additions & 13 deletions src/Middleware/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,30 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
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\MethodFailureHandler\StandardMethodFailureHandler;
use Yiisoft\Router\MethodFailureHandler\MethodFailureHandlerInterface;
use Yiisoft\Router\UrlMatcherInterface;

final class Router implements MiddlewareInterface
{
private readonly MiddlewareDispatcher $dispatcher;
private readonly MethodFailureHandlerInterface|null $methodFailureHandler;

public function __construct(
private readonly UrlMatcherInterface $matcher,
private readonly ResponseFactoryInterface $responseFactory,
ResponseFactoryInterface $responseFactory,
MiddlewareFactory $middlewareFactory,
private readonly CurrentRoute $currentRoute,
?EventDispatcherInterface $eventDispatcher = null
?EventDispatcherInterface $eventDispatcher = null,
MethodFailureHandlerInterface|false|null $methodFailureHandler = null,
) {
$this->dispatcher = new MiddlewareDispatcher($middlewareFactory, $eventDispatcher);
$this->methodFailureHandler = $methodFailureHandler === false
? null
: $methodFailureHandler ?? new StandardMethodFailureHandler($responseFactory);
Comment on lines +34 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. What's the goal of not having any handler?
  2. Ternary condition here is hardly readable. Better to split it.

}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
Expand All @@ -37,15 +42,8 @@ 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 ($result->isMethodFailure() && $this->methodFailureHandler !== null) {
return $this->methodFailureHandler->handle($request, $result->methods());
}

if (!$result->isSuccess()) {
Expand Down
Loading