Skip to content

andrewdyer/php-settings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚙️ Settings

A simple, framework-agnostic settings container for PHP applications.

⚖️ License

Licensed under the MIT license and is free for private or commercial projects.

✨ Introduction

This library provides a lightweight wrapper around a plain PHP array, giving you a clean interface for storing and retrieving application configuration values. It offers a straightforward, dependency-free way to manage configuration without coupling your code to a specific framework, making it easy to drop into any project or architecture.

📥 Installation

composer require andrewdyer/php-settings

Requires PHP 8.3 or newer.

🚀 Getting Started

Create a Settings instance by passing in your configuration array.

declare(strict_types=1);

use Anddye\Settings\Settings;

$settings = new Settings([
    'app_name' => 'My Application',
    'database' => [
        'host' => 'localhost',
        'port' => 5432,
        'credentials' => [
            'username' => 'admin',
            'password' => 'secret',
        ],
    ],
]);

📚 Usage

Retrieve all settings

Get the entire settings array using the all() method.

$all = $settings->all();

Retrieve a setting

Access a top-level setting using its key with the get() method.

$appName = $settings->get('app_name'); // 'My Application'

The get() method can also access nested settings using dot notation.

$host = $settings->get('database.host'); // 'localhost'

Dot notation can traverse multiple levels of configuration.

$username = $settings->get('database.credentials.username'); // 'admin'

Requesting a parent key with get() returns the entire nested configuration array.

$database = $settings->get('database');

Check if a setting exists

Check if a top-level key exists using the has() method.

$settings->has('app_name'); // true

The has() method also supports nested keys using dot notation.

$settings->has('database.credentials.password'); // true

Literal keys containing dots

If a top-level key contains dots, the exact key takes precedence over nested resolution.

$settings = new Settings([
    'database.host' => 'literal',
]);

$settings->get('database.host'); // 'literal'

If the exact key does not exist, the get() method falls back to resolving nested values using dot notation.

$settings = new Settings([
    'database' => [
        'host' => 'nested',
    ],
]);

$settings->get('database.host'); // 'nested'

About

A simple, framework-agnostic settings container for PHP applications.

Resources

Stars

Watchers

Forks

Releases

No releases published

Contributors

Languages