-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: application configuration (#329)
* 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
1 parent
7dfda69
commit 97c89c5
Showing
13 changed files
with
410 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.