-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
593c4bb
commit 44476e6
Showing
21 changed files
with
272 additions
and
134 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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Exception; | ||
|
||
use InvalidArgumentException; | ||
|
||
final class InvalidRouteCondition extends InvalidArgumentException | ||
{ | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Exception; | ||
|
||
use InvalidArgumentException; | ||
|
||
final class InvalidRouteHandler extends InvalidArgumentException | ||
{ | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Exception; | ||
|
||
use InvalidArgumentException; | ||
|
||
final class InvalidRouteMiddleware extends InvalidArgumentException | ||
{ | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Exception; | ||
|
||
use DomainException; | ||
|
||
final class RouteNotFound extends DomainException | ||
{ | ||
} |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Handler; | ||
|
||
use Closure; | ||
use IngeniozIT\Router\Exception\InvalidRouteCondition; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
readonly final class ConditionHandler | ||
{ | ||
private Closure $handler; | ||
|
||
public function __construct( | ||
private ContainerInterface $container, | ||
mixed $callback, | ||
) { | ||
$handler = is_string($callback) ? $this->container->get($callback) : $callback; | ||
|
||
if (!is_callable($handler)) { | ||
throw new InvalidRouteCondition('Invalid condition handler'); | ||
} | ||
|
||
$this->handler = $handler(...); | ||
} | ||
|
||
/** | ||
* @return array<string, mixed>|false | ||
*/ | ||
public function handle(ServerRequestInterface $request): array|false | ||
{ | ||
$result = ($this->handler)($request); | ||
|
||
if ($result === false || is_array($result)) { | ||
return $result; | ||
} | ||
|
||
throw new InvalidRouteCondition('Condition must either return an array or false.'); | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Handler; | ||
|
||
use Closure; | ||
use IngeniozIT\Router\Exception\InvalidRouteMiddleware; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
readonly final class MiddlewaresHandler | ||
{ | ||
private Closure $handler; | ||
|
||
public function __construct( | ||
private ContainerInterface $container, | ||
mixed $callback, | ||
) { | ||
$handler = is_string($callback) ? $this->container->get($callback) : $callback; | ||
|
||
if (is_callable($handler)) { | ||
$this->handler = $handler(...); | ||
return; | ||
} | ||
|
||
if ($handler instanceof MiddlewareInterface) { | ||
$this->handler = $handler->process(...); | ||
return; | ||
} | ||
|
||
throw new InvalidRouteMiddleware('Middleware must be a PSR Middleware or a callable.'); | ||
} | ||
|
||
public function handle(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$result = ($this->handler)($request, $handler); | ||
|
||
if (!$result instanceof ResponseInterface) { | ||
throw new InvalidRouteMiddleware('Middleware must return a PSR Response.'); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Handler; | ||
|
||
use Closure; | ||
use IngeniozIT\Router\Exception\InvalidRouteHandler; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
readonly final class RouteHandler | ||
{ | ||
private Closure|MiddlewareInterface|RequestHandlerInterface $handler; | ||
|
||
public function __construct( | ||
private ContainerInterface $container, | ||
mixed $callback, | ||
) { | ||
$handler = is_string($callback) ? $this->container->get($callback) : $callback; | ||
|
||
if ( | ||
!($handler instanceof MiddlewareInterface) | ||
&& !($handler instanceof RequestHandlerInterface) | ||
&& !is_callable($handler) | ||
) { | ||
throw new InvalidRouteHandler('Route handler must be a PSR Middleware, a PSR RequestHandler or a callable.'); | ||
} | ||
|
||
$this->handler = is_callable($handler) ? $handler(...) : $handler; | ||
} | ||
|
||
public function handle(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$result = $this->executeHandler($request, $handler); | ||
|
||
if (!$result instanceof ResponseInterface) { | ||
throw new InvalidRouteHandler('Route handler must return a PSR Response.'); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
private function executeHandler(ServerRequestInterface $request, RequestHandlerInterface $handler): mixed | ||
{ | ||
if ($this->handler instanceof RequestHandlerInterface) { | ||
return $this->handler->handle($request); | ||
} | ||
|
||
if ($this->handler instanceof MiddlewareInterface) { | ||
return $this->handler->process($request, $handler); | ||
} | ||
|
||
return ($this->handler)($request, $handler); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.