Add ViewHelper in view for injecting some basic classes in views
This commit is contained in:
parent
de5ff3a2b9
commit
45797ba20a
@ -13,9 +13,9 @@ use Ardent\Undercurrent\View\ViewInterface;
|
|||||||
class HelloWorldController
|
class HelloWorldController
|
||||||
{
|
{
|
||||||
#[Route('/')]
|
#[Route('/')]
|
||||||
public function index(): ResponseInterface
|
public function index(): ViewInterface
|
||||||
{
|
{
|
||||||
return new GenericResponse('Index, <a href="/hello">Hello</a>');
|
return new BaseView('/index.php');
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/error')]
|
#[Route('/error')]
|
||||||
@ -30,10 +30,10 @@ class HelloWorldController
|
|||||||
return new GenericResponse('Hello World!');
|
return new GenericResponse('Hello World!');
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/view')]
|
#[Route('/view/{name}')]
|
||||||
public function view(): ViewInterface
|
public function view(string $name): ViewInterface
|
||||||
{
|
{
|
||||||
return new BaseView('/Template/home.php', ['message' => 'Hello World!']);
|
return new BaseView('/home.php', ['name' => $name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/routes/{word}')]
|
#[Route('/routes/{word}')]
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
<?php /** @var $this \Ardent\Undercurrent\View\BaseView */ ?>
|
<?php /** @var $this \Ardent\Undercurrent\View\BaseView */ ?>
|
||||||
|
|
||||||
<html>
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>TOSTI MAN KOMT JE HALEN</title>
|
<title>Base view title</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<?= $this->data['slot'] ?>
|
Base, slot: <?= $this->data['slot'] ?>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -1,4 +1,4 @@
|
|||||||
<?php /** @var $this \Ardent\Undercurrent\View\BaseView */ ?>
|
<?php /** @var $this \Ardent\Undercurrent\View\BaseView */ ?>
|
||||||
<?php $this->extends = '/Template/base.php' ?>
|
<?php $this->extends = '/base.php' ?>
|
||||||
|
|
||||||
<?= $this->data['message'] ?>
|
Hello <?= $this->data['name'] ?>!
|
||||||
|
4
app/Template/index.php
Normal file
4
app/Template/index.php
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?php /** @var $this \Ardent\Undercurrent\View\BaseView */ ?>
|
||||||
|
<?php $this->extends = '/base.php' ?>
|
||||||
|
|
||||||
|
Hello <a href="<?= $this->helper->getRoute(\App\Controller\HelloWorldController::class, 'hello') ?>">index</a>!
|
@ -6,6 +6,7 @@ class AppConfig
|
|||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly string $rootPath,
|
private readonly string $rootPath,
|
||||||
|
private readonly string $templatePath,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -14,4 +15,9 @@ class AppConfig
|
|||||||
{
|
{
|
||||||
return $this->rootPath;
|
return $this->rootPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getTemplatePath(): string
|
||||||
|
{
|
||||||
|
return $this->getRootPath() . $this->templatePath;
|
||||||
|
}
|
||||||
}
|
}
|
@ -11,6 +11,7 @@ class Route
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
public string $path,
|
public string $path,
|
||||||
public ?MethodEnum $method = null,
|
public ?MethodEnum $method = null,
|
||||||
|
public ?string $name = null,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ namespace Ardent\Undercurrent\Http;
|
|||||||
use Ardent\Undercurrent\AppConfig;
|
use Ardent\Undercurrent\AppConfig;
|
||||||
use Ardent\Undercurrent\Container\ContainerInterface;
|
use Ardent\Undercurrent\Container\ContainerInterface;
|
||||||
use Ardent\Undercurrent\Logger\LoggerInterface;
|
use Ardent\Undercurrent\Logger\LoggerInterface;
|
||||||
|
use Ardent\Undercurrent\View\ViewHelper;
|
||||||
use Ardent\Undercurrent\View\ViewInterface;
|
use Ardent\Undercurrent\View\ViewInterface;
|
||||||
|
|
||||||
class GenericRouter implements RouterInterface
|
class GenericRouter implements RouterInterface
|
||||||
@ -38,7 +39,10 @@ class GenericRouter implements RouterInterface
|
|||||||
);
|
);
|
||||||
|
|
||||||
if ($response instanceof ViewInterface) {
|
if ($response instanceof ViewInterface) {
|
||||||
return new GenericResponse($response->render($this->container->get(AppConfig::class)));
|
return new GenericResponse($response->render(
|
||||||
|
$this->container->get(AppConfig::class),
|
||||||
|
$this->container->get(ViewHelper::class)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
@ -47,7 +51,7 @@ class GenericRouter implements RouterInterface
|
|||||||
throw new RouteNotFoundException($request);
|
throw new RouteNotFoundException($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toUri(string $controller, string $method): string
|
public function toUri(string $controller, string $method, array $params): string
|
||||||
{
|
{
|
||||||
foreach ($this->config->getRoutes() as $route) {
|
foreach ($this->config->getRoutes() as $route) {
|
||||||
if ($route->getController() === $controller && $route->getMethod() === $method) {
|
if ($route->getController() === $controller && $route->getMethod() === $method) {
|
||||||
|
@ -7,4 +7,6 @@ use Ardent\Undercurrent\View\ViewInterface;
|
|||||||
interface RouterInterface
|
interface RouterInterface
|
||||||
{
|
{
|
||||||
public function dispatch(RequestInterface $request): ResponseInterface;
|
public function dispatch(RequestInterface $request): ResponseInterface;
|
||||||
|
|
||||||
|
public function toUri(string $controller, string $method, array $params): string;
|
||||||
}
|
}
|
@ -14,6 +14,7 @@ use Ardent\Undercurrent\Http\RouterInterface;
|
|||||||
use Ardent\Undercurrent\Http\StatusEnum;
|
use Ardent\Undercurrent\Http\StatusEnum;
|
||||||
use Ardent\Undercurrent\Logger\LogContainer;
|
use Ardent\Undercurrent\Logger\LogContainer;
|
||||||
use Ardent\Undercurrent\Logger\LoggerInterface;
|
use Ardent\Undercurrent\Logger\LoggerInterface;
|
||||||
|
use Ardent\Undercurrent\View\ViewHelper;
|
||||||
use Ardent\Undercurrent\View\ViewInterface;
|
use Ardent\Undercurrent\View\ViewInterface;
|
||||||
|
|
||||||
class BaseKernel
|
class BaseKernel
|
||||||
@ -26,7 +27,7 @@ class BaseKernel
|
|||||||
|
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
$appConfig = new AppConfig($this->rootDirectory);
|
$appConfig = new AppConfig($this->rootDirectory, '/Template');
|
||||||
|
|
||||||
$container = (new GenericContainer());
|
$container = (new GenericContainer());
|
||||||
$container
|
$container
|
||||||
@ -36,6 +37,7 @@ class BaseKernel
|
|||||||
->add(GenericContainer::class, fn($container) => $container)
|
->add(GenericContainer::class, fn($container) => $container)
|
||||||
->add(AppConfig::class, fn() => $appConfig)
|
->add(AppConfig::class, fn() => $appConfig)
|
||||||
->add(GenericRouter::class)
|
->add(GenericRouter::class)
|
||||||
|
->add(ViewHelper::class)
|
||||||
->add(LogContainer::class);
|
->add(LogContainer::class);
|
||||||
|
|
||||||
$this->dependencies($container);
|
$this->dependencies($container);
|
||||||
|
@ -3,28 +3,29 @@
|
|||||||
namespace Ardent\Undercurrent\View;
|
namespace Ardent\Undercurrent\View;
|
||||||
|
|
||||||
use Ardent\Undercurrent\AppConfig;
|
use Ardent\Undercurrent\AppConfig;
|
||||||
use Ardent\Undercurrent\Http\GenericResponse;
|
|
||||||
use Ardent\Undercurrent\Http\ResponseInterface;
|
|
||||||
|
|
||||||
class BaseView implements ViewInterface
|
class BaseView implements ViewInterface
|
||||||
{
|
{
|
||||||
|
private ViewHelper $helper;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly string $path,
|
private readonly string $path,
|
||||||
private readonly array $data = [],
|
private readonly array $data = [],
|
||||||
private ?string $extends = null,
|
private ?string $extends = null,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render(AppConfig $config): string
|
public function render(AppConfig $config, ViewHelper $helper): string
|
||||||
{
|
{
|
||||||
$path = $config->getRootPath() . $this->path;
|
$this->helper = $helper;
|
||||||
|
$path = $config->getTemplatePath() . $this->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);
|
return (new BaseView($this->extends, $this->data + ['slot' => $output]))->render($config, $helper);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
|
19
src/View/ViewHelper.php
Normal file
19
src/View/ViewHelper.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ardent\Undercurrent\View;
|
||||||
|
|
||||||
|
use Ardent\Undercurrent\Http\RouterInterface;
|
||||||
|
|
||||||
|
class ViewHelper
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly RouterInterface $router,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRoute(string $class, string $method, array $params = []): string
|
||||||
|
{
|
||||||
|
return $this->router->toUri($class, $method, $params);
|
||||||
|
}
|
||||||
|
}
|
@ -7,5 +7,5 @@ use Ardent\Undercurrent\Http\ResponseInterface;
|
|||||||
|
|
||||||
interface ViewInterface
|
interface ViewInterface
|
||||||
{
|
{
|
||||||
public function render(AppConfig $config): string;
|
public function render(AppConfig $config, ViewHelper $helper): string;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user