Add logger

diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php
index 334ecac..5a2ac6f 100644
--- a/src/Http/GenericRouter.php
+++ b/src/Http/GenericRouter.php
@@ -4,6 +4,7 @@ namespace Ardent\Undercurrent\Http;

 use Ardent\Undercurrent\Container\ClassNotFoundException;
 use Ardent\Undercurrent\Container\ContainerInterface;
+use Ardent\Undercurrent\Logger\LoggerInterface;
 use Exception;
 use ReflectionMethod;

@@ -11,6 +12,7 @@ class GenericRouter implements RouterInterface
 {
     public function __construct(
         private readonly ContainerInterface $container,
+        private readonly LoggerInterface    $logger,
         private readonly RouterConfig       $config,
     )
     {
@@ -28,6 +30,8 @@ class GenericRouter implements RouterInterface
                 continue;
             }

+            $this->logger->add(sprintf('Matched route %s', $route->getRoute()->path));
+
             return $this->container->call(
                 $route->getController(),
                 $route->getMethod(),
@@ -44,9 +48,9 @@ class GenericRouter implements RouterInterface
             return [];
         }

-        $result = preg_match_all('/{(\w+)}/', $routeUri, $tokens);
+        $result = preg_match_all('/{\w+}/', $routeUri, $tokens);

-        if (!$result) {
+        if (!$result === null) {
             return null;
         }

@@ -59,6 +63,7 @@ class GenericRouter implements RouterInterface
             ) . '$/';

         $result = preg_match($matchingRegex, $requestUri, $matches);
+
         if (!$result) {
             return null;
         }
diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php
index 634cafc..60164d8 100644
--- a/src/Kernel/BaseKernel.php
+++ b/src/Kernel/BaseKernel.php
@@ -5,10 +5,15 @@ namespace Ardent\Undercurrent\Kernel;
 use Ardent\Undercurrent\Container\ContainerInterface;
 use Ardent\Undercurrent\Container\GenericContainer;
 use Ardent\Undercurrent\Http\GenericRequest;
+use Ardent\Undercurrent\Http\GenericResponse;
 use Ardent\Undercurrent\Http\GenericRouter;
 use Ardent\Undercurrent\Http\MethodEnum;
 use Ardent\Undercurrent\Http\RouterConfig;
 use Ardent\Undercurrent\Http\RouterInterface;
+use Ardent\Undercurrent\Http\StatusEnum;
+use Ardent\Undercurrent\Logger\LogContainer;
+use Ardent\Undercurrent\Logger\LoggerInterface;
+use Exception;

 class BaseKernel
 {
@@ -18,8 +23,10 @@ class BaseKernel
         $container
             ->alias(RouterInterface::class, GenericRouter::class)
             ->alias(ContainerInterface::class, GenericContainer::class)
+            ->alias(LoggerInterface::class, LogContainer::class)
             ->add(GenericContainer::class, fn($container) => $container)
-            ->add(GenericRouter::class);
+            ->add(GenericRouter::class)
+            ->add(LogContainer::class);

         $this->dependencies($container);

@@ -34,7 +41,20 @@ class BaseKernel
             $_REQUEST,
         );
         $router = $container->get(RouterInterface::class);
-        $response = $router->dispatch($request);
+        $log = $container->get(LogContainer::class);
+        try {
+            $response = $router->dispatch($request);
+        } catch (\Throwable $e) {
+            $response = new GenericResponse(
+                $e->getMessage(),
+                StatusEnum::NOT_FOUND
+            );
+            //$response = $container->get(RouterConfig::class)->getExceptionRoute()->getController()::exception($e);
+        }
+
+        foreach ($log->getLogs() as $log) {
+            echo sprintf('<p>%s</p>', $log);
+        }

         http_response_code($response->getStatus()->value);

diff --git a/src/Logger/LogContainer.php b/src/Logger/LogContainer.php
new file mode 100644
index 0000000..39f3855
--- /dev/null
+++ b/src/Logger/LogContainer.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace Ardent\Undercurrent\Logger;
+
+class LogContainer implements LoggerInterface
+{
+    private array $logs = [];
+
+    public function add(string $log): void
+    {
+        $this->logs[] = $log;
+    }
+
+    public function getLogs(): array
+    {
+        return $this->logs;
+    }
+}
\ No newline at end of file
diff --git a/src/Logger/LoggerInterface.php b/src/Logger/LoggerInterface.php
new file mode 100644
index 0000000..1b2867a
--- /dev/null
+++ b/src/Logger/LoggerInterface.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Ardent\Undercurrent\Logger;
+
+interface LoggerInterface
+{
+    public function add(string $log): void;
+}
\ No newline at end of file
This commit is contained in:
Tim 2023-08-11 01:08:05 +02:00
parent 42c590dfd6
commit 1aded029fc
4 changed files with 55 additions and 4 deletions

View File

@ -4,6 +4,7 @@ namespace Ardent\Undercurrent\Http;
use Ardent\Undercurrent\Container\ClassNotFoundException; use Ardent\Undercurrent\Container\ClassNotFoundException;
use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Container\ContainerInterface;
use Ardent\Undercurrent\Logger\LoggerInterface;
use Exception; use Exception;
use ReflectionMethod; use ReflectionMethod;
@ -11,6 +12,7 @@ class GenericRouter implements RouterInterface
{ {
public function __construct( public function __construct(
private readonly ContainerInterface $container, private readonly ContainerInterface $container,
private readonly LoggerInterface $logger,
private readonly RouterConfig $config, private readonly RouterConfig $config,
) )
{ {
@ -28,6 +30,8 @@ class GenericRouter implements RouterInterface
continue; continue;
} }
$this->logger->add(sprintf('Matched route %s', $route->getRoute()->path));
return $this->container->call( return $this->container->call(
$route->getController(), $route->getController(),
$route->getMethod(), $route->getMethod(),
@ -44,9 +48,9 @@ class GenericRouter implements RouterInterface
return []; return [];
} }
$result = preg_match_all('/{(\w+)}/', $routeUri, $tokens); $result = preg_match_all('/{\w+}/', $routeUri, $tokens);
if (!$result) { if (!$result === null) {
return null; return null;
} }
@ -59,6 +63,7 @@ class GenericRouter implements RouterInterface
) . '$/'; ) . '$/';
$result = preg_match($matchingRegex, $requestUri, $matches); $result = preg_match($matchingRegex, $requestUri, $matches);
if (!$result) { if (!$result) {
return null; return null;
} }

View File

@ -5,10 +5,15 @@ namespace Ardent\Undercurrent\Kernel;
use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Container\ContainerInterface;
use Ardent\Undercurrent\Container\GenericContainer; use Ardent\Undercurrent\Container\GenericContainer;
use Ardent\Undercurrent\Http\GenericRequest; use Ardent\Undercurrent\Http\GenericRequest;
use Ardent\Undercurrent\Http\GenericResponse;
use Ardent\Undercurrent\Http\GenericRouter; use Ardent\Undercurrent\Http\GenericRouter;
use Ardent\Undercurrent\Http\MethodEnum; use Ardent\Undercurrent\Http\MethodEnum;
use Ardent\Undercurrent\Http\RouterConfig; use Ardent\Undercurrent\Http\RouterConfig;
use Ardent\Undercurrent\Http\RouterInterface; use Ardent\Undercurrent\Http\RouterInterface;
use Ardent\Undercurrent\Http\StatusEnum;
use Ardent\Undercurrent\Logger\LogContainer;
use Ardent\Undercurrent\Logger\LoggerInterface;
use Exception;
class BaseKernel class BaseKernel
{ {
@ -18,8 +23,10 @@ class BaseKernel
$container $container
->alias(RouterInterface::class, GenericRouter::class) ->alias(RouterInterface::class, GenericRouter::class)
->alias(ContainerInterface::class, GenericContainer::class) ->alias(ContainerInterface::class, GenericContainer::class)
->alias(LoggerInterface::class, LogContainer::class)
->add(GenericContainer::class, fn($container) => $container) ->add(GenericContainer::class, fn($container) => $container)
->add(GenericRouter::class); ->add(GenericRouter::class)
->add(LogContainer::class);
$this->dependencies($container); $this->dependencies($container);
@ -34,7 +41,20 @@ class BaseKernel
$_REQUEST, $_REQUEST,
); );
$router = $container->get(RouterInterface::class); $router = $container->get(RouterInterface::class);
$response = $router->dispatch($request); $log = $container->get(LogContainer::class);
try {
$response = $router->dispatch($request);
} catch (\Throwable $e) {
$response = new GenericResponse(
$e->getMessage(),
StatusEnum::NOT_FOUND
);
//$response = $container->get(RouterConfig::class)->getExceptionRoute()->getController()::exception($e);
}
foreach ($log->getLogs() as $log) {
echo sprintf('<p>%s</p>', $log);
}
http_response_code($response->getStatus()->value); http_response_code($response->getStatus()->value);

View File

@ -0,0 +1,18 @@
<?php
namespace Ardent\Undercurrent\Logger;
class LogContainer implements LoggerInterface
{
private array $logs = [];
public function add(string $log): void
{
$this->logs[] = $log;
}
public function getLogs(): array
{
return $this->logs;
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace Ardent\Undercurrent\Logger;
interface LoggerInterface
{
public function add(string $log): void;
}