| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Config.php example
|
|
|
4 |
*
|
|
|
5 |
* Lots of different manipulations to show Config features.
|
|
|
6 |
*
|
|
|
7 |
* @author Bertrand Mansion <bmansion@mamasam.com>
|
|
|
8 |
* @package Config
|
|
|
9 |
*/
|
|
|
10 |
// $Id: IniFromScratch.php 120857 2003-03-21 18:04:21Z mansion $
|
|
|
11 |
|
|
|
12 |
require_once('Config.php');
|
|
|
13 |
|
|
|
14 |
// Creates a PHPArray config with attributes, from scratch
|
|
|
15 |
|
|
|
16 |
$dsn = array('type' => 'mysql',
|
|
|
17 |
'host' => 'localhost',
|
|
|
18 |
'user' => 'mamasam',
|
|
|
19 |
'pass' => 'foobar');
|
|
|
20 |
|
|
|
21 |
$c = new Config_Container('section', 'root');
|
|
|
22 |
$c->createComment('DB Config');
|
|
|
23 |
$db =& $c->createSection('DB', $dsn);
|
|
|
24 |
$fields =& $db->createSection('fields');
|
|
|
25 |
$fields->createDirective('username', 'USERNAME', array('type' => 'varchar', 'size' => 32));
|
|
|
26 |
$fields->createDirective('password', 'PASSWD', array('type' => 'varchar', 'size' => 32));
|
|
|
27 |
$c->createBlank();
|
|
|
28 |
$c->createComment('Support config');
|
|
|
29 |
$c->createDirective('support', 'See my wishlist...');
|
|
|
30 |
|
|
|
31 |
echo '<pre>'. $c->toString('phparray') .'</pre>';
|
|
|
32 |
unset($c);
|
|
|
33 |
|
|
|
34 |
// Parses and writes an existing php array $conf
|
|
|
35 |
|
|
|
36 |
$conf['storage']['driver'] = 'sql';
|
|
|
37 |
$conf['storage']['params']['phptype'] = 'mysql';
|
|
|
38 |
$conf['storage']['params']['hostspec'] = 'localhost';
|
|
|
39 |
$conf['storage']['params']['username'] = 'mamasam';
|
|
|
40 |
$conf['storage']['params']['password'] = 'foobar';
|
|
|
41 |
$conf['menu']['apps'] = array('imp', 'turba');
|
|
|
42 |
$conf['stdcontent']['para'][0] = 'This is really cool !';
|
|
|
43 |
$conf['stdcontent']['para'][1] = 'It just rocks...';
|
|
|
44 |
|
|
|
45 |
$c = new Config();
|
|
|
46 |
$root =& $c->parseConfig($conf, 'phparray');
|
|
|
47 |
|
|
|
48 |
$storage =& $root->getItem('section', 'storage');
|
|
|
49 |
$storage->removeItem();
|
|
|
50 |
$root->addItem($storage);
|
|
|
51 |
|
|
|
52 |
echo '<pre>'. $root->toString('phparray', array('name' => 'test')) .'</pre>';
|
|
|
53 |
|
|
|
54 |
if ($c->writeConfig('/tmp/Config_Test.php', 'phparray', array('name' => 'test')) === true) {
|
|
|
55 |
echo 'Config written into /tmp/Config_Test.php';
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
// Making a php ini file with $storage only
|
|
|
59 |
|
|
|
60 |
$ini = new Config();
|
|
|
61 |
$iniRoot =& $ini->getRoot();
|
|
|
62 |
$iniRoot->addItem($storage);
|
|
|
63 |
|
|
|
64 |
$comment =& new Config_Container('comment', null, 'This is the php ini version of storage');
|
|
|
65 |
$iniRoot->addItem($comment, 'top');
|
|
|
66 |
$iniRoot->createBlank('after', $comment);
|
|
|
67 |
echo '<pre>'. $iniRoot->toString('inicommented') .'</pre>';
|
|
|
68 |
|
|
|
69 |
// Gonna make an array with it
|
|
|
70 |
|
|
|
71 |
echo '<pre>'; var_dump($iniRoot->toArray()); echo '</pre>';
|
|
|
72 |
|
|
|
73 |
// Now, I'll parse you php.ini file and make it a php array
|
|
|
74 |
|
|
|
75 |
$phpIni = new Config();
|
|
|
76 |
$phpIni->parseConfig('/usr/local/lib/php.ini', 'inifile');
|
|
|
77 |
$root =& $phpIni->getRoot();
|
|
|
78 |
echo '<pre>'.$root->toString('phparray', array('name' => 'php_ini')).'</pre>';
|
|
|
79 |
|
|
|
80 |
?>
|