Skip to content

Commit

Permalink
improve project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
IngeniozIT committed Apr 8, 2024
1 parent 32d4333 commit cca6e93
Show file tree
Hide file tree
Showing 22 changed files with 109 additions and 48 deletions.
6 changes: 6 additions & 0 deletions src/Condition/ConditionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
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_callable;
use function is_string;

readonly final class ConditionHandler
{
private Closure $handler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace IngeniozIT\Router\Condition;
namespace IngeniozIT\Router\Condition\Exception;

use IngeniozIT\Router\Condition\ConditionException;
use InvalidArgumentException;

final class InvalidConditionHandler extends InvalidArgumentException implements ConditionException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

declare(strict_types=1);

namespace IngeniozIT\Router\Condition;
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)
Expand Down
20 changes: 20 additions & 0 deletions src/Middleware/Exception/InvalidMiddlewareHandler.php
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.'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

declare(strict_types=1);

namespace IngeniozIT\Router\Middleware;
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)
Expand Down
15 changes: 0 additions & 15 deletions src/Middleware/InvalidMiddlewareHandler.php

This file was deleted.

5 changes: 5 additions & 0 deletions src/Middleware/MiddlewareHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
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;
Expand Down
3 changes: 3 additions & 0 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use IngeniozIT\Router\Route\RouteElement;

use function array_reduce;
use function strtoupper;

