From 9309540c1f431b4b1ea8f9764bfaeafdf5953508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Buli=C5=84ski?= Date: Fri, 12 Jan 2024 22:59:53 +0100 Subject: [PATCH] Better exception marking (#2) --- src/App.php | 16 ++++++++++++++++ src/Container.php | 8 ++++++++ src/Router.php | 15 +++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/App.php b/src/App.php index 07bb121..c1f0ad7 100644 --- a/src/App.php +++ b/src/App.php @@ -5,12 +5,19 @@ namespace Depth\Techno; use Depth\Techno\Exceptions\EnvNotFoundException; +use Depth\Techno\Exceptions\RouteNotFoundException; +use Depth\Techno\Exceptions\RouterException; +use Depth\Techno\Exceptions\ServiceNotFoundException; use M1\Env\Parser; +use ReflectionException; final readonly class App { private Container $container; + /** + * @throws EnvNotFoundException + */ public function __construct( string $env_path = __DIR__.'/.env', private string $router_path = __DIR__.'/routes.php', @@ -21,6 +28,12 @@ public function __construct( $this->container = new Container(); } + /** + * @throws ServiceNotFoundException + * @throws RouterException + * @throws ReflectionException + * @throws RouteNotFoundException + */ public function run(): void { if ($_ENV['DEBUG'] ?? false) { @@ -32,6 +45,9 @@ public function run(): void $router->resolve($this->router_path)->send(); } + /** + * @throws EnvNotFoundException + */ private function loadEnv(string $env_path): void { $contents = file_get_contents($env_path); diff --git a/src/Container.php b/src/Container.php index ea40157..33b9813 100644 --- a/src/Container.php +++ b/src/Container.php @@ -21,6 +21,9 @@ final class Container implements ContainerInterface /** * @param class-string $id + * + * @throws ServiceNotFoundException + * @throws ReflectionException */ public function get(string $id): object { @@ -62,6 +65,8 @@ public function set(string $id, object $value): self /** * @param class-string $id + * + * @throws ServiceNotFoundException */ private function resolve(string $id): object { @@ -83,6 +88,9 @@ private function resolve(string $id): object /** * @param ReflectionClass $item + * + * @throws ServiceNotFoundException + * @throws ReflectionException */ private function getInstance(ReflectionClass $item): object { diff --git a/src/Router.php b/src/Router.php index aa76a78..cbf9c08 100644 --- a/src/Router.php +++ b/src/Router.php @@ -6,6 +6,8 @@ use Depth\Techno\Exceptions\RouteNotFoundException; use Depth\Techno\Exceptions\RouterException; +use Depth\Techno\Exceptions\ServiceNotFoundException; +use ReflectionException; use Symfony\Component\HttpFoundation\Response; use function array_key_exists; @@ -18,12 +20,21 @@ public function __construct( private Container $container, ) { - $this->path = explode('/', trim(explode('?', $_SERVER['REQUEST_URI'] ?? '')[0], '/')); + $this->path = explode( + '/', + trim(explode('?', $_SERVER['REQUEST_URI'] ?? '')[0], '/'), + ); } + /** + * @throws ServiceNotFoundException + * @throws RouterException + * @throws ReflectionException + * @throws RouteNotFoundException + */ public function resolve(string $router_path): Response { - /** @var array */ + /** @var array $router_path */ $routes = require $router_path; $link = "{$_SERVER['REQUEST_METHOD']} /{$this->path[0]}";