Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/m6w6/merry
Merry container and config
https://github.com/m6w6/merry
Last synced: 4 days ago
JSON representation
Merry container and config
- Host: GitHub
- URL: https://github.com/m6w6/merry
- Owner: m6w6
- License: bsd-2-clause
- Created: 2014-08-13T18:28:14.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-03-31T11:07:07.000Z (over 9 years ago)
- Last Synced: 2024-05-02T07:33:22.390Z (7 months ago)
- Language: PHP
- Homepage:
- Size: 141 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# merry\\Config
A merry configuration container. [![Build Status](https://travis-ci.org/m6w6/merry.svg)](https://travis-ci.org/m6w6/merry)
Example:
```php
use merry\Config;$config = new Config([
"db" => [
"dsn" => "user=mike",
"flags" => pq\Connection::PERSISTENT
],
"cache" => [
"pid" => "cluster1",
"hosts" => ["10.0.1.1", "10.0.1.2", "10.0.1.3"]
]
]);printf("Using database: '%s'\n", $config->db->dsn);
printf("Using cache cluster: '%s'\n", $config->cache->pid);$config->apply([
"db" => function($conf) {
return new pq\Connection($conf->dsn, $conf->flags);
},
"cache" => function($conf) {
$cache = new Memcached($conf->pid);
foreach ($conf->{$conf->pid}->hosts as $host) {
$cache->addServer($host);
}
return $cache;
}
]);extract($config->toArray());
if (!($q1 = $cache->get("q1"))) {
$result = $db->exec("SELECT 1");
$cache->set("q1", $q1 = $result->fetchAll());
}
```Another example:
```php
use merry\Config;$array = parse_ini_string('
[localhost]
db.dsn = "user=mike"
db.flags = 2 ;pq\Connection::PERSISTENT
cache.pid = "cluster1"
cache.cluster1.hosts[] = "10.0.1.1"
cache.cluster1.hosts[] = "10.0.1.2"
cache.cluster1.hosts[] = "10.0.1.3"
[production : localhost]
db.dsn = "user=app"
');$config = new Config($array, getenv("APPLICATION_ENV"));
$flags = \RecursiveTreeIterator::BYPASS_CURRENT;
foreach (new \RecursiveTreeIterator($config, $flags) as $key => $val ) {
printf("%s: %s\n", $key, ($val instanceof Config) ? "" : $val);
}
```Output:
```
|-db:
| |-dsn: user=app
| \-flags: 2
\-cache:
|-pid: cluster1
\-cluster1:
\-hosts:
|-0: 10.0.1.1
|-1: 10.0.1.2
\-2: 10.0.1.3
```