final readonly class Route
{
public const GET = 0b0000001;
Expand Down
22 changes: 22 additions & 0 deletions src/Route/Exception/InvalidRouteHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace IngeniozIT\Router\Route\Exception;

use IngeniozIT\Router\Route\RouteException;
use InvalidArgumentException;

use function get_debug_type;

final class InvalidRouteHandler extends InvalidArgumentException implements RouteException
{
public function __construct(public mixed $handler)
{
parent::__construct(
'Route handler must be a PSR Middleware, a PSR RequestHandler or a callable, ' . get_debug_type(
$handler
) . ' given.'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace IngeniozIT\Router\Route;
namespace IngeniozIT\Router\Route\Exception;

use UnexpectedValueException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

declare(strict_types=1);

namespace IngeniozIT\Router\Route;
namespace IngeniozIT\Router\Route\Exception;

use IngeniozIT\Router\Route\RouteException;
use InvalidArgumentException;

use function get_debug_type;

final class InvalidRouteResponse extends InvalidArgumentException implements RouteException
{
public function __construct(public mixed $response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

declare(strict_types=1);

namespace IngeniozIT\Router\Route;
namespace IngeniozIT\Router\Route\Exception;

use DomainException;
use IngeniozIT\Router\Route\RouteException;

use function implode;

final class MissingRouteParameters extends DomainException implements RouteException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

declare(strict_types=1);

namespace IngeniozIT\Router\Route;
namespace IngeniozIT\Router\Route\Exception;

use DomainException;
use IngeniozIT\Router\Route\RouteException;

final class RouteNotFound extends DomainException implements RouteException
{
Expand Down
15 changes: 0 additions & 15 deletions src/Route/InvalidRouteHandler.php

This file was deleted.

16 changes: 15 additions & 1 deletion src/Route/RouteElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@
namespace IngeniozIT\Router\Route;

use IngeniozIT\Router\Route;
use IngeniozIT\Router\Route\Exception\InvalidRouteParameter;
use IngeniozIT\Router\Route\Exception\MissingRouteParameters;
use Psr\Http\Message\ServerRequestInterface;

use function array_diff;
use function array_filter;
use function array_intersect;
use function array_keys;
use function count;
use function http_build_query;
use function in_array;
use function preg_match;
use function preg_match_all;
use function str_contains;
use function str_replace;

final readonly class RouteElement
{
public string $path;
Expand Down Expand Up @@ -147,7 +161,7 @@ private function validatePathParameters(array $parameters): void
}

foreach ($this->parameters as $parameter) {
if (!preg_match('#^' . $this->parameterPattern($parameter) . '$#', (string) $parameters[$parameter])) {
if (!preg_match('#^' . $this->parameterPattern($parameter) . '$#', (string)$parameters[$parameter])) {
throw new InvalidRouteParameter($this->name ?? '', $parameter, $this->parameterPattern($parameter));
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Route/RouteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
namespace IngeniozIT\Router\Route;

use Closure;
use IngeniozIT\Router\Route\Exception\InvalidRouteHandler;
use IngeniozIT\Router\Route\Exception\InvalidRouteResponse;
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 RouteHandler
{
private Closure|MiddlewareInterface|RequestHandlerInterface $handler;
Expand Down
2 changes: 2 additions & 0 deletions src/RouteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use IngeniozIT\Router\Route\RouteElement;

use function array_map;

final class RouteGroup
{
/** @var RouteElement[]|RouteGroup[] */
Expand Down
2 changes: 1 addition & 1 deletion src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use IngeniozIT\Router\Condition\ConditionHandler;
use IngeniozIT\Router\Middleware\MiddlewareHandler;
use IngeniozIT\Router\Route\Exception\RouteNotFound;
use IngeniozIT\Router\Route\RouteElement;
use IngeniozIT\Router\Route\RouteHandler;
use IngeniozIT\Router\Route\RouteNotFound;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
use Psr\Http\Server\RequestHandlerInterface;
Expand Down
4 changes: 2 additions & 2 deletions tests/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Closure;
use IngeniozIT\Http\Message\UriFactory;
use IngeniozIT\Router\Route;
use IngeniozIT\Router\Route\InvalidRouteHandler;
use IngeniozIT\Router\Route\InvalidRouteResponse;
use IngeniozIT\Router\Route\Exception\InvalidRouteHandler;
use IngeniozIT\Router\Route\Exception\InvalidRouteResponse;
use IngeniozIT\Router\RouteGroup;
use IngeniozIT\Router\Tests\Utils\RouterCase;
use IngeniozIT\Router\Tests\Utils\TestHandler;
Expand Down
4 changes: 2 additions & 2 deletions tests/ConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Closure;
use IngeniozIT\Http\Message\UriFactory;
use IngeniozIT\Router\Condition\InvalidConditionHandler;
use IngeniozIT\Router\Condition\InvalidConditionResponse;
use IngeniozIT\Router\Condition\Exception\InvalidConditionHandler;
use IngeniozIT\Router\Condition\Exception\InvalidConditionResponse;
use IngeniozIT\Router\Route;
use IngeniozIT\Router\RouteGroup;
use IngeniozIT\Router\Tests\Utils\RouterCase;
Expand Down
4 changes: 2 additions & 2 deletions tests/MiddlewaresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Exception;
use IngeniozIT\Http\Message\UriFactory;
use IngeniozIT\Router\Middleware\InvalidMiddlewareHandler;
use IngeniozIT\Router\Middleware\InvalidMiddlewareResponse;
use IngeniozIT\Router\Middleware\Exception\InvalidMiddlewareHandler;
use IngeniozIT\Router\Middleware\Exception\InvalidMiddlewareResponse;
use IngeniozIT\Router\Route;
use IngeniozIT\Router\RouteGroup;
use IngeniozIT\Router\Tests\Utils\RouterCase;
Expand Down
6 changes: 3 additions & 3 deletions tests/NameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace IngeniozIT\Router\Tests;

use IngeniozIT\Router\Route;
use IngeniozIT\Router\Route\InvalidRouteParameter;
use IngeniozIT\Router\Route\MissingRouteParameters;
use IngeniozIT\Router\Route\RouteNotFound;
use IngeniozIT\Router\Route\Exception\InvalidRouteParameter;
use IngeniozIT\Router\Route\Exception\MissingRouteParameters;
use IngeniozIT\Router\Route\Exception\RouteNotFound;
use IngeniozIT\Router\RouteGroup;
use IngeniozIT\Router\Tests\Utils\RouterCase;

Expand Down

0 comments on commit cca6e93

Please sign in to comment.