Add key based genericConfig

This commit is contained in:
Tim 2023-08-15 21:21:24 +02:00
parent 18848e041f
commit 56e8caa26c
10 changed files with 43 additions and 9 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
vendor/
app/config.php

9
app/config.example.php Normal file
View File

@ -0,0 +1,9 @@
<?php
namespace App;
use Ardent\Undercurrent\Config\GenericConfig;
return new GenericConfig([
'key' => 'value',
]);

View File

@ -1,6 +1,6 @@
<?php
namespace Ardent\Undercurrent;
namespace Ardent\Undercurrent\Config;
class AppConfig
{

View File

@ -0,0 +1,17 @@
<?php
namespace Ardent\Undercurrent\Config;
class GenericConfig
{
public function __construct(
private readonly array $config,
)
{
}
public function get(string $key): mixed
{
return $this->config[$key];
}
}

View File

@ -2,7 +2,6 @@
namespace Ardent\Undercurrent\Container;
use Ardent\Undercurrent\Http\RouteConfig;
use Ardent\Undercurrent\Logger\LogContainer;
use Ardent\Undercurrent\Logger\LoggerInterface;
use Exception;
@ -41,6 +40,10 @@ class GenericContainer implements ContainerInterface
public function alias(string $alias, string $className): self
{
if (isset($this->aliases[$alias])) {
throw new Exception(sprintf('Class %s already defined', $className));
}
$this->aliases[$alias] = $className;
return $this;

View File

@ -2,7 +2,7 @@
namespace Ardent\Undercurrent\Http;
use Ardent\Undercurrent\AppConfig;
use Ardent\Undercurrent\Config\AppConfig;
use Ardent\Undercurrent\Container\ContainerInterface;
use Ardent\Undercurrent\Logger\LoggerInterface;
use Ardent\Undercurrent\View\ViewHelper;

View File

@ -2,7 +2,8 @@
namespace Ardent\Undercurrent\Kernel;
use Ardent\Undercurrent\AppConfig;
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;
@ -15,7 +16,6 @@ use Ardent\Undercurrent\Http\StatusEnum;
use Ardent\Undercurrent\Logger\LogContainer;
use Ardent\Undercurrent\Logger\LoggerInterface;
use Ardent\Undercurrent\View\ViewHelper;
use Ardent\Undercurrent\View\ViewInterface;
class BaseKernel
{
@ -40,7 +40,12 @@ class BaseKernel
->add(ViewHelper::class)
->add(LogContainer::class);
// App related dependencies
$this->dependencies($container);
$configPath = $this->rootDirectory . '/config.php';
if (file_exists($configPath)) {
$container->add(GenericConfig::class, fn() => include $configPath);
}
$this->render($container);
}

View File

@ -2,7 +2,7 @@
namespace Ardent\Undercurrent\View;
use Ardent\Undercurrent\AppConfig;
use Ardent\Undercurrent\Config\AppConfig;
class BaseView implements ViewInterface
{

View File

@ -2,8 +2,7 @@
namespace Ardent\Undercurrent\View;
use Ardent\Undercurrent\AppConfig;
use Ardent\Undercurrent\Http\ResponseInterface;
use Ardent\Undercurrent\Config\AppConfig;
interface ViewInterface
{