Skip to content

Commit

Permalink
feat: Add config file for Router Service
Browse files Browse the repository at this point in the history
Part of #18

And validate it
  • Loading branch information
Gashmob committed May 27, 2024
1 parent eca776b commit d7c710b
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 7 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added config/router.yml
Empty file.
59 changes: 59 additions & 0 deletions include/Config/ConfigurationValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* MIT License
*
* Copyright (c) 2024-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types=1);

namespace Archict\Router\Config;

use Archict\Router\Exception\ErrorHandlerShouldImplementInterfaceException;
use Archict\Router\Exception\HTTPCodeNotHandledException;
use Archict\Router\ResponseHandler;
use ReflectionClass;

/**
* @internal
*/
final class ConfigurationValidator
{
/**
* @throws HTTPCodeNotHandledException
* @throws ErrorHandlerShouldImplementInterfaceException
*/
public function validate(RouterConfiguration $configuration): void
{
foreach ($configuration->error_handling as $code => $handler) {
if ($code < 400 || $code >= 600) {
throw new HTTPCodeNotHandledException($code);
}

if (class_exists($handler)) {
$reflection = new ReflectionClass($handler);
if (!$reflection->implementsInterface(ResponseHandler::class)) {
throw new ErrorHandlerShouldImplementInterfaceException($code, $handler);
}
}
}
}
}
42 changes: 42 additions & 0 deletions include/Config/RouterConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* MIT License
*
* Copyright (c) 2024-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types=1);

namespace Archict\Router\Config;

/**
* @internal
*/
final readonly class RouterConfiguration
{
/**
* @param array<int, string|class-string> $error_handling
*/
public function __construct(
public array $error_handling = [],
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* MIT License
*
* Copyright (c) 2024-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types=1);

namespace Archict\Router\Exception;

use Archict\Router\ResponseHandler;

final class ErrorHandlerShouldImplementInterfaceException extends RouterException
{
public function __construct(int $code, string $class)
{
parent::__construct("Error handler $class for code $code should implement interface " . ResponseHandler::class);
}
}
36 changes: 36 additions & 0 deletions include/Exception/HTTPCodeNotHandledException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* MIT License
*
* Copyright (c) 2024-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types=1);

namespace Archict\Router\Exception;

final class HTTPCodeNotHandledException extends RouterException
{
public function __construct(int $code)
{
parent::__construct("Error handling doesn't handle code $code");
}
}
12 changes: 11 additions & 1 deletion include/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

use Archict\Brick\Service;
use Archict\Core\Event\EventDispatcher;
use Archict\Router\Config\ConfigurationValidator;
use Archict\Router\Config\RouterConfiguration;
use Archict\Router\Exception\ErrorHandlerShouldImplementInterfaceException;
use Archict\Router\Exception\FailedToCreateRouteException;
use Archict\Router\Exception\HTTP\HTTPException;
use Archict\Router\Exception\HTTPCodeNotHandledException;
use Archict\Router\Exception\RouterException;
use Archict\Router\HTTP\FinalResponseHandler;
use Archict\Router\Route\MiddlewareInformation;
Expand All @@ -26,7 +30,7 @@
use Psr\SimpleCache\InvalidArgumentException;
use Throwable;

#[Service]
#[Service(RouterConfiguration::class, 'router.yml')]
final class Router
{
private const CACHE_KEY = 'archict/router->route_collection';
Expand All @@ -35,12 +39,18 @@ final class Router
private RouteCollection $route_collection;
private ?ResponseInterface $response;

/**
* @throws HTTPCodeNotHandledException
* @throws ErrorHandlerShouldImplementInterfaceException
*/
public function __construct(
private readonly EventDispatcher $event_dispatcher,
private readonly CacheInterface $cache,
private readonly RouterConfiguration $configuration,
) {
$this->mapper = (new MapperBuilder())->allowPermissiveTypes()->mapper();
$this->normalizer = (new MapperBuilder())->normalizer(Format::json());
(new ConfigurationValidator())->validate($this->configuration);
}

/**
Expand Down

0 comments on commit d7c710b

Please sign in to comment.