Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #415 from leocavalcante/config
Browse files Browse the repository at this point in the history
Add Config Module
  • Loading branch information
leocavalcante committed Sep 20, 2020
2 parents c9b7b92 + e0cac16 commit 71dd195
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 62 deletions.
8 changes: 2 additions & 6 deletions composer.json
Expand Up @@ -30,6 +30,7 @@
"gabordemooij/redbean": "^5.5",
"google/protobuf": "^3.12",
"grpc/grpc": "^1.30",
"hassankhan/config": "^2.1",
"laminas/laminas-diactoros": "^2.2",
"laminas/laminas-httphandlerrunner": "^1.1",
"laminas/laminas-stratigility": "^3.2",
Expand All @@ -50,6 +51,7 @@
"Siler\\": "src/"
},
"files": [
"src/Config/Config.php",
"src/Container/Container.php",
"src/Diactoros/Diactoros.php",
"src/Dotenv/Dotenv.php",
Expand Down Expand Up @@ -101,11 +103,5 @@
"@composer analyze",
"@composer test"
]
},
"repositories": {
"phpinsights": {
"type": "vcs",
"url": "https://github.com/jibbarth/phpinsights"
}
}
}
170 changes: 146 additions & 24 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions src/Config/Config.php
@@ -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();
}

0 comments on commit 71dd195

Please sign in to comment.