All Projects → selective-php → config

selective-php / config

Licence: MIT license
Config component, strictly typed

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to config

Ins sandstorm
[INS] Config setting for our sandstorm server
Stars: ✭ 61 (+335.71%)
Mutual labels:  config, settings, configuration
Dynaconf
Configuration Management for Python ⚙
Stars: ✭ 2,082 (+14771.43%)
Mutual labels:  config, settings, configuration
cfg-rs
A Configuration Library for Rust Applications
Stars: ✭ 18 (+28.57%)
Mutual labels:  config, settings, configuration
Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (+1078.57%)
Mutual labels:  config, settings, configuration
Rime pure
【rime小狼毫\trime同文】手机/PC一站式配置【简约皮肤\拼音搜狗词库\原创trime同文四叶草九宫格拼音方案\四叶草拼音、小鹤双拼、极品五笔、徐码、郑码】 rime配置
Stars: ✭ 73 (+421.43%)
Mutual labels:  config, settings, configuration
climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (+42.86%)
Mutual labels:  config, settings, configuration
configster
Rust library for parsing configuration files
Stars: ✭ 19 (+35.71%)
Mutual labels:  config, settings
javaproperties
Python library for reading & writing Java .properties files
Stars: ✭ 20 (+42.86%)
Mutual labels:  config, configuration
read-env
🔧 Transform environment variables into JSON object with sanitized values.
Stars: ✭ 60 (+328.57%)
Mutual labels:  config, configuration
nvim
❤️ A neovim config repo.
Stars: ✭ 33 (+135.71%)
Mutual labels:  config, configuration
plaster
Application config settings abstraction layer.
Stars: ✭ 19 (+35.71%)
Mutual labels:  config, settings
logstash-config
logstash-config provides a parser and abstract syntax tree (AST) for the Logstash config format, written in Go
Stars: ✭ 26 (+85.71%)
Mutual labels:  config, configuration
salak.rs
A multi layered configuration loader and zero-boilerplate configuration parser.
Stars: ✭ 27 (+92.86%)
Mutual labels:  config, configuration
Flex-AntiCheat
Flex AntiCheat - Optimized Configs For Multiple AntiCheats
Stars: ✭ 37 (+164.29%)
Mutual labels:  config, configuration
dotfiles
Linux configuration files (dotfiles) and some useful scripts
Stars: ✭ 22 (+57.14%)
Mutual labels:  config, configuration
libconfini
Yet another INI parser
Stars: ✭ 106 (+657.14%)
Mutual labels:  config, configuration
sitri
Sitri - powerful settings & configs for python
Stars: ✭ 20 (+42.86%)
Mutual labels:  config, configuration
superconfig
Access environment variables. Also includes presence validation, type coercion and default values.
Stars: ✭ 33 (+135.71%)
Mutual labels:  config, configuration
setset
Powerful Incremental Type-driven Settings Engine.
Stars: ✭ 20 (+42.86%)
Mutual labels:  settings, configuration
JsonSettings
This library simplifies creating configuration for your C# app/service by utilizing the serialization capabilities of Json.NET to serialize nested (custom) objects, dictionaries and lists as simply as by creating a POCO and inheriting JsonSettings class.
Stars: ✭ 59 (+321.43%)
Mutual labels:  settings, configuration

selective/config

A strictly typed configuration component for PHP. Inspired by Apache Commons Configuration.

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Requirements

  • PHP 7.2+ or 8

Installation

composer require selective/config

Theory of Operation

You can use the Configuration to read single values from a multidimensional array by passing the path to one of the get{type}() and find{type}() methods.

Each get*() / find*() method takes a default value as second argument. If the path cannot be found in the original array, the default is used as return value.

A get*() method returns only the declared return type. If the default value is not given and the element cannot be found, an exception is thrown.

A find*() method returns only the declared return type or null. No exception is thrown if the element cannot be found.

Usage

<?php

use Selective\Config\Configuration;

$config = new Configuration([
    'key1' => [
        'key2' => [
            'key3' => 'value1',
        ]
    ]
]);

// Output: value1
echo $config->getString('key1.key2.key3');

Slim 4 integration

Add this dependency injection container definition:

use Selective\Config\Configuration;

// ...

return [
    // Application settings
    Configuration::class => function () {
        return new Configuration(require __DIR__ . '/settings.php');
    },
    
    // ...
];

Examples

Configuring a database connection

The settings:

// Database settings
$settings['db'] = [
    'driver' => 'mysql',
    'host' => 'localhost',
    'username' => 'root',
    'database' => 'test',
    'password' => '',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'flags' => [
        PDO::ATTR_PERSISTENT => false,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    ],
];

The container definition:

use Selective\Config\Configuration;
use PDO;

return [
    // ...

    PDO::class => static function (ContainerInterface $container) {
        $config = $container->get(Configuration::class);

        $host = $config->getString('db.host');
        $dbname =  $config->getString('db.database');
        $username = $config->getString('db.username');
        $password = $config->getString('db.password');
        $charset = $config->getString('db.charset');
        $flags = $config->getArray('db.flags');
        $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";

        return new PDO($dsn, $username, $password, $flags);
    },

    // ...

];

Injecting the configuration

The settings:

$settings['module'] = [
    'key1' => 'my-value',
];

The consumer class:

<?php

namespace App\Domain\User\Service;

use Selective\Config\Configuration;

final class Foo
{
    private $config;

    public function __construct(Configuration $config)
    {
        $this->config = $config;
    }

    public function bar()
    {
        $myKey1 = $this->config->getString('module.key1');
        
        // ...
    }
}

Similar libraries

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].