Skip to content

Commit

Permalink
improve routed
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Feb 4, 2025
1 parent 026cf08 commit a43bd84
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
6 changes: 4 additions & 2 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ public function dispatch(ServerRequestInterface $request): DispatchInterface
(string) message(
'No route found for `%uri%`',
uri: $uri,
)
),
404
),
GroupCountBased::METHOD_NOT_ALLOWED => throw new MethodNotAllowedException(
(string) message(
'Method `%method%` is not in the list of allowed methods: `%allowed%`',
method: $method,
allowed: implode(', ', $format[1]),
)
),
405
),
// @codeCoverageIgnoreStart
default => throw new LogicException(
Expand Down
17 changes: 5 additions & 12 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ function route(
'Unknown HTTP method `%provided%` provided for `%controller%` controller.',
provided: $httpMethod,
controller: $controllerName->__toString(),
)
),
405
);
}
/** @var MethodInterface $object */
Expand Down Expand Up @@ -236,6 +237,8 @@ function controllerName(BindInterface|string $item): ControllerNameInterface
* Executes the request on router.
*
* @param array<string, mixed> $container Service container (name => instance).
*
* @throws NotFoundException|MethodNotAllowedException
*/
function routed(
ServerRequestInterface $serverRequest,
Expand All @@ -244,17 +247,7 @@ function routed(
array $container = [],
): RoutedInterface {
$container['responseFactory'] = $responseFactory;

try {
$routed = $router->dispatcher()->dispatch($serverRequest);
} catch (NotFoundException|MethodNotAllowedException $e) {
$code = $e instanceof MethodNotAllowedException ? 405 : 404;

return (new Routed(
$responseFactory->createResponse($code),
bind(NullController::class),
))->withThrowable($e);
}
$routed = $router->dispatcher()->dispatch($serverRequest);
$queue = [];
$middlewares = $routed->bind()->middlewares();
foreach ($middlewares as $middlewareName) {
Expand Down
13 changes: 4 additions & 9 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Chevere\Tests;

use Chevere\Http\Controllers\NullController;
use Chevere\Http\Exceptions\MethodNotAllowedException;
use Chevere\Router\Exceptions\NotFoundException;
use Chevere\Router\Exceptions\VariableInvalidException;
Expand Down Expand Up @@ -245,13 +244,9 @@ public function testRoutedNull(string $method, string $path, int $code, string $
route('/foo', PATCH: ControllerNoParameters::class)
)
);
$routed = routed($request, $router);
$this->assertSame($code, $routed->response()->getStatusCode());
$this->assertInstanceOf($exception, $routed->throwable());
$this->assertSame($reason, $routed->throwable()->getMessage());
$this->assertSame(
NullController::class,
$routed->bind()->controllerName()->__toString()
);
$this->expectException($exception);
$this->expectExceptionMessage($reason);
$this->expectExceptionCode($code);
routed($request, $router);
}
}

0 comments on commit a43bd84

Please sign in to comment.