PHP code example of m6w6 / merry

1. Go to this page and download the library: Download m6w6/merry library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

m6w6 / merry example snippets


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());
}

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);
}