https://github.com/activecollab/configrepository
Compose a configuration repository that reads and write options from serveral sources.
https://github.com/activecollab/configrepository
Last synced: about 1 year ago
JSON representation
Compose a configuration repository that reads and write options from serveral sources.
- Host: GitHub
- URL: https://github.com/activecollab/configrepository
- Owner: activecollab
- License: mit
- Created: 2015-12-16T14:10:26.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-12-17T12:46:56.000Z (over 10 years ago)
- Last Synced: 2025-03-30T13:03:28.303Z (about 1 year ago)
- Language: PHP
- Homepage: https://labs.activecollab.com
- Size: 40 KB
- Stars: 1
- Watchers: 7
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Configuration Repository
[](https://travis-ci.org/activecollab/configrepository)
This package provides a way to compose a configuration repository that reads and write options from serveral sources.
Existing adapters:
1. `DotEnvAdapter` - load environment variables from `.env` file (read-only),
2. `EtcdAdapter` - read and write options from and to etcd cluster,
3. `PhpConstantsAdapter` - load options from regular PHP file full of constants. Note that this file is not included, but parsed statically, so you don't need to worry that these constants will be defined in your code.
To write your own adapters, simply implement `ActiveCollab\ConfigRepository\AdapterInterface`.
## Working with data
Repository provides a couple of confenient methods to retrive and set the data:
```php
exists('CONFIG_NAME');
// Get value from first adapter that has it, or return default value if none of the adapters have it
$repository->get('CONFIG_NAME', 'default if not found');
// Get value from first adapter that has it, or throw an exception if it is not found in any adapter
$repository->mustGet('CONFIG_NAME'); // Throws an exception if option is not fpund
// Set value in all adapters where this option exists. Read-only adapters that have this option will throw an exception
$repository->set('CONFIG_NAME', 'value to set');
```
To work with the data from a particular adapter, find it and use the same methods:
```php
getAdapter(PhpConstantsAdapter::class)->exists('CONFIG_NAME');
$repository->getAdapter(PhpConstantsAdapter::class)->get('CONFIG_NAME', 'default if not found');
$repository->getAdapter(PhpConstantsAdapter::class)->mustGet('CONFIG_NAME'); // Throws an exception if option is not fpund
$repository->getAdapter(PhpConstantsAdapter::class)->set('CONFIG_NAME', 'value to set');
```
## Adapter Composition
When constructing a repository instance, you can specify a list of adapters:
```php
new PhpConstantsAdapter(__DIR__ . '/Resources/config.simple.php'),
], new DotEnvAdapter(__DIR__ . '/Resources', '.env'));
$repository->getAdapter(PhpConstantsAdapter::class); // Returns PhpConstantsAdapter instance
$repository->getAdapter('second'); // Returns PhpConstantsAdapter instance
$repository->getAdapter(DotEnvAdapter::class); // Returns DotEnvAdapter instance
```
Adapters can also be added at any time, using `addAdapter()` method:
```php
addAdapter(new DotEnvAdapter($repository->get('DOT_ENV_DIR_PATH')));
```