Skip to content

Commit e05250c

Browse files
author
Fady Michel
committed
add middleware psr15
1 parent 071c6c8 commit e05250c

File tree

4 files changed

+11
-22
lines changed

4 files changed

+11
-22
lines changed

src/Route.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Route
4444
* ]
4545
* @param array $methods
4646
*/
47-
public function __construct(string $name, string $path, array $parameters, array $methods = ['GET', 'POST'])
47+
public function __construct(string $name, string $path, array $parameters, array $methods = ['GET'])
4848
{
4949
if ($methods === []) {
5050
throw new \InvalidArgumentException('HTTP methods argument was empty; must contain at least one method');

src/Router.php

+1-12
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,11 @@ public function add(Route $route): self
3737
return $this;
3838
}
3939

40-
/**
41-
* @param ServerRequestInterface $serverRequest
42-
* @return Route
43-
* @throws \Exception
44-
*/
4540
public function match(ServerRequestInterface $serverRequest): Route
4641
{
4742
return $this->matchFromPath($serverRequest->getUri()->getPath(), $serverRequest->getMethod());
4843
}
4944

50-
/**
51-
* @param string $path
52-
* @param string $method
53-
* @return Route
54-
* @throws \Exception
55-
*/
5645
public function matchFromPath(string $path, string $method): Route
5746
{
5847
foreach ($this->routes as $route) {
@@ -62,7 +51,7 @@ public function matchFromPath(string $path, string $method): Route
6251
return $route;
6352
}
6453

65-
throw new RouteNotFound('No route found for '.$method, self::NO_ROUTE);
54+
throw new RouteNotFound('No route found for ' . $method, self::NO_ROUTE);
6655
}
6756

6857
public function generateUri(string $name, array $parameters = []): string

src/RouterInterface.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DevCoder;
44

5+
use DevCoder\Exception\RouteNotFound;
56
use Psr\Http\Message\ServerRequestInterface;
67

78
/**
@@ -13,23 +14,23 @@ interface RouterInterface
1314
/**
1415
* @param ServerRequestInterface $serverRequest
1516
* @return Route
16-
* @throws \Exception if no found route.
17+
* @throws RouteNotFound if no found route.
1718
*/
1819
public function match(ServerRequestInterface $serverRequest) : Route;
1920

2021
/**
2122
* @param string $path
2223
* @param string $method
2324
* @return Route
24-
* @throws \Exception if no found route.
25+
* @throws RouteNotFound if no found route.
2526
*/
2627
public function matchFromPath(string $path, string $method) : Route;
2728

2829
/**
2930
* @param string $name
3031
* @param array $parameters
3132
* @return string
32-
* @throws \Exception if unable to generate the given URI.
33+
* @throws \InvalidArgumentException if unable to generate the given URI.
3334
*/
3435
public function generateUri(string $name, array $parameters = []) : string;
3536
}

src/RouterMiddleware.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DevCoder;
44

5+
use DevCoder\Exception\RouteNotFound;
56
use Exception;
67
use Psr\Http\Message\ResponseFactoryInterface;
78
use Psr\Http\Message\ResponseInterface;
@@ -23,16 +24,12 @@ final class RouterMiddleware implements MiddlewareInterface
2324
* @var RouterInterface
2425
*/
2526
private $router;
27+
2628
/**
2729
* @var ResponseFactoryInterface
2830
*/
2931
private $responseFactory;
3032

31-
/**
32-
* RouterMiddleware constructor.
33-
* @param RouterInterface $router
34-
* @param ResponseFactoryInterface $responseFactory
35-
*/
3633
public function __construct(RouterInterface $router, ResponseFactoryInterface $responseFactory)
3734
{
3835
$this->router = $router;
@@ -54,8 +51,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
5451
$request = $request->withAttribute($key, $value);
5552
}
5653

57-
} catch (Exception $exception) {
54+
} catch (RouteNotFound $e) {
5855
return $this->responseFactory->createResponse(404);
56+
} catch (\Throwable $e) {
57+
throw $e;
5958
}
6059

6160
return $handler->handle($request);

0 commit comments

Comments
 (0)