2017-25 © Pedro Peláez
 

library config

Configure your application

image

jasny/config

Configure your application

  • Thursday, July 26, 2018
  • by jasny
  • Repository
  • 4 Watchers
  • 18 Stars
  • 12,024 Installations
  • PHP
  • 2 Dependents
  • 1 Suggesters
  • 3 Forks
  • 0 Open issues
  • 16 Versions
  • 5 % Grown

The README.md

Jasny's Config class

Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight, (*1)

Configure your application. Implements PSR-11 to be used as container., (*2)

Installation

Jasny Config is registred at packagist as jasny/config and can be easily installed using composer., (*3)

composer require jasny/config

Usage

use Jasny\Config;

$config = (new Config())
  ->load('settings.yml')
  ->load('settings.dev.yml', ['optional' => true]);

Loaders

  • Dir
  • Ini
  • Json
  • Yaml
    • Symfony
    • Yaml extension
  • Env
  • DynamoDB

The DelegateLoader can pick a loader based on class name or file extension., (*4)

For every loader; a Jasny\Config\LoadException is thrown if the loader can't load the settings., (*5)

Dir

DirLoader loader will traverse through every file in a directory. It uses the filename (without extension) as key name., (*6)

option type default
recursive bool false Traverse recursively through subdirectories
optional bool false Return empty config is directory doesn't exist
base_path string getcwd() Basepath for relative paths

Ini

IniLoader parses an INI file using parse_ini_file., (*7)

option type default
process_sections bool false Group settings by section name
mode enum INI_SCANNER_NORMAL Mode of parsing options (off = false, etc)
optional bool false Return empty config is file doesn't exist
base_path string getcwd() Basepath for relative paths

Json

JsonLoader parses a JSON file using json_decode., (*8)

option type default
optional bool false Return empty config is file doesn't exist
base_path string getcwd() Basepath for relative paths

Yaml

YamlLoader parses a YAML file using yaml_parse., (*9)

option type default
num int 0 Document to extract from stream (0 is first doc)
callbacks array [] Content handlers for YAML nodes
optional bool false Return empty config is file doesn't exist
base_path string getcwd() Basepath for relative paths

The YamlLoader is used by default if the yaml extension is loaded., (*10)

Symfony\Yaml

YamlSymfonyLoader is an alternative loader for YAML, using the Symfony Yaml component., (*11)

option type default
optional bool false Return empty config is file doesn't exist
base_path string getcwd() Basepath for relative paths

When constructing the loader a Symfony\Component\Yaml\Parser object may be passed., (*12)

use Symfony\Component\Yaml;

$parser = new class() extends Yaml\Parser() {
    public function parse($value, $options = 0) {
        return parent::parseFile($value, $options | Yaml\Yaml::PARSE_CONSTANT); 
    }
}

$yamlLoader = new YamlSymfonyLoader($options, $parser);

Env

EnvLoader maps environment variables to settings. In the mapping the settings key supports dot notation., (*13)

option type default
map array (required) key/value pairs from env name to setting key

Example, (*14)

$map = [
    "APP_SECRET" => "secret",
    "APP_DATABASE_PASSWORD" => "db.password"
];

$config->load('env', ['map' => $map]);

DynamoDB

DynamoDBLoader allows loading settings from an AWS DynamoDB database. It expects all settings to be in a single item. It will not do a table scan. By default the whole item is taken as settings. Alternatively, use the settings_field option to specify a Map field that holds the settings., (*15)

option type default
table string (required) DynamoDB table name
key_field string 'key' Indexed field (typically primary index)
key_value string (required) Value to select item on
settings_field string null within the item that holds the settings

Example, (*16)

$dynamodb = Aws\DynamoDb\DynamoDbClient::factory([
    'region' => 'eu-west-1',
    'version' => '2012-08-10'
]);
 *
$config = new Jasny\Config();

$config->load($dynamodb, [
   'table' => 'config',
   'key_field' => 'key',
   'key_value' => 'myapp'
]);

DelegateLoader

The DelegateLoader is a map with loaders, than can automatically pick a loader based on the file extension or class name., (*17)

By default it holds all the loaders of this library. You may pass an array of loaders as dependency injection., (*18)

use Jasny\Config\Loader;

$loader = new Loader\DelegateLoader([
    'yaml' => new Loader\YamlLoader(['base_path' => __DIR__ . '/config']),
    'json' => new Loader\JsonLoader(['base_path' => __DIR__]) 
]);

$config = new Config([], $loader);
$config->load('composer.json');
$config->load('settings.yml');

Config

The Config object is a dynamic object; settings are added as public properties. It extends the stdClass object., (*19)

Upon construction you can pass settings as associative array or stdClass object., (*20)

