Advanced configuration manager library.
- PHP 7.4 or higher
- ParameterBag Library
composer require initphp/config
class MyAppConfig extends \InitPHP\Config\Classes
{
public $url = 'http://lvh.me';
public $name = 'LocalHost';
public $db = [
'host' => 'localhost',
'user' => 'root'
];
// ...
}
$config = new MyAppConfig();
echo $config->get('url');
// Output : "http://lvh.me"
echo $config->get('details', 'Not Found');
// Output : "Not Found"
echo $config->get('db.host');
// Output : "localhost"
if($config->has('name')){
echo $config->get('name');
// Output : "LocalHost"
}
Lets you define properties of an object or class as a configuration.
public function setClass(string|object $classOrObject): self;
Example :
namespace App\Config;
class AppConfig
{
public $url = 'http://lvh.me';
}
class Database
{
public $host = 'localhost';
}
use \InitPHP\Config\Config;
// Class
Config::setClass(\App\Config\AppConfig::class);
// or Object
Config::setClass(new \App\Config\Database());
Config::get('appconfig.url');
Config::get('database.host');
Imports an array.
public function setArray(?string $name, array $assoc = []): self;
Example :
require_once "vendor/autoload.php";
use \InitPHP\Config\Config;
$configs = [
'url' => 'http://lvh.me',
'db' => [
'host' => 'localhost',
'user' => 'db_user',
'pass' => '',
'name' => 'database'
],
];
Config::setArray('site', $configs);
Config::get('site.url');
Config::get('site.db.host', '127.0.0.1');
Config::get('site.db.user', 'root');
Loads the configurations in the PHP file, which returns an associative array.
public function setFile(?string $name, string $path): self;
Example :
public_html/db_config.php
:
<?php
return [
'HOST' => 'localhost',
'USER' => 'root',
'PASS' => '',
'NAME' => 'database'
];
require_once "vendor/autoload.php";
use \InitPHP\Config\Config;
Config::setFile('DB', __DIR__ . '/public_html/db_config.php');
// Usage :
Config::get('db.host');
Loads PHP files in a directory as configuration files.
public function setDir(?string $name, string $path, array $exclude = []): self;
Example :
public_html/config/db.php
:
<?php
return [
'HOST' => 'localhost',
'USER' => 'root',
'PASS' => '',
'NAME' => 'database'
];
public_html/config/site.php
:
<?php
return [
'URL' => 'http://lvh.me',
// ...
];
require_once "vendor/autoload.php";
use \PHPConfig\Config;
Config::setDir('app', __DIR__ . '/public_html/config/');
// Usage :
Config::get('app.site.url');
Config::get('app.db.host');
Copyright © 2022 MIT License