HomeBack home

C3
Import Configuration Data

$Date$

 Table of contents

  Introduction

This example introduce the main facility to initialize configuration data of an application. You may choice source and format of data (see PEAR::Config for containers list and options).

[Top]

  Synopsis

$registry = &Registry::singleton();
$app = 'sw4p';
$registry->registerApplication($app);

$imp = $registry->importConfig($app, [$name = null, [$type = 'phpArray', [$options = array(),
                                     [$override = true, [$layer = 'user']]]]]
                               );

[Top]

  Parameters

string    $app           The target application
string    $name          (optional) Resource to read from
string    $type          (optional) Type of data source (phpArray, Ini, XML, ...)
array     $options       (optional) Options for the parser
bool      $override      (optional) Whether to overwrite existing data 
string    $layer         (optional) Config layer to insert data into

[Top]

  Play example


<?php
/**
 * Example C3 for Config_Registry : import Config
 * 
 * @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>Import configuration data from an external source </h1>";

$registry =& Config_Registry::singleton();

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

$imp = $registry->importConfig($app);

print "<h2>try 1</h2>";
print "<p>The external resource should be a valid config container.";
print " See PEAR::Config package for list</p>";

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

print "<h2>try 2</h2>";
print "<p>source of import: php array </p>";

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

echo '<font color="blue"><b>"'.$layer.'" data comes from file "'.$data2 .'" ('.$type.')<br/></b></font>';

$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>";
}
?>

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

[Top]