All Projects → northwoods → config

northwoods / config

Licence: other
Simple configuration management for PHP

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to config

Simple-YAML
A Java API that provides an easy-to-use way to store data using the YAML format.
Stars: ✭ 68 (+353.33%)
Mutual labels:  yaml, configuration
icingaweb2-module-fileshipper
Provide CSV, JSON, XML and YAML files as an Import Source for the Icinga Director and optionally ship hand-crafted additional Icinga2 config files
Stars: ✭ 25 (+66.67%)
Mutual labels:  yaml, configuration
Config
JSON or YAML configuration wrapper with convenient access methods.
Stars: ✭ 237 (+1480%)
Mutual labels:  yaml, configuration
Configurate
A simple configuration library for Java applications providing a node structure, a variety of formats, and tools for transformation
Stars: ✭ 148 (+886.67%)
Mutual labels:  yaml, configuration
parse it
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.
Stars: ✭ 86 (+473.33%)
Mutual labels:  yaml, configuration
Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (+1000%)
Mutual labels:  yaml, configuration
paerser
No description or website provided.
Stars: ✭ 38 (+153.33%)
Mutual labels:  yaml, configuration
Hiyapyco
HiYaPyCo - A Hierarchical Yaml Python Config
Stars: ✭ 58 (+286.67%)
Mutual labels:  yaml, configuration
climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (+33.33%)
Mutual labels:  yaml, configuration
cfg-rs
A Configuration Library for Rust Applications
Stars: ✭ 18 (+20%)
Mutual labels:  yaml, configuration
Fig
A minimalist Go configuration library
Stars: ✭ 142 (+846.67%)
Mutual labels:  yaml, configuration
yaask
Make your yaml configurable with interactive configurations!
Stars: ✭ 15 (+0%)
Mutual labels:  yaml, configuration
Night Config
Powerful java configuration library for toml, yaml, hocon, json and in-memory configurations
Stars: ✭ 93 (+520%)
Mutual labels:  yaml, configuration
Konf
A type-safe cascading configuration library for Kotlin/Java/Android, supporting most configuration formats
Stars: ✭ 225 (+1400%)
Mutual labels:  yaml, configuration
Config Lite
A super simple & flexible & useful config module.
Stars: ✭ 78 (+420%)
Mutual labels:  yaml, configuration
Config
Configuration for Go applications
Stars: ✭ 239 (+1493.33%)
Mutual labels:  yaml, configuration
Zaml
The Final Form of configuration files
Stars: ✭ 45 (+200%)
Mutual labels:  yaml, configuration
Resticprofile
Configuration profiles for restic backup
Stars: ✭ 48 (+220%)
Mutual labels:  yaml, configuration
go-config
Configuration file loader for Go
Stars: ✭ 27 (+80%)
Mutual labels:  yaml, configuration
goodconf
Transparently load variables from environment or JSON/YAML file.
Stars: ✭ 80 (+433.33%)
Mutual labels:  yaml, configuration

Northwoods Config

Build Status StyleCI Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads License SensioLabsInsight

PHP configuration loading library. It is made to enable your application to have different configurations depending on the environment it is running in. For example, your application can have different configurations for testing, development, staging, and production.

It is not recommended to include your staging or production configuration in version control! To support system specific configuration, you may install josegonzalez/dotenv.

Installation

The best way to install and use this package is through composer:

composer require northwoods/config

Usage

There are several different ways to use this package. The most simple is to use the static factory:

use Northwoods\Config\ConfigFactory;

$config = ConfigFactory::make([
    'directory' => __DIR__ . '/config',
]);

Configuration can now be read using a "dot path":

$token = $config->get('app.timezone');

This will load config/app.php and return the timezone key, if it exists.

Optionally, an environment can be set that will add an additional search path:

$config = ConfigFactory::make([
    'directory' => __DIR__ . '/config',
    'environment' => 'dev',
]);

Now when app.timezone is requested, both config/dev/app.php and config/app.php will be searched for the timezone key and the first result will be returned.

Best Practice

It is not recommended to add your staging or production secrets to configuration files that are checked into source control. A better solution is to use an env loader such as josegonzalez/dotenv or vlucas/phpdotenv. This will allow writing PHP configuration files that read from getenv:

return [
    'database' => [
        'password' => getenv('DATABASE_PASSWORD')
    ],
];

Refer the package documentation for how to populate env values from a .env file.

YAML Files

YAML configuration files are supported when the symfony/yaml package is installed:

$config = ConfigFactory::make([
    'directory' => __DIR__ . '/config',
    'environment' => 'dev',
    'type' => 'yaml'
]);

Note: This assumes that all configuration files have a .yaml extension! If you wish to combine PHP and YAML files, create a custom ConfigCollection with ConfigFactory.

Decorators

It is also possible to replace variables in configuration by using the VariableDecorator. This allows you to define variables at runtime without changing configuration:

use Northwoods\Config\Decorator\VariableDecorator;

// Wrap any existing configuration with the decorator
$config = new VariableDecorator($config);
$config->setVariables(['%cacheDir%' => '/tmp']);

Now any configuration value that contains %cacheDir% will have it replaced with /tmp:

return [
    'emails' => '%cacheDir%/emails',
];

Would resolve to /tmp/emails when decorated.

ConfigInterface

All configuration containers must implement ConfigInterface.

get()

Configuration values are accessed using the get($path, $default) method.

The first parameter is a "dot path" to search for in the form file.key.extra. An unlimited depth is supported.

The second parameter is a default value that will be returned if the path cannot be resolved. If the default is not provided null will be used.

// If not defined, $timezone will be null
$timezone = $config->get('app.timezone');

// If not defined, $timezone will be "UTC"
$timezone = $config->get('app.timezone', 'UTC');

set()

Configuration values can also be set at runtime using the set($path, $value) method.

The first parameter is a "dot path" to set in the form file.key.extra. An unlimited depth is supported.

The second parameter is the value to set for the path.

$config->set('app.timezone', 'Europe/Berlin');

Classes

The following classes are part of this package:

  • ConfigFactory - factory for configuration containers
  • ConfigDirectory - container that reads a single directory
  • ConfigCollection - container that reads from an collection of containers
  • Decorator\VariableDecorator - decorator to support variable replacement in configuration values
  • Loader\LoaderFactory - factory for configuration readers
  • Loader\PhpLoader - reader for .php configuration files
  • Loader\YamlLoader - reader for .yaml configuration files

Functions

Getting and setting functionality is implemented by array_path and array_path_set:

use function Northwoods\Config\array_path;
use function Northwoods\Config\array_path_set;

$config = [
    'service' => [
        'uri' => 'http://api.example.com/'
    ],
];

// get a value from an array
$uri = array_path($config, 'service.uri');

// set a value in an array
$config = array_path_set($config, 'service.uri', 'https://api.example.com/v2/')

Examples

See more examples in the examples folder.

PHP Configuration File

Example of a PHP configuration file:

return [
    'timezone' => "America/New_York"
];

YAML Configuration File

Example of a YAML configuration file:

timezone: America/New_York

Original Ownership

This library is a fork of sinergi/config, starting in July 2017, and has changed significantly. Thank you to Gabriel Bull for creating the original package!

License

Config is licensed under The MIT License (MIT).

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].