2017 © Pedro Peláez
 

library config

Config Framework (multiple apps)

image

corex/config

Config Framework (multiple apps)

  • Monday, July 2, 2018
  • by corex
  • Repository
  • 0 Watchers
  • 0 Stars
  • 34 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 55 % Grown

The README.md

Config Component

license build Code Coverage PHPStan Level, (*1)

Breaking changes - this package has been rewritten from scratch to be more strict and flexible to use. Adapters are supported in favor of loaders. Breaking changes can be found in CHANGELOG., (*2)

Getting configuration values works by creating instance of Config::class and use methods to get values. Multiple adapters are supported., (*3)

Example:, (*4)

$adapter = new ArrayAdapter([
    'actor' => [
        'name' => 'James Bond',
    ],
]);

$config = new Config([$adapter]);

$actorName = $config->getString('actor.name');

A method section() is added for convenience when section and rest of key is separated eg. section = "actor" and key = "name"., (*5)

Example of using section():, (*6)

$adapter = new ArrayAdapter([
    'actor' => [
        'name' => 'James Bond',
    ],
]);

$config = new Config([$adapter]);

$actorName = $config->section('actor')->getString('name');

A method getConfigClassObject() is added for getting array and pass to config-class on constructor., (*7)

Example of using getConfigClassObject():, (*8)

class MyConfigClass implements ConfigClassInterface
{
    public function __construct(array $data)
    {
    }

    public static function getSection(): string
    {
        return 'actor';
    }
}

$adapter = new ArrayAdapter([
    'actor' => [
        'name' => 'James Bond',
    ],
]);

$config = new Config([$adapter]);

/** @var MyConfigClass $actorConfig */
$myConfigClass = $config->getConfigClassObject(MyConfigClass::class);

ConfigFactory

A ConfigFactory exists to make it easier to create instances of Config::class., (*9)

Example:, (*10)

$config = (new ConfigFactory())
    ->createWithServerAndEnvAndProjectConfigArrayFileAdapter();

The above example exposes 3 adapters ServerAdapter, EnvAdapter and ProjectConfigArrayFileAdapter., (*11)

From above example, when getting value, the process is following. - ServerAdapter is checked for key. If found, value is returned. - EnvAdapter is checked for key. If found, value is returned. - ProjectConfigArrayFileAdapter is checked for key. If found, value is returned., (*12)

Based on various methods to get values, a null is returned or an exception is thrown., (*13)

More methods exists, but in the situation where they does not fit in, instantiate Config::class with your own order of adapters., (*14)

Keys

Every key must be specified as dot notation e.g. "actor.name"., (*15)

"_" and "-" will not be treated as separators., (*16)

When using ServerAdapter and EnvAdapter, the key will be converted to shoutcase e.g. ACTOR_NAME. This makes it easy to override values in e.g. cloud environments., (*17)

Key object is passed to adapter and multiple methods exists to get key in various formats. Use custom() to build your own., (*18)

Config

Various methods exists on config-class for getting values in correct format. There exists methods for getting specific type with or without null eg. "getString()" or "getStringOrNull()" . Following types are supported: string, int, bool translated-bool, double, array, list., (*19)

For all type methods, value from adapters will be checked if they are correct type, otherwise an exception is thrown., (*20)

"translated-bool" translates/converts following values to boolean., (*21)

Values for true : ['true', true, 'yes', 'on', 1, '1']., (*22)

Values for false : ['false', false, 'no', 'off', 0, '0']., (*23)

"list" means an array where keys are numeric keys 0..n., (*24)

Adapters

It is possible to write your own adapter by extending AbstractAdapter or implementing AdapterInterface., (*25)

Following standard adapters expose arrays as the basis for configuration values., (*26)

ArrayAdapter, (*27)

Serve simple array., (*28)

$adapter = new ArrayAdapter([
    'actor' => [
        'name' => 'James Bond',
    ],
]);

EnvAdapter, (*29)

Serve $_ENV global array., (*30)

$adapter = new EnvAdapter();

ServerAdapter, (*31)

Serve $_SERVER global array., (*32)

$adapter = new ServerAdapter();

ArrayFileAdapter, (*33)

Serve php array files outside project root., (*34)

$adapter = new ArrayFileAdapter(new Filesystem(), '/config-dir-outside-project-root');

ProjectPathArrayFileAdapter - Serve, (*35)

Serve php array files in project root from relative directory., (*36)

$adapter = new ProjectPathArrayFileAdapter(new Filesystem(), 'my-config-dir');

ProjectConfigArrayFileAdapter, (*37)

Serve php array files in project root from relative directory called "config"., (*38)

$adapter = new ProjectConfigArrayFileAdapter(new Filesystem());

Array files.

Example of an array-file., (*39)

Name of file "bond.php"., (*40)

<?php

declare(strict_types=1);

return [
    'actor1' => [
        'firstname' => 'Roger',
        'lastname' => 'Moore'
    ],
    'actor3' => [
        'firstname' => 'Daniel',
        'lastname' => 'Craig'
    ]
];

These type of files can be loaded via ArrayFileAdapter, ProjectPathArrayFileAdapter and ProjectConfigArrayFileAdapter., (*41)

Example of a config-key: "bond.actor1.firstname" which will return "Roger"., (*42)

First section of key "bond" indicates the section and on these adapters the filename., (*43)

The Versions

02/07 2018

dev-master

9999999-dev

Config Framework (multiple apps)

  Sources   Download

MIT

The Requires

 

The Development Requires

config

02/07 2018

dev-develop

dev-develop

Config Framework (multiple apps)

  Sources   Download

MIT

The Requires

 

The Development Requires

config

02/07 2018

1.1.0

1.1.0.0

Config Framework (multiple apps)

  Sources   Download

MIT

The Requires

 

The Development Requires

config

05/05 2018

1.0.2

1.0.2.0

Config Framework (multiple apps)

  Sources   Download

MIT

The Requires

 

The Development Requires

config

03/05 2018

1.0.1

1.0.1.0

Config Framework (multiple apps)

  Sources   Download

MIT

The Requires

 

The Development Requires

config

17/03 2018

1.0.0

1.0.0.0

Config Framework (multiple apps)

  Sources   Download

MIT

The Requires

 

The Development Requires

config