This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #415 from leocavalcante/config
Add Config Module
- Loading branch information
Showing
8 changed files
with
298 additions
and
62 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,75 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Siler\Config; | ||
|
||
use Noodlehaus\Config; | ||
use Siler\Container; | ||
|
||
const CONFIG = 'siler_config'; | ||
|
||
/** | ||
* Gets or sets a configuration. | ||
* | ||
* @param string $key | ||
* @param mixed|null $default | ||
* @return mixed|null | ||
*/ | ||
function config(string $key, $default = null) | ||
{ | ||
/** @var Config|null $config */ | ||
$config = Container\get(CONFIG); | ||
|
||
if ($config === null) { | ||
return null; | ||
} | ||
|
||
return $config->get($key, $default); | ||
} | ||
|
||
/** | ||
* Load configuration values. | ||
* | ||
* @param string|array $values Filenames or string with configuration | ||
* @return Config | ||
*/ | ||
function load($values): Config | ||
{ | ||
$config = new Config($values); | ||
Container\set(CONFIG, $config); | ||
return $config; | ||
} | ||
|
||
/** | ||
* Checks if the key exists on the config. | ||
* | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
function has(string $key): bool | ||
{ | ||
/** @var Config|null $config */ | ||
$config = Container\get(CONFIG); | ||
|
||
if ($config === null) { | ||
return false; | ||
} | ||
|
||
return $config->has($key); | ||
} | ||
|
||
/** | ||
* Returns all the configurations. | ||
* | ||
* @return array|null | ||
*/ | ||
function all(): ?array | ||
{ | ||
/** @var Config|null $config */ | ||
$config = Container\get(CONFIG); | ||
|
||
if ($config === null) { | ||
return null; | ||
} | ||
|
||
return $config->all(); | ||
} |
Oops, something went wrong.