initphp/config

InitPHP Config Library

1.0 2022-03-16 11:20 UTC

This package is auto-updated.

Last update: 2024-10-24 11:56:38 UTC


README

Advanced configuration manager library.

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Requirements

Installation

composer require initphp/config

Usage

Config Classes

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"
}

Config Library

Config::setClass()

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');

Config::setArray()

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');

Config::setFile()

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');

Config::setDir()

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');

Credit

License

Copyright © 2022 MIT License