Skip to content

Commit

Permalink
feat: application configuration (#329)
Browse files Browse the repository at this point in the history
* feat: application configuration

* rename: `Config::class` to `ConfigRepository::class`

* test: add `ConfigRepositoryTest::class` test case

* fix: overwrite exist variable cause reset value in itterator

* remove unuse class
  • Loading branch information
SonyPradana committed Jun 20, 2024
1 parent 7dfda69 commit 97c89c5
Show file tree
Hide file tree
Showing 13 changed files with 410 additions and 45 deletions.
75 changes: 34 additions & 41 deletions src/System/Integrate/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,12 @@ public function __construct(string $base_path)
{
parent::__construct();

// base binding
static::$app = $this;
$this->set('app', $this);
$this->set(Application::class, $this);
$this->set(Container::class, $this);
// set base path
$this->setBasePath($base_path);
$this->setConfigPath($_ENV['CONFIG_PATH'] ?? DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR);

// load config and load provider
$this->loadConfig($base_path);
// base binding
$this->setBaseBinding();

// register base provider
$this->register(IntegrateServiceProvider::class);
Expand All @@ -231,43 +229,29 @@ public static function getIntance()
return static::$app;
}

/**
* Register base binding container.
*/
protected function setBaseBinding(): void
{
static::$app = $this;
$this->set('app', $this);
$this->set(Application::class, $this);
$this->set(Container::class, $this);
}

/**
* Load and set Configuration to application.
*
* @param string $base_path Base path
*
* @return void
*/
public function loadConfig(string $base_path)
public function loadConfig(ConfigRepository $configs): void
{
// set base path
$this->setBasePath($base_path);
$this->setAppPath($base_path);
$config_path = $base_path . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR;

// check file exis
$configs = $this->defaultConfigs();
$paths = [
'app.config.php',
'database.config.php',
'pusher.config.php',
'cachedriver.config.php',
'view.config.php',
];
foreach ($paths as $path) {
$file_path = $config_path . $path;

if (file_exists($file_path)) {
$config = include $file_path;
foreach ($config as $key => $value) {
$configs[$key] = $value;
}
}
}
// give access to get config directly
$this->set('config', fn (): ConfigRepository => $configs);

// base env
$this->set('environment', $configs['ENVIRONMENT']);
// application path
$this->setAppPath($this->basePath());
$this->setModelPath($configs['MODEL_PATH']);
$this->setViewPath($configs['VIEW_PATH']);
$this->setViewPaths($configs['VIEW_PATHS']);
Expand All @@ -277,7 +261,6 @@ public function loadConfig(string $base_path)
$this->setCommandPath($configs['COMMAND_PATH']);
$this->setCachePath($configs['CACHE_PATH']);
$this->setCompiledViewPath($configs['COMPILED_VIEW_PATH']);
$this->setConfigPath($configs['CONFIG']);
$this->setMiddlewarePath($configs['MIDDLEWARE']);
$this->setProviderPath($configs['SERVICE_PROVIDER']);
$this->setMigrationPath($configs['MIGRATION_PATH']);
Expand All @@ -292,17 +275,15 @@ public function loadConfig(string $base_path)
$this->set('config.view.extensions', $configs['VIEW_EXTENSIONS']);
// load provider
$this->providers = $configs['PROVIDERS'];
$this->defineder($configs);
// give access to get config directly
$this->set('config', $configs);
$this->defineder($configs->toArray());
}

/**
* Default config, prevent for empety config.
*
* @return array<string, mixed> Configs
*/
private function defaultConfigs()
public function defaultConfigs()
{
return [
// app config
Expand Down Expand Up @@ -660,6 +641,17 @@ public function appPath()
return $this->get('path.app');
}

/**
* Get application (bootstrapper) cach path.
* default './boostrap/cache/'.
*
* @return string
*/
public function getApplicationCachePath()
{
return $this->basePath() . 'bootsrap' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
}

/**
* Get model path.
*
Expand Down Expand Up @@ -1086,6 +1078,7 @@ protected function registerAlias(): void
'request' => [Request::class],
'view.instance' => [Templator::class],
'vite.gets' => [Vite::class],
'config' => [ConfigRepository::class],
] as $abstrack => $aliases) {
foreach ($aliases as $alias) {
$this->alias($abstrack, $alias);
Expand Down
32 changes: 32 additions & 0 deletions src/System/Integrate/Bootstrap/ConfigProviders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace System\Integrate\Bootstrap;

use System\Integrate\Application;
use System\Integrate\ConfigRepository;

class ConfigProviders
{
public function bootstrap(Application $app): void
{
$config_path = $app->configPath();
$config = $app->defaultConfigs();
$has_cache = false;
if (file_exists($file = $app->getApplicationCachePath() . 'config.php')) {
$config = array_merge($config, require $file);
$has_cache = true;
}

if (false === $has_cache) {
foreach (glob("{$config_path}*.config.php") as $path) {
foreach (include $path as $key => $value) {
$config[$key] = $value;
}
}
}

$app->loadConfig(new ConfigRepository($config));
}
}
88 changes: 88 additions & 0 deletions src/System/Integrate/ConfigRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace System\Integrate;

/**
* @implements \ArrayAccess<string, mixed>
*/
class ConfigRepository implements \ArrayAccess
{
/**
* Create new config using array.
*
* @param array<string, mixed> $config
*/
public function __construct(protected $config = [])
{
}

/**
* Checks if the given key or index exists in the config.
*/
public function has(string $key): bool
{
return array_key_exists($key, $this->config);
}

/**
* Get config.
*/
public function get(string $key, mixed $default = null): mixed
{
return $this->config[$key] ?? $default;
}

/**
* Set new or create config.
*/
public function set(string $key, mixed $value): void
{
$this->config[$key] = $value;
}

/**
* Convert back to array.
*
* @return array<string, mixed>
*/
public function toArray(): array
{
return $this->config;
}

// array access

/**
* Checks if the given key or index exists in the config.
*/
public function offsetExists(mixed $offset): bool
{
return $this->has($offset);
}

/**
* Get config.
*/
public function offsetGet(mixed $offset): mixed
{
return $this->get($offset);
}

/**
* Set new or create config.
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->set($offset, $value);
}

/**
* Unset or set to null.
*/
public function offsetUnset(mixed $offset): void
{
$this->set($offset, null);
}
}
2 changes: 2 additions & 0 deletions src/System/Integrate/Console/Karnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use System\Console\Style\Style;
use System\Integrate\Application;
use System\Integrate\Bootstrap\BootProviders;
use System\Integrate\Bootstrap\ConfigProviders;
use System\Integrate\Bootstrap\RegisterProviders;

class Karnel
Expand All @@ -21,6 +22,7 @@ class Karnel

/** @var array<int, class-string> Apllication bootstrap register. */
protected array $bootstrappers = [
ConfigProviders::class,
RegisterProviders::class,
BootProviders::class,
];
Expand Down
2 changes: 2 additions & 0 deletions src/System/Integrate/Http/Karnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use System\Http\Response;
use System\Integrate\Application;
use System\Integrate\Bootstrap\BootProviders;
use System\Integrate\Bootstrap\ConfigProviders;
use System\Integrate\Bootstrap\RegisterProviders;
use System\Integrate\Exceptions\Handler;
use System\Integrate\Http\Middleware\MaintenanceMiddleware;
Expand All @@ -30,6 +31,7 @@ class Karnel

/** @var array<int, class-string> Apllication bootstrap register. */
protected array $bootstrappers = [
ConfigProviders::class,
RegisterProviders::class,
BootProviders::class,
];
Expand Down
10 changes: 8 additions & 2 deletions tests/Integrate/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use PHPUnit\Framework\TestCase;
use System\Http\Request;
use System\Integrate\Application;
use System\Integrate\ConfigRepository;
use System\Integrate\Exceptions\ApplicationNotAvailable;
use System\Integrate\Http\Exception\HttpException;

Expand Down Expand Up @@ -38,13 +39,15 @@ public function itCanLoadApp()
}

/** @test */
public function itCanLoadDefaultConfig()
public function itCanLoadConfigFromDefault()
{
$app = new Application('/');

$app->loadConfig(new ConfigRepository($app->defaultConfigs()));
/** @var Config */
$config = $app->get('config');

$this->assertEquals($this->defaultConfigs(), $config);
$this->assertEquals($this->defaultConfigs(), $config->toArray());

$app->flush();
}
Expand Down Expand Up @@ -106,6 +109,7 @@ public function itCanTerminateAfterApplicationDone()
public function itCanDetectMaintenenceMode()
{
$app = new Application(__DIR__);
$app->loadConfig(new ConfigRepository($app->defaultConfigs()));

$this->assertFalse($app->isDownMaintenanceMode());

Expand Down Expand Up @@ -138,6 +142,7 @@ public function itCanGetDownDefault()
{
$app = new Application('/');

$app->loadConfig(new ConfigRepository($app->defaultConfigs()));
$this->assertEquals([
'redirect' => null,
'retry' => null,
Expand Down Expand Up @@ -214,6 +219,7 @@ public function itCanAddCallImediatllyIfApplicationAlredyBooted()
public function itCanCallDeprecatedMethod()
{
$app = new Application(__DIR__);
$app->loadConfig(new ConfigRepository($app->defaultConfigs()));

$this->assertEquals($app->basePath(), $app->base_path());
$this->assertEquals($app->appPath(), $app->app_path());
Expand Down
Loading

0 comments on commit 97c89c5

Please sign in to comment.