initial checkin
[m6w6/merry] / README.md
1 = merry\\Config
2
3 A merry configuration container.
4
5 Example:
6 ```php
7 use merry\Config;
8
9 $config = new Config([
10 "db" => [
11 "dsn" => "user=mike",
12 "flags" => pq\Connection::PERSISTENT
13 ],
14 "cache" => [
15 "pid" => "cluster1",
16 "hosts" => ["10.0.1.1", "10.0.1.2", "10.0.1.3"]
17 ]
18 ]);
19
20 printf("Using database: '%s'\n", $config->db->dsn);
21 printf("Using cache cluster: '%s'\n", $config->cache->pid);
22
23 $config->apply([
24 "db" => function($conf) {
25 return new pq\Connection($conf->dsn, $conf->flags);
26 },
27 "cache" => function($conf) {
28 $cache = new Memcached($conf->pid);
29 foreach ($conf->{$conf->pid}->hosts as $host) {
30 $cache->addServer($host);
31 }
32 return $cache;
33 }
34 ]);
35
36
37 extract($config->toArray());
38
39 if (!($q1 = $cache->get("q1"))) {
40 $result = $db->exec("SELECT 1");
41 $cache->set("q1", $q1 = $result->fetchAll());
42 }
43 ```
44
45 Another example:
46 ```php
47 use merry\Config;
48
49 $array = parse_ini_string('
50 [localhost]
51 db.dsn = "user=mike"
52 db.flags = 2 ;pq\Connection::PERSISTENT
53 cache.pid = "cluster1"
54 cache.cluster1.hosts[] = "10.0.1.1"
55 cache.cluster1.hosts[] = "10.0.1.2"
56 cache.cluster1.hosts[] = "10.0.1.3"
57 [production : localhost]
58 db.dsn = "user=app"
59 ');
60
61 $config = new Config($array, getenv("APPLICATION_ENV"));
62 $flags = \RecursiveTreeIterator::BYPASS_CURRENT;
63 foreach (new \RecursiveTreeIterator($config, $flags) as $key => $val ) {
64 printf("%s: %s\n", $key, ($val instanceof Config) ? "" : $val);
65 }
66 ```
67
68 Output:
69 ```
70 |-db:
71 | |-dsn: user=app
72 | \-flags: 2
73 \-cache:
74 |-pid: cluster1
75 \-cluster1:
76 \-hosts:
77 |-0: 10.0.1.1
78 |-1: 10.0.1.2
79 \-2: 10.0.1.3
80 ```