diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php index aac2684..eed77a5 100644 --- a/app/Controller/HelloWorldController.php +++ b/app/Controller/HelloWorldController.php @@ -15,7 +15,7 @@ class HelloWorldController #[Route('/')] public function index(): ViewInterface { - return new BaseView('/index.php'); + return new BaseView('/index'); } #[Route('/error')] @@ -33,7 +33,7 @@ class HelloWorldController #[Route('/view/{name}')] public function view(string $name): ViewInterface { - return new BaseView('/home.php', ['name' => $name]); + return new BaseView('/home', ['name' => $name]); } #[Route('/routes/{word}')] diff --git a/app/Template/index.php b/app/Template/index.php index 9d633a4..9d8a892 100644 --- a/app/Template/index.php +++ b/app/Template/index.php @@ -1,4 +1,4 @@ -extends = '/base.php' ?> +extends = '/base' ?> -Hello index! +Hello index! diff --git a/src/Container/GenericContainer.php b/src/Container/GenericContainer.php index 0e467b6..96b0b94 100644 --- a/src/Container/GenericContainer.php +++ b/src/Container/GenericContainer.php @@ -3,6 +3,8 @@ namespace Ardent\Undercurrent\Container; use Ardent\Undercurrent\Http\RouteConfig; +use Ardent\Undercurrent\Logger\LogContainer; +use Ardent\Undercurrent\Logger\LoggerInterface; use Exception; use ReflectionClass; use ReflectionMethod; @@ -15,12 +17,20 @@ class GenericContainer implements ContainerInterface private array $aliases = []; - public function add(string $className, ?callable $definition = null, bool $singleton = true): self + public function add( + string $className, + ?callable $definition = null, + bool $singleton = true + ): self { if (!$definition) { $definition = fn() => $this->autowire($className); } + if (isset($this->definitions[$className])) { + throw new Exception(sprintf('Class %s already defined', $className)); + } + $this->definitions[$className] = [ 'definition' => $definition, 'singleton' => $singleton, @@ -55,6 +65,10 @@ class GenericContainer implements ContainerInterface return $this->instances[$className]; } $instance = $definition($this); + if ($className !== LogContainer::class) { + $logger = $this->get(LoggerInterface::class); + $logger->add(sprintf('Created singleton instance of %s', $className)); + } $this->instances[$className] = $instance; } else { $instance = $definition($this); diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php index 64f83cc..d983400 100644 --- a/src/Kernel/BaseKernel.php +++ b/src/Kernel/BaseKernel.php @@ -72,9 +72,11 @@ class BaseKernel echo $response->getBody(); + echo '
'; foreach ($log->getLogs() as $log) { - echo sprintf(''; } protected function addControllers(ContainerInterface $container, array $controllers): void diff --git a/src/View/BaseView.php b/src/View/BaseView.php index 1c9820b..5050e86 100644 --- a/src/View/BaseView.php +++ b/src/View/BaseView.php @@ -20,14 +20,26 @@ class BaseView implements ViewInterface { $this->helper = $helper; $path = $config->getTemplatePath() . $this->path; + if (!str_ends_with($path, '.php')) { + $path .= '.php'; + } + + if (!file_exists($path)) { + throw new \RuntimeException(sprintf('View file %s does not exist', $path)); + } ob_start(); include $path; $output = ob_get_clean(); if ($this->extends) { - return (new BaseView($this->extends, $this->data + ['slot' => $output]))->render($config, $helper); + return (new BaseView($this->extends, $this->data + ['slot' => $output]))->render(...func_get_args()); } return $output; } + + public function getRoute(string $class, string $method, array $params = []): string + { + return $this->helper->getRouter()->toUri($class, $method, $params); + } } \ No newline at end of file diff --git a/src/View/ViewHelper.php b/src/View/ViewHelper.php index f73f510..02436fd 100644 --- a/src/View/ViewHelper.php +++ b/src/View/ViewHelper.php @@ -12,8 +12,8 @@ class ViewHelper { } - public function getRoute(string $class, string $method, array $params = []): string + public function getRouter(): RouterInterface { - return $this->router->toUri($class, $method, $params); + return $this->router; } } \ No newline at end of file%s
', $log); + echo sprintf('%s
', $log); } + echo '