Skip to content

Commit db4d0e5

Browse files
committed
[config] Update readme
1 parent 466722e commit db4d0e5

File tree

1 file changed

+83
-2
lines changed

1 file changed

+83
-2
lines changed

README.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,95 @@ It also promises to be immutable after first read.
2626

2727
## Usage
2828

29+
### Load config from file
30+
31+
We support writing your config in php files
32+
33+
Example:
2934
```php
35+
<?php
36+
37+
// config.ph
38+
39+
return [
40+
'config-key' => 'value',
41+
'nested' => [
42+
'key' => 'nested-value'
43+
'bool-key' => false,
44+
],
45+
];
46+
```
3047

31-
$config = new \Stefna\Config\FileConfig('path-to-php-config.php');
48+
```php
49+
$config = new \Stefna\Config\FileConfig('path-to-php/config.php');
3250
// config file is not read until it's needed
3351

34-
$config->getBool('boolKey');
52+
$config->getBool('nested.bool-key') === false;
53+
$config->getString('config-key') === 'value';
3554
```
3655

56+
### Load multiple files into config
57+
58+
```php
59+
<?php
60+
61+
// common.ph
62+
63+
return [
64+
'config-key' => 'value',
65+
'nested' => [
66+
'key' => 'nested-value'
67+
'bool-key' => false,
68+
],
69+
];
70+
```
71+
```php
72+
<?php
73+
74+
// production.ph
75+
76+
return [
77+
'config-key' => 'production-value',
78+
'nested' => [
79+
'extra-key' => 42,
80+
],
81+
];
82+
```
83+
84+
```php
85+
$config = new \Stefna\Config\FileCollectionConfig('path-to-php/');
86+
$config->addFile('common.php');
87+
$config->addFile('production.php');
88+
89+
// config files is not read until it's needed
90+
91+
$config->getInt('nested.extra-key') === 42;
92+
$config->getString('config-key') === 'product-value';
93+
```
94+
95+
### Mutable config
96+
97+
We do provide a mutable config that allows you to override values in the "root" config
98+
this is meant to be used when testing applications but still allow the "root" configuration to stay immutable
99+
100+
101+
```php
102+
$rootConfig = new \Stefna\Config\FileCollectionConfig('path-to-php/');
103+
$rootConfig->addFile('common.php');
104+
$rootConfig->addFile('production.php');
105+
106+
$config = new \Stefna\Config\MutableConfig($rootConfig);
107+
108+
$config->setConfigValue('config-key', 'overridden-value');
109+
110+
$config->getString('config-key') === 'overridden-value';
111+
112+
$config->resetConfigValue('config-key');
113+
114+
$config->getString('config-key') === 'production-value';
115+
```
116+
117+
37118
## Contribute
38119

39120
We are always happy to receive bug/security reports and bug/security fixes

0 commit comments

Comments
 (0)