Download the PHP package corex/config without Composer
On this page you can find all versions of the php package corex/config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package config
Short Description Config Component with adapter support (array, array-files, env, server, etc...)
License MIT
Informations about the package config
Config Component
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.
Getting configuration values works by creating instance of Config::class and use methods to get values. Multiple adapters are supported.
Example:
A method section()
is added for convenience when section and rest of key is separated eg. section = "actor" and key = "name".
Example of using section():
A method getConfigClassObject()
is added for getting array and pass to config-class on constructor.
Example of using getConfigClassObject():
ConfigFactory
A ConfigFactory exists to make it easier to create instances of Config::class.
Example:
The above example exposes 3 adapters ServerAdapter, EnvAdapter and ProjectConfigArrayFileAdapter.
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.
Based on various methods to get values, a null is returned or an exception is thrown.
More methods exists, but in the situation where they does not fit in, instantiate Config::class with your own order of adapters.
Keys
Every key must be specified as dot notation e.g. "actor.name".
"_" and "-" will not be treated as separators.
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.
Key object is passed to adapter and multiple methods exists to get key in various formats. Use custom()
to build your own.
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.
For all type methods, value from adapters will be checked if they are correct type, otherwise an exception is thrown.
"translated-bool" translates/converts following values to boolean.
Values for true : ['true', true, 'yes', 'on', 1, '1'].
Values for false : ['false', false, 'no', 'off', 0, '0'].
"list" means an array where keys are numeric keys 0..n.
Adapters
It is possible to write your own adapter by extending AbstractAdapter or implementing AdapterInterface.
Following standard adapters expose arrays as the basis for configuration values.
ArrayAdapter
Serve simple array.
EnvAdapter
Serve $_ENV global array.
ServerAdapter
Serve $_SERVER global array.
ArrayFileAdapter
Serve php array files outside project root.
ProjectPathArrayFileAdapter - Serve
Serve php array files in project root from relative directory.
ProjectConfigArrayFileAdapter
Serve php array files in project root from relative directory called "config".
Array files.
Example of an array-file.
Name of file "bond.php".
These type of files can be loaded via ArrayFileAdapter, ProjectPathArrayFileAdapter and ProjectConfigArrayFileAdapter.
Example of a config-key: "bond.actor1.firstname" which will return "Roger".
First section of key "bond" indicates the section and on these adapters the filename.