Streamline view rendering

This commit is contained in:
Tim 2023-08-15 13:37:03 +02:00
parent 45797ba20a
commit 24683e70a8
6 changed files with 37 additions and 9 deletions

View File

@ -15,7 +15,7 @@ class HelloWorldController
#[Route('/')] #[Route('/')]
public function index(): ViewInterface public function index(): ViewInterface
{ {
return new BaseView('/index.php'); return new BaseView('/index');
} }
#[Route('/error')] #[Route('/error')]
@ -33,7 +33,7 @@ class HelloWorldController
#[Route('/view/{name}')] #[Route('/view/{name}')]
public function view(string $name): ViewInterface public function view(string $name): ViewInterface
{ {
return new BaseView('/home.php', ['name' => $name]); return new BaseView('/home', ['name' => $name]);
} }
#[Route('/routes/{word}')] #[Route('/routes/{word}')]

View File

@ -1,4 +1,4 @@
<?php /** @var $this \Ardent\Undercurrent\View\BaseView */ ?> <?php /** @var $this \Ardent\Undercurrent\View\BaseView */ ?>
<?php $this->extends = '/base.php' ?> <?php $this->extends = '/base' ?>
Hello <a href="<?= $this->helper->getRoute(\App\Controller\HelloWorldController::class, 'hello') ?>">index</a>! Hello <a href="<?= $this->getRoute(\App\Controller\HelloWorldController::class, 'hello') ?>">index</a>!

View File

@ -3,6 +3,8 @@
namespace Ardent\Undercurrent\Container; namespace Ardent\Undercurrent\Container;
use Ardent\Undercurrent\Http\RouteConfig; use Ardent\Undercurrent\Http\RouteConfig;
use Ardent\Undercurrent\Logger\LogContainer;
use Ardent\Undercurrent\Logger\LoggerInterface;
use Exception; use Exception;
use ReflectionClass; use ReflectionClass;
use ReflectionMethod; use ReflectionMethod;
@ -15,12 +17,20 @@ class GenericContainer implements ContainerInterface
private array $aliases = []; 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) { if (!$definition) {
$definition = fn() => $this->autowire($className); $definition = fn() => $this->autowire($className);
} }
if (isset($this->definitions[$className])) {
throw new Exception(sprintf('Class %s already defined', $className));
}
$this->definitions[$className] = [ $this->definitions[$className] = [
'definition' => $definition, 'definition' => $definition,
'singleton' => $singleton, 'singleton' => $singleton,
@ -55,6 +65,10 @@ class GenericContainer implements ContainerInterface
return $this->instances[$className]; return $this->instances[$className];
} }
$instance = $definition($this); $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; $this->instances[$className] = $instance;
} else { } else {
$instance = $definition($this); $instance = $definition($this);

View File

@ -72,9 +72,11 @@ class BaseKernel
echo $response->getBody(); echo $response->getBody();
echo '<pre>';
foreach ($log->getLogs() as $log) { foreach ($log->getLogs() as $log) {
echo sprintf('<p>%s</p>', $log); echo sprintf('%s<br>', $log);
} }
echo '</pre>';
} }
protected function addControllers(ContainerInterface $container, array $controllers): void protected function addControllers(ContainerInterface $container, array $controllers): void

View File

@ -20,14 +20,26 @@ class BaseView implements ViewInterface
{ {
$this->helper = $helper; $this->helper = $helper;
$path = $config->getTemplatePath() . $this->path; $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(); ob_start();
include $path; include $path;
$output = ob_get_clean(); $output = ob_get_clean();
if ($this->extends) { 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; return $output;
} }
public function getRoute(string $class, string $method, array $params = []): string
{
return $this->helper->getRouter()->toUri($class, $method, $params);
}
} }

View File

@ -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;
} }
} }