Add doctrine and seperate out the renderer
This commit is contained in:
@ -6,7 +6,8 @@ class AppConfig
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $rootPath,
|
||||
private readonly string $templatePath,
|
||||
private readonly string $templatePath = '/Template',
|
||||
private readonly string $entityPath = '/Entity',
|
||||
)
|
||||
{
|
||||
}
|
||||
@ -20,4 +21,9 @@ class AppConfig
|
||||
{
|
||||
return $this->getRootPath() . $this->templatePath;
|
||||
}
|
||||
|
||||
public function getEntityPath(): string
|
||||
{
|
||||
return $this->getRootPath() . $this->entityPath;
|
||||
}
|
||||
}
|
@ -12,6 +12,14 @@ class GenericConfig
|
||||
|
||||
public function get(string $key): mixed
|
||||
{
|
||||
return $this->config[$key];
|
||||
$keys = explode('.', $key);
|
||||
$value = $this->config;
|
||||
foreach ($keys as $key) {
|
||||
if (!isset($value[$key])) {
|
||||
throw new \InvalidArgumentException("Key '$key' not found in config");
|
||||
}
|
||||
$value = $value[$key];
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
}
|
49
src/Http/Renderer.php
Normal file
49
src/Http/Renderer.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Ardent\Undercurrent\Http;
|
||||
|
||||
use Ardent\Undercurrent\Container\ContainerInterface;
|
||||
use Ardent\Undercurrent\Logger\LogContainer;
|
||||
|
||||
class Renderer
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ContainerInterface $container,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function render(): void
|
||||
{
|
||||
$request = new GenericRequest(
|
||||
MethodEnum::from($_SERVER['REQUEST_METHOD']),
|
||||
$_SERVER['REQUEST_URI'],
|
||||
$_REQUEST,
|
||||
);
|
||||
$router = $this->container->get(RouterInterface::class);
|
||||
$log = $this->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);
|
||||
}
|
||||
|
||||
http_response_code($response->getStatus()->value);
|
||||
|
||||
foreach ($response->getHeaders() as $header) {
|
||||
header($header);
|
||||
}
|
||||
|
||||
echo $response->getBody();
|
||||
|
||||
echo '<pre>Log:<br>';
|
||||
foreach ($log->getLogs() as $log) {
|
||||
echo sprintf('%s<br>', $log);
|
||||
}
|
||||
echo '</pre>';
|
||||
}
|
||||
}
|
@ -6,82 +6,54 @@ use Ardent\Undercurrent\Config\AppConfig;
|
||||
use Ardent\Undercurrent\Config\GenericConfig;
|
||||
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 Ardent\Undercurrent\View\ViewHelper;
|
||||
use Doctrine\DBAL\DriverManager;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\ORMSetup;
|
||||
|
||||
class BaseKernel
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $rootDirectory,
|
||||
private readonly AppConfig $appConfig,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
public function setup(): ContainerInterface
|
||||
{
|
||||
$appConfig = new AppConfig($this->rootDirectory, '/Template');
|
||||
|
||||
$container = (new GenericContainer());
|
||||
$container
|
||||
->alias(RouterInterface::class, GenericRouter::class)
|
||||
->alias(ContainerInterface::class, GenericContainer::class)
|
||||
->alias(LoggerInterface::class, LogContainer::class)
|
||||
->add(GenericContainer::class, fn($container) => $container)
|
||||
->add(AppConfig::class, fn() => $appConfig)
|
||||
->add(AppConfig::class, fn() => $this->appConfig)
|
||||
->add(GenericRouter::class)
|
||||
->add(ViewHelper::class)
|
||||
->add(EntityManager::class, function (ContainerInterface $container) {
|
||||
$config = $container->get(GenericConfig::class);
|
||||
$paths = [$container->get(AppConfig::class)->getEntityPath()];
|
||||
|
||||
$ormConfig = ORMSetup::createAttributeMetadataConfiguration($paths, $config->get('dev'));
|
||||
$connection = DriverManager::getConnection($config->get('orm'), $ormConfig);
|
||||
|
||||
return new EntityManager($connection, $ormConfig);
|
||||
})
|
||||
->add(LogContainer::class);
|
||||
|
||||
// App related dependencies
|
||||
$this->dependencies($container);
|
||||
$configPath = $this->rootDirectory . '/config.php';
|
||||
$configPath = $this->appConfig->getRootPath() . '/config.php';
|
||||
if (file_exists($configPath)) {
|
||||
$container->add(GenericConfig::class, fn() => include $configPath);
|
||||
}
|
||||
|
||||
$this->render($container);
|
||||
}
|
||||
|
||||
private function render(GenericContainer $container): void
|
||||
{
|
||||
$request = new GenericRequest(
|
||||
MethodEnum::from($_SERVER['REQUEST_METHOD']),
|
||||
$_SERVER['REQUEST_URI'],
|
||||
$_REQUEST,
|
||||
);
|
||||
$router = $container->get(RouterInterface::class);
|
||||
$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);
|
||||
}
|
||||
|
||||
http_response_code($response->getStatus()->value);
|
||||
|
||||
foreach ($response->getHeaders() as $header) {
|
||||
header($header);
|
||||
}
|
||||
|
||||
echo $response->getBody();
|
||||
|
||||
echo '<pre>Log:<br>';
|
||||
foreach ($log->getLogs() as $log) {
|
||||
echo sprintf('%s<br>', $log);
|
||||
}
|
||||
echo '</pre>';
|
||||
return $container;
|
||||
}
|
||||
|
||||
protected function addControllers(ContainerInterface $container, array $controllers): void
|
||||
|
Reference in New Issue
Block a user