Skip to content

Commit

Permalink
Better exception marking (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jędrzej authored Jan 12, 2024
1 parent c77b039 commit b496f20
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ final class Container implements ContainerInterface

/**
* @param class-string $id
*
* @throws ServiceNotFoundException
* @throws ReflectionException
*/
public function get(string $id): object
{
Expand Down Expand Up @@ -62,6 +65,8 @@ public function set(string $id, object $value): self

/**
* @param class-string $id
*
* @throws ServiceNotFoundException
*/
private function resolve(string $id): object
{
Expand All @@ -83,6 +88,9 @@ private function resolve(string $id): object

/**
* @param ReflectionClass<object> $item
*
* @throws ServiceNotFoundException
* @throws ReflectionException
*/
private function getInstance(ReflectionClass $item): object
{
Expand Down
15 changes: 13 additions & 2 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string, class-string> */
/** @var array<string, class-string> $router_path */
$routes = require $router_path;

$link = "{$_SERVER['REQUEST_METHOD']} /{$this->path[0]}";
Expand Down

0 comments on commit b496f20

Please sign in to comment.