Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
major refactoring of tests and code + remove some features + add bett…
…er features (#1) * major refactoring of tests and code + remove some features + add better features * fix code quality * Router::pathTo handles custom parameters * allow pathTo to append extra parameters as the path query * improve tests structure and readability * almost completed the documentation * Indicate data providers as annotations * Improve exceptions * improve project structure * continue updating the README * Allow conditions to return true instead of an array * Update documentation * Add documentation about the exceptions
- Loading branch information
1 parent
d9d7a5f
commit 16c9bcc
Showing
42 changed files
with
1,822 additions
and
965 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
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\Condition; | ||
|
||
use IngeniozIT\Router\RouterException; | ||
|
||
interface ConditionException extends RouterException | ||
{ | ||
} |
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\Condition; | ||
|
||
use Closure; | ||
use IngeniozIT\Router\Condition\Exception\InvalidConditionHandler; | ||
use IngeniozIT\Router\Condition\Exception\InvalidConditionResponse; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
use function is_array; | ||
use function is_bool; | ||
use function is_callable; | ||
use function is_string; | ||
|
||
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 InvalidConditionHandler($handler); | ||
} | ||
|
||
$this->handler = $handler(...); | ||
} | ||
|
||
/** | ||
* @return array<string, mixed>|false | ||
*/ | ||
public function handle(ServerRequestInterface $request): array|false | ||
{ | ||
$result = ($this->handler)($request); | ||
|
||
if (!is_bool($result) && !is_array($result)) { | ||
throw new InvalidConditionResponse($result); | ||
} | ||
|
||
return $result === true ? [] : $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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Condition\Exception; | ||
|
||
use IngeniozIT\Router\Condition\ConditionException; | ||
use InvalidArgumentException; | ||
|
||
use function get_debug_type; | ||
|
||
final class InvalidConditionHandler extends InvalidArgumentException implements ConditionException | ||
{ | ||
public function __construct(public mixed $handler) | ||
{ | ||
parent::__construct('Condition handler must be a callable, ' . get_debug_type($handler) . ' given.'); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Condition\Exception; | ||
|
||
use IngeniozIT\Router\Condition\ConditionException; | ||
use InvalidArgumentException; | ||
|
||
use function get_debug_type; | ||
|
||
final class InvalidConditionResponse extends InvalidArgumentException implements ConditionException | ||
{ | ||
public function __construct(public mixed $response) | ||
{ | ||
parent::__construct( | ||
'Condition must either return an array or a boolean, ' . get_debug_type($response) . ' given.' | ||
); | ||
} | ||
} |
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.
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Middleware\Exception; | ||
|
||
use IngeniozIT\Router\Middleware\MiddlewareException; | ||
use InvalidArgumentException; | ||
|
||
use function get_debug_type; | ||
|
||
final class InvalidMiddlewareHandler extends InvalidArgumentException implements MiddlewareException | ||
{ | ||
public function __construct(public mixed $handler) | ||
{ | ||
parent::__construct( | ||
'Middleware handler must be a PSR Middleware or a callable, ' . get_debug_type($handler) . ' given.' | ||
); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Middleware\Exception; | ||
|
||
use IngeniozIT\Router\Middleware\MiddlewareException; | ||
use InvalidArgumentException; | ||
|
||
use function get_debug_type; | ||
|
||
final class InvalidMiddlewareResponse extends InvalidArgumentException implements MiddlewareException | ||
{ | ||
public function __construct(public mixed $response) | ||
{ | ||
parent::__construct('Middleware must return a PSR Response, ' . get_debug_type($response) . ' given.'); | ||
} | ||
} |
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\Middleware; | ||
|
||
use IngeniozIT\Router\RouterException; | ||
|
||
interface MiddlewareException extends RouterException | ||
{ | ||
} |
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IngeniozIT\Router\Middleware; | ||
|
||
use Closure; | ||
use IngeniozIT\Router\Middleware\Exception\InvalidMiddlewareHandler; | ||
use IngeniozIT\Router\Middleware\Exception\InvalidMiddlewareResponse; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
use function is_callable; | ||
use function is_string; | ||
|
||
readonly final class MiddlewareHandler | ||
{ | ||
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 InvalidMiddlewareHandler($handler); | ||
} | ||
|
||
public function handle(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$result = ($this->handler)($request, $handler); | ||
|
||
if (!$result instanceof ResponseInterface) { | ||
throw new InvalidMiddlewareResponse($result); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
Oops, something went wrong.