Download the PHP package elbucho/config without Composer
On this page you can find all versions of the php package elbucho/config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package config
elbucho/config
This project provides an object-oriented configuration system for all of your config files. This includes support for the following file types:
- INI files
- PHP files (return array())
- JSON files
- XML files
- YAML files
Usage
The Config class is instantiated with an array of key => value pairs. Nested keys are instantiated as separate Config classes.
Retrieving values
Keys can be accessed either through the magic __get() method, or via the get() method. The get() method supports dotted notation (see examples below).
$config = new Elbucho\Config\Config(array('foo' => array('bar' => 1)));
$config->foo->bar == 1
$config->get('foo.bar') == 1
$config->get('foo')->bar == 1
$config->get('foo')->get('bar') == 1
When using the magic __get() method, if a key does not exist, the Config class will return false
.
When using the get() method, you can specify the return value if the key does not exist.
$config->foo->bar1 == false
$config->get('foo.bar1', 2) == 2
$config->foo->get('bar1', 'missing') == 'missing'
Setting values
You can set values by using either the magic __set() method, or by using the set() method:
$config->foo->bar = 2
$config->set('foo.bar', 2)
$config->foo->set('bar', 2)
If you use the set() method, and specify a path that doesn't currently exist, that path will be created:
$config->get('foo.bar.foobar') == false
$config->set('foo.bar.foobar.asdf', 'fdsa')
Un-setting values
You can unset values either by using the magic __unset() method, or by using remove():
unset($config->foo->bar)
$config->remove('foo.bar')
$config->foo->remove('bar')
Finding whether a key exists
You can determine whether a key has been set via either the magic __isset() method, or with exists():
isset($config->foo->bar)
$config->exists('foo.bar')
$config->foo->exists('bar')
Returning as an array
You can return the Config object as an array by using the toArray() method:
$config->toArray() == ['foo' => ['bar' => 1]]
Appending values
If you wish to merge two Config classes, you can use the append function:
$config1->toArray() == ['foo' => ['bar' => 1, 'baz' => 2]]
File loaders
You can load a configuration file into a Config object via the use of one of the Loader classes:
File Type | Loader Class |
---|---|
INI | Elbucho\Config\Loader\File\IniFileLoader |
JSON | Elbucho\Config\Loader\File\JsonFileLoader |
PHP | Elbucho\Config\Loader\File\PhpFileLoader |
XML | Elbucho\Config\Loader\File\XmlFileLoader |
YAML/YML | Elbucho\Config\Loader\File\YamlFileLoader |
To load a file and create a Config-compatible array from the values stored therein, do this:
Directory Loader
You can also load all of the files in a given directory with the directory loader:
File names (minus the extensions) will be listed as keys in the $config object. For example, let's assume this file exists in your config directory as "database.yml":
When you run the code above to import this file, here is how your config object will look:
Environment Overwriting
In many cases, you will want to have a set of config files that are applicable for all environments, and will want to overwrite specific keys with environment-specific values.
For example, let's say that we have a config folder format like this:
In this example, we want the keys that are common to all environments in /config/database.yml
and /config/framework.yml
loaded, but we want to overwrite certain ones with the values
in /config/environment/live
since we're in the live environment. Here's how you would do that:
Extending the class
Each loader must conform to the Elbucho\Config\LoaderInterface
interface. If you would like
to write a custom file loader, it should throw an Elbucho\Config\InvalidFileException
exception
when encountering issues opening or parsing the file.
New loaders can also be registered with the DirectoryLoader via the registerLoader() method: