HomeBack home

C1
Gets Configuration data for a specific layer

$Date$

 Table of contents

  Introduction

This example will show you how to get a view of a layer configuration.
You have 2 output formats: phparray or serialize-data.

[Top]

  Synopsis

$registry = &Registry::singleton();
$app = 'sw4p';
$registry->registerApplication($app, [$name = '', [$fileroot = '.']]);
$imp = $registry->importConfig($app, 'data/user1.php');
...
$layer = 'user';
$conf = $registry->getConfigLayer($layer, $app, [$compress = false]);

[Top]

  Parameters

string    $layer         Config layer name ('user', 'system' or 'default')
string    $app           The desired application
bool      $compress      (optional) set it to TRUE if you want serialized data

[Top]

  Output

Without compress-level:
array(1) {
  ["root"]=>
  array(3) {
    ["DB_NAME"]=>
    string(3) "db2"
    ["DB_USER"]=>
    string(4) "root"
    ["DB_PASS"]=>
    string(10) "myrootpass"
  }
}
Serialized data (compress-level):
"a:1:{s:4:"root";a:3:{s:7:"DB_NAME";s:3:"db2";s:7:"DB_USER";s:4:"root";s:7:"DB_PASS";s:10:"myrootpass";}}"

[Top]

  Play example


<?php
/**
 * Example C1 for Config_Registry : gets Configuration data
 * for a specific layer ('user', 'system', 'default')
 * 
 * @version    0.1
 * @author     Laurent Laville <pear@laurent-laville.org>
 * @access     public
 * @package    Config_Registry
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 *
 * $Id$
 */
 
require_once 'Config/Registry.php';
require_once '../handleError.php';

print "<h1>gets configuration data </h1>";
print "<p>You can obtain a serialized or uncompressed array of data.</p>";

$registry =& Config_Registry::singleton();

$app = 'sw4p';
$reg = $registry->registerApplication($app, 'Setup Wizard for PHP', realpath('.'));
handleError($reg);

$imp = $registry->importConfig($app, 'data/user1.php', 'phpArray');
handleError($imp);

$options = array();
$imp = $registry->importConfig($app, 'data/default1.php', 'phpArray', $options, true, 'default');
handleError($imp);

print "<h2>try 1</h2>";
print "<p>Layers allowed are only: 'user', 'system', 'default'</p>";
print "<p>Tying to get a configuration data for an unknown layer gave an error</p>";

$layer = 'users';
$conf = $registry->getConfigLayer($layer, $app);

if (PEAR::isError($conf)) {
    echo'<font color="red"><b>'. $conf->getMessage() .'</b></font>';
}

print "<h2>try 2</h2>";
print "<p>Tying to get a configuration data for a non-registered application gave an error</p>";

$layer = 'user';
$conf = $registry->getConfigLayer($layer, 'horde');

if (PEAR::isError($conf)) {
    echo '<font color="red"><b>'. $conf->getMessage() .'</b></font>';
}

print "<h2>try 3</h2>";
print "<p>gets uncompressed data of initial configuration 'data/user1.php'</p>";

$layer = 'user';
$conf = $registry->getConfigLayer($layer, $app);

if (PEAR::isError($conf)) {
    echo '<font color="red"><b>'. $conf->getMessage() .'</b></font>';
} else {
    print "<pre>";
    var_dump($conf);
    print "</pre>";
}

print "<h2>try 4</h2>";
print "<p>gets serialized(compressed) data of initial configuration 'data/user1.php'</p>";

$layer = 'user';
$conf = $registry->getConfigLayer($layer, $app, true);

if (PEAR::isError($conf)) {
    echo '<font color="red"><b>'. $conf->getMessage() .'</b></font>';
} else {
    print "<pre>";
    var_dump($conf);
    print "</pre>";
}
?>

href:  examples/configs/getConfigLayer.php
Run this script

[Top]