GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - InitPHP/Config: Advanced configuration manager for PHP with dotted-path access, case-insensitive keys, and array/file/directory/class loaders.

BranchesTags
Open more actions menu

Latest commit

 

History

7 Commits
7 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
docs
docs
 
 
src
src
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.php-cs-fixer.dist.php
.php-cs-fixer.dist.php
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
phpstan.neon.dist
phpstan.neon.dist
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 

Repository files navigation

InitPHP Config

Advanced configuration manager for PHP: a single configuration tree with dotted-path access, case-insensitive keys, and loaders for arrays, PHP files, whole directories and class/object properties — usable either as an injectable object or through a static facade.

Designed & Maintained with Tan CI Latest Stable Version Total Downloads License PHP Version Require

Requirements

  • PHP 8.1 or higher
  • initphp/parameterbag ^2.0

Installation

composer require initphp/config

Key Concepts

  • Dotted paths — db.host addresses host inside the db array. Keys can be nested arbitrarily deep.
  • Case-insensitive keys — DB.Host, db.host and Db.HOST all refer to the same entry. Keys are folded to lower-case internally.
  • Three entry points that all share the same read/write contract (ConfigInterface):
    • Classes — turn a class's public properties into configuration.
    • Library — an injectable object with the full loader API.
    • Config — a static facade over a shared Library.

Quick Start

Configuration classes

Declare your configuration as public properties and read it through the shared API:

use InitPHP\Config\Classes;

final class MyAppConfig extends Classes
{
    public string $url  = 'http://lvh.me';
    public string $name = 'LocalHost';

    /** @var array<string, string> */
    public array $db = [
        'host' => 'localhost',
        'user' => 'root',
    ];
}

$config = new MyAppConfig();

$config->get('url');                 // "http://lvh.me"
$config->get('db.host');             // "localhost"
$config->get('details', 'Not Found'); // "Not Found"

if ($config->has('name')) {
    $config->get('name');            // "LocalHost"
}

The Library object

use InitPHP\Config\Library;

$config = new Library();

$config->set('site.url', 'http://lvh.me')
       ->set('site.db.host', 'localhost');

$config->get('site.url');     // "http://lvh.me"
$config->get('SITE.DB.HOST'); // "localhost" (case-insensitive)

The Config static facade

Every call is forwarded to a lazily created, process-wide Library singleton:

use InitPHP\Config\Config;

Config::setArray('site', ['url' => 'http://lvh.me']);

Config::get('site.url'); // "http://lvh.me"
Config::has('site.url'); // true

Config::reset(); // discard the shared instance (useful in tests)

Reading and writing

The following methods are available on Classes, Library, and the Config facade alike:

Method Description
get(string $key, mixed $default = null): mixed Read a value, or $default when the key is absent.
set(string $key, mixed $value): self Write a value (intermediate arrays are created as needed).
has(string $key): bool Whether the key exists (a stored null still counts as present).
remove(string $key): self Remove a key (a no-op when it is absent).
all(): array The entire configuration tree as a plain array.

Loaders (Library / Config)

setArray()

public function setArray(?string $name, array $assoc = []): self;

Imports an associative array. When $name is null or '' the array is merged into the root; otherwise it is stored under $name.

Config::setArray('site', [
    'url' => 'http://lvh.me',
    'db'  => ['host' => 'localhost', 'user' => 'db_user'],
]);

Config::get('site.url');     // "http://lvh.me"
Config::get('site.db.host'); // "localhost"

setFile()

public function setFile(?string $name, string $path): self;

Loads a PHP file that returns an associative array.

// config/db.php
return [
    'HOST' => 'localhost',
    'USER' => 'root',
];
Config::setFile('db', __DIR__ . '/config/db.php');

Config::get('db.host'); // "localhost"  (keys are case-insensitive)

setDir()

public function setDir(?string $name, string $path, array $exclude = []): self;

Loads every top-level *.php file in a directory. Each file is stored under a key derived from its base name; when $name is given it becomes a common prefix. Files can be skipped via $exclude (with or without the .php suffix).

// config/db.php   -> returns ['HOST' => 'localhost']
// config/site.php -> returns ['URL'  => 'http://lvh.me']

Config::setDir('app', __DIR__ . '/config', ['secrets']);

Config::get('app.db.host'); // "localhost"
Config::get('app.site.url'); // "http://lvh.me"

setClass()

public function setClass(string|object $classOrObject): self;

Imports the public properties of a class or object under the class's short name. A class name imports the property defaults; an instance imports the current values.

namespace App\Config;

class AppConfig
{
    public string $url = 'http://lvh.me';
}

class Database
{
    public string $host = 'localhost';
}
Config::setClass(\App\Config\AppConfig::class); // by class name
Config::setClass(new \App\Config\Database());   // by instance

Config::get('appconfig.url'); // "http://lvh.me"
Config::get('database.host'); // "localhost"

Object-style access (Library)

A Library exposes top-level entries as read-only nested objects:

$config = new Library();
$config->set('db.host', 'localhost')->set('db.user', 'root');

$config->db->host; // "localhost"
$config->db->user; // "root"

Error handling

Loader failures throw InitPHP\Config\Exceptions\ConfigException (a RuntimeException): missing files, files that do not return an array, invalid directories, and unknown class names.

use InitPHP\Config\Exceptions\ConfigException;

try {
    Config::setFile('db', '/path/to/missing.php');
} catch (ConfigException $e) {
    // "Configuration file "/path/to/missing.php" was not found."
}

Documentation

In-depth guides with runnable examples live in docs/.

Testing

composer test      # PHPUnit
composer analyse   # PHPStan (level 8)
composer cs:check  # PHP-CS-Fixer (dry-run)

Credits

  • Muhammet ŞAFAK — [email protected]

License

Released under the MIT License. Copyright © InitPHP.

About

Advanced configuration manager for PHP with dotted-path access, case-insensitive keys, and array/file/directory/class loaders.

initphp.org

Topics

configconfig-managerconfigurationconfiguration-managementdot-notationdotted-pathinitphpparameter-bagphpphp-configphp-configurationphp-librarysettings

Resources

Readme
MIT license

Code of conduct

Code of conduct

Contributing

Contributing

Security policy

Security policy
Activity
Custom properties

Stars

2 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Sponsor this project

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.