2017 © Pedro Peláez
 

library config

A configuration loader for PHP

image

awurth/config

A configuration loader for PHP

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 100 % Grown

The README.md

PHP Configuration

SensioLabsInsight Scrutinizer Code Quality, (*1)

Loads your application's configuration from PHP, YAML or JSON files, and stores it in a cache file for performance., (*2)

Uses the Symfony Config component., (*3)

Installation

``` bash $ composer require awurth/config, (*4)


If you want to be able to load YAML files, you have to install the Symfony YAML component too: ``` bash $ composer require symfony/yaml

Usage

Load without cache

``` php // config.php return [ 'database' => [ 'name' => 'database_name', 'user' => 'root', 'password' => 'pass' ] ];, (*5)


``` yaml # config.yml database: name: database_name user: root password: pass

``` json // config.json { "database": { "name": "database_name", "user": "root", "password": "pass" } }, (*6)


``` php $loader = new AWurth\Config\ConfigurationLoader(); $phpConfig = $loader->load('path/to/config.php'); $yamlConfig = $loader->load('path/to/config.yml'); $jsonConfig = $loader->load('path/to/config.json'); // Result: $phpConfig = $yamlConfig = $jsonConfig = [ 'database' => [ 'name' => 'database_name', 'user' => 'root', 'password' => 'pass' ] ];

``` php $debug = 'prod' !== $environment;, (*7)

$loader = new AWurth\Config\ConfigurationLoader('path/to/cache.php', $debug);, (*8)

$config = $loader->load('path/to/config');, (*9)

**The cache file should not be versioned**, especially if you store your database credentials in it.

If the second parameter (`$debug`) is set to `true`, the loader will parse the configuration files and regenerate the cache every time you edit a configuration file (including imports).

If set to `false` (in production), the loader will read the cache file directly if it exists or generate it if not. The configuration won't be reloaded if you modify configuration files, so if you want to reload the cache, you have to delete the cache file.

## Import files from the configuration
You can import other files into a configuration file with the `imports` key. The `imports` array will be removed from the final configuration.
Valid file paths are:
- Relative paths: `../config.yml`
- Absolute paths: `/path/to/config.yml`
- Relative or absolute paths using [placeholders](#using-parameters): `%root_dir%/config/config.%env%.yml`

``` yaml
# config.dev.yml
imports:
    - 'parameters.yml'
    - 'config.yml'

database: ...

``` php // config.dev.php return [ 'imports' => [ 'parameters.yml', // You can import YAML or JSON files from a PHP configuration file 'config.php' ], 'database' => [ ... ] ];, (*10)


``` json // config.dev.json { "imports": [ "parameters.yml", "config.json" ], "database": {} }

Single import

``` yaml imports: 'file.yml', (*11)


### Named imports ##### Without named import ``` yaml # config.yml imports: - security.yml # security.yml security: login_path: /login logout_path: /logout
With named import

``` yaml, (*12)

config.yml

imports: security: security.yml, (*13)

security.yml

login_path: /login logout_path: /logout, (*14)


## Using parameters ``` yaml # parameters.yml parameters: database_name: my_db_name database_user: root database_password: my_password # config.yml imports: - 'parameters.yml' parameters: locale: en database: name: '%database_name%' user: '%database_user%' password: '%database_password%' translator: fallback: '%locale%' logfile: '%root_dir%/cache/%environment%.log' your_custom_config: '%your_custom_param%'

``` php $loader = new AWurth\Config\ConfigurationLoader();, (*15)

$loader->setParameters([ 'root_dir' => '/path/to/project/root', 'environment' => 'dev', 'your_custom_param' => 'your_custom_value' ]);, (*16)

// OR, (*17)

$loader ->setParameter('root_dir', '/path/to/project/root') ->setParameter('environment', 'dev') ->setParameter('your_custom_param', 'your_custom_value') ;, (*18)

$config = $loader->load(DIR.'/config.yml');, (*19)

// Result: $config = [ 'parameters' => [ 'database_name' => 'my_db_name', 'database_user' => 'root', 'database_password' => 'my_password', 'locale' => 'en' ], 'database' => [ 'name' => 'my_db_name', 'user' => 'root', 'password' => 'my_password' ], 'translator' => [ 'fallback' => 'en' ], 'logfile' => '/path/to/project/root/cache/dev.log', 'your_custom_config' => 'your_custom_value' ];, (*20)


## Using PHP constants in YAML files You can use simple PHP constants (like `PHP_INT_MAX`) or class constants (like `Monolog\Logger::DEBUG`) by using the YAML tag `!php/const:` ``` yaml monolog: level: !php/const:Monolog\Logger::ERROR

Constants like __DIR__ and __FILE__ don't work, use parameters instead., (*21)

Options

Imports and parameters keys

``` php $loader = new AWurth\Config\ConfigurationLoader();, (*22)

$loader->getOptions() ->setImportsKey('require') ->setParametersKey('replacements');, (*23)

$config = $loader->load(DIR.'/config.yml');, (*24)


``` yaml # config.yml require: # Does the same as 'imports:' - 'parameters.yml' - 'security.yml' replacements: # Does the same as 'parameters:' locale: en parameters: database_user: root translator: fallback: '%locale%' # Will be replaced by 'en' database: user: '%database_user%' # Will be replaced by null

Disable imports / parameters

Imports and parameters features can be disabled with the setEnableImports and setEnableParameters methods. If your configuration contains an imports key, it won't be removed from the final configuration. Placeholders without corresponding parameters won't be replaced by null., (*25)

``` php $loader = new AWurth\Config\ConfigurationLoader();, (*26)

$loader->getOptions() ->setEnableImports(false) ->setEnableParameters(false);, (*27)


## Add custom file loaders You can add your custom file loaders with the `addLoader` method. The loaders must implement `Symfony\Component\Config\Loader\LoaderInterface`. ``` php $loader = new AWurth\Config\ConfigurationLoader(); $loader->addLoader(new XmlFileLoader()); $loader->addLoader(new CustomFileLoader()); $loader->load('path/to/xml_file.xml'); $loader->load('path/to/file.extension');

TODO

  • Tests

The Versions

30/10 2017

dev-master

9999999-dev

A configuration loader for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

30/10 2017

0.3.0

0.3.0.0

A configuration loader for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

22/10 2017

0.2.0

0.2.0.0

A configuration loader for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

20/10 2017

0.1.0

0.1.0.0

A configuration loader for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires