Skip to content

Config

The Config library is made to enable your application to have different configurations depending on the environment it is running in. For example, your application can have different configurations for unit tests, development, staging and production.

A good practice would be to not include your production or staging configurations in your version control. To do this, Config supports Dotenv.

Usage

Use the factory to instantiate a Config collection class:

1
2
3
4
5
use Qubus\Config\Collection;

$config = Collection::factory([
    'path' => __DIR__ . '/config'
]);

Optionally, you can also setup the environment. Setting up the environment will merge normal configurations with configurations in the environment directory. For example, if you setup the environment to be prod, the configurations from the directory config/prod/* will be loaded on top of the configurations from the directory config/*. Consider the following example:

1
2
3
4
5
6
use Qubus\Config\Collection;

$config = Collection::factory([
    'path' => __DIR__ . '/config',
    'environment' => 'prod'
]);

Optionally, you can also use dotenv to hide sensible information into a .env file. To do so, specify a directory where the .env file. Like in this example:

1
2
3
4
5
6
7
use Qubus\Config\Collection;

$config = Collection::factory([
    'path' => __DIR__ . '/config',
    'dotenv' => __DIR__,
    'environment' => 'prod'
]);

You can than use the configurations like this:

1
$config->getConfigKey('app.timezone');

Note: In the above example, app is the name of the file minus the .php extension, and timezone is a key found in app.php.

Getter

The configuration getter uses a simple syntax: file_name.array_key.

For example:

1
$config->getConfigKey('app.timezone');

You can optionally set a default value like this:

1
$config->getConfigKey('app.timezone', "America/New_York");

You can use the getter to access multidimensional arrays in your configurations:

1
$config->getConfigKey('database.connections.default.host');

Setter

Alternatively, you can set configurations from your application code:

1
$config->setConfigKey('app.timezone', "Europe/Berlin");

You can set entire arrays of configurations:

1
2
3
4
5
6
$config->setConfigKey('database', [
    'host' => "localhost",
    'dbname' => "my_database",
    'user' => "my_user",
    'password' => "my_password"
]);

Invokable Classes

Sometimes in your own projects you may want to use config classes for storing application settings, without needing file I/O. You can do this by creating invokable classes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use Qubus\Config\Parser;

class SimpleConfig
{
    public function __invoke()
    {
        return [
            'database' => [
                'host' => 'localhost',
                'port'    => 443,
            ],
            'application' => [
                'name'   => 'configuration',
                'secret' => 's3cr3t',
            ],
        ];
    }
}

$config = new SimpleConfig();

[$file, $key, $sub] = Parser::getKey('file.application.secret');

echo Parser::getValue($config(), $key, $sub); // will print out `s3cr3t`

Decorator

It is also possible to replace configuration variables by using the VariableDecorator. This allows you to define variables at runtime without changing configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
use Qubus\Config\Collection;
use Qubus\Config\VariableDecorator;

// app.php contains the following:
# return [
#     'timezone' => 'America/Denver',
#     'test_dir' => '%vendorDir%/testdev1',
# ];

$config = Collection::factory([
    'path' =>  __DIR__ . "/files",
    'environment' => 'production',
    'dotenv' => __DIR__ . "/files"
]);

$decorator = new VariableDecorator($config);
$decorator->setVariables(['%vendorDir%' => __DIR__ . "/files"]);

echo $decorator->getConfigKey('app.test_dir'); // will print out `/some/directory/to/files/testdev1`

PHP Configuration File

Example of a PHP configuration file:

1
2
3
return [
    'timezone' => "America/New_York"
];

Yaml Configuration File

Example of a YAML configuration file:

1
timezone: America/New_York

Dotenv

Example of using Dotenv in a PHP configuration file:

use function Qubus\Config\Helpers\env;

1
2
3
return [
    'timezone' => env('TIMEZONE', "Denver")
];

And in the .env file:

1
TIMEZONE="America/New_York"