$config = new Jasny\Config([
    'foo' => 'bar',
    'db' => [
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'god'
    ]
]);

Optionally you can pass a loader. If your config is only in JSON files, consider passing the JsonLoader. This saves the creation of the DelegateLoader and loaders for all other file types., (*21)

load()

Load new setting from file or other data source and add it to the config., (*22)

load(string|object $source, array $options = []), (*23)

This is a fluent interface, so the method returns $this., (*24)

Example, (*25)

$config = (new Config())
    ->load('composer.json')
    ->load('config/settings.yml')
    ->load($dynamoDB, ['table' => 'config', 'key_value' => 'myapp']);

merge()

Merge one or more Config (or stdClass) objects into the config., (*26)

merge(stdClass $settings, ...), (*27)

This is a fluent interface, so the method returns $this., (*28)

get()

The Config object implements the PSR-11 ContainerInterface. The get method finds a setting in the config by key. The dot notation may be used to get child properties., (*29)

mixed get(string $key), (*30)

If the setting doesn't exist, a Jasny\Config\Exception\NotFoundException is thrown., (*31)

Example, (*32)

$config->get('foo'); // bar
$config->get('db.host'); // localhost

// throws a NotFoundException
$config->get('something_random');

has()

The has method is part of the PSR-11 ContainerInterface. It checks if a setting exists. As with get(), the key supports the dot notation., (*33)

bool has(string $key), (*34)

saveAsScript() / loadFromScript()

The saveAsScript() method create parsable string representation of a variable using var_export and store it as a PHP script., (*35)

As described in the 500x faster caching article, storing the parsed configuration as a PHP script can make loading it much faster., (*36)

The loadFromScript() method can be used to load the settings. It's recommended to use this function rather than just file_exists and include, as checking the file exists would always hit the file system. This function checks the opcode cache first., (*37)

$config = Config::loadFromScript('tmp/settings.php');

if (!isset($config)) {
    $config = (new Config())
        ->load('composer.json')
        ->load('config/settings.yml');

    $config->saveAsScript('tmp/settings.php');
}

The Versions

26/07 2018

dev-master

9999999-dev

Configure your application

  Sources   Download

MIT

The Requires

 

The Development Requires

json configuration ini config yaml mysql db

26/07 2018

v2.0.0

2.0.0.0

Configure your application

  Sources   Download

MIT

The Requires

 

The Development Requires

json configuration ini config yaml mysql db

26/07 2018

v2.x-dev

2.9999999.9999999.9999999-dev

Configure your application

  Sources   Download

MIT

The Requires

 

The Development Requires

json configuration ini config yaml mysql db

26/07 2018

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1

Configure your application

  Sources   Download

MIT

The Requires

 

The Development Requires

json configuration ini config yaml mysql db

21/12 2015

v1.3.2

1.3.2.0

Configure your application. You can load .ini, .json, .yaml or .neon files or a MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json configuration ini config yaml mysql db

17/12 2015

v1.3.1

1.3.1.0

Configure your application. You can load .ini, .json, .yaml or .neon files or a MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json configuration ini config yaml mysql db

10/12 2015

v1.3.0

1.3.0.0

Configure your application. You can load .ini, .json, .yaml or .neon files or a MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json configuration ini config yaml mysql db

10/12 2015

dev-scrutinize-code

dev-scrutinize-code

Configure your application. You can load .ini, .json, .yaml or .neon files or a MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json configuration ini config yaml mysql db

21/08 2015

v1.2.0

1.2.0.0

Configure your application. You can load .ini, .json, .yaml or .neon files or a MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json configuration ini config yaml mysql db

21/08 2015

v1.1.5

1.1.5.0

Configure your application. You can load .ini, .json, .yaml or .neon files or a MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json configuration ini config yaml mysql db

21/08 2015

v1.1.4

1.1.4.0

Configure your application. You can load .ini, .json, .yaml or .neon files or a MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json configuration ini config yaml mysql db

31/07 2014

v1.1.3

1.1.3.0

Configure your application. You can load .ini, .json, .yaml or .neon files or a MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json configuration ini config yaml mysql db

23/04 2014

v1.1.2

1.1.2.0

Configure your application. You can load .ini, .json, .yaml or .neon files or a MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json configuration ini config yaml mysql db

26/12 2012

v1.1.1

1.1.1.0

Configure your application. You can load .ini, .json and .yaml files or MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

json configuration ini config yaml mysql db

21/12 2012

v1.1.0

1.1.0.0

Configure your application. You can load .ini, .json and .yaml files or MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

json configuration ini config yaml mysql db

06/11 2012

v1.0.0

1.0.0.0

Configure your application. You can load .ini, .json and .yaml files or MySQL DB.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

json configuration ini config yaml mysql db