Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// +----------------------------------------------------------------------+
3
// | PHP Version 4                                                        |
4
// +----------------------------------------------------------------------+
5
// | Copyright (c) 1997-2003 The PHP Group                                |
6
// +----------------------------------------------------------------------+
7
// | This source file is subject to version 2.0 of the PHP license,       |
8
// | that is bundled with this package in the file LICENSE, and is        |
9
// | available at through the world-wide-web at                           |
10
// | http://www.php.net/license/2_02.txt.                                 |
11
// | If you did not receive a copy of the PHP license and are unable to   |
12
// | obtain it through the world-wide-web, please send a note to          |
13
// | license@php.net so we can mail you a copy immediately.               |
14
// +----------------------------------------------------------------------+
15
// | Author: Bertrand Mansion <bmansion@mamasam.com>                      |
16
// +----------------------------------------------------------------------+
17
//
18
// $Id: Config.php 306597 2010-12-24 05:11:09Z aharvey $
19
 
20
require_once('PEAR.php');
21
require_once('Config/Container.php');
22
 
23
$GLOBALS['CONFIG_TYPES'] =
24
        array(
25
            'apache'        => array('Config/Container/Apache.php', 'Config_Container_Apache'),
26
            'genericconf'   => array('Config/Container/GenericConf.php', 'Config_Container_GenericConf'),
27
            'inifile'       => array('Config/Container/IniFile.php', 'Config_Container_IniFile'),
28
            'inicommented'  => array('Config/Container/IniCommented.php', 'Config_Container_IniCommented'),
29
            'phparray'      => array('Config/Container/PHPArray.php', 'Config_Container_PHPArray'),
30
						'phpconstants'	=> array('Config/Container/PHPConstants.php', 'Config_Container_PHPConstants'),
31
            'xml'           => array('Config/Container/XML.php', 'Config_Container_XML')
32
            );
33
 
34
/**
35
* Config
36
*
37
* This class allows for parsing and editing of configuration datasources.
38
* Do not use this class only to read datasources because of the overhead
39
* it creates to keep track of the configuration structure.
40
*
41
* @author   Bertrand Mansion <bmansion@mamasam.com>
42
* @package  Config
43
*/
44
class Config {
45
 
46
    /**
47
    * Datasource
48
    * Can be a file url, a dsn, an object...
49
    * @var mixed
50
    */
51
    var $datasrc;
52
 
53
    /**
54
    * Type of datasource for config
55
    * Ex: IniCommented, Apache...
56
    * @var string
57
    */
58
    var $configType = '';
59
 
60
    /**
61
    * Options for parser
62
    * @var string
63
    */
64
    var $parserOptions = array();
65
 
66
    /**
67
    * Container object
68
    * @var object
69
    */
70
    var $container;
71
 
72
    /**
73
    * Constructor
74
    * Creates a root container
75
    *
76
    * @access public
77
    */
78
    function Config()
79
    {
80
        $this->container = new Config_Container('section', 'root');
81
    } // end constructor
82
 
83
    /**
84
    * Returns true if container is registered
85
    *
86
    * @param    string  $configType  Type of config
87
    * @access public
88
    * @return   bool
89
    */
90
    function isConfigTypeRegistered($configType)
91
    {
92
        return isset($GLOBALS['CONFIG_TYPES'][strtolower($configType)]);
93
    } // end func isConfigTypeRegistered
94
 
95
    /**
96
     * Register a new container
97
     *
98
     * @param    string       $configType  Type of config
99
     * @param    array|false  $configInfo  Array of format:
100
     *           array('path/to/Name.php',
101
     *                 'Config_Container_Class_Name').
102
     *
103
     *           If left false, defaults to:
104
     *           array('Config/Container/$configType.php',
105
     *                 'Config_Container_$configType')
106
     * @access   public
107
     * @static
108
     * @author   Greg Beaver <cellog@users.sourceforge.net>
109
     * @return   true|PEAR_Error  true on success
110
     */
111
    function registerConfigType($configType, $configInfo = false)
112
    {
113
        if (Config::isConfigTypeRegistered($configType)) {
114
            $info = $GLOBALS['CONFIG_TYPES'][strtolower($configType)];
115
            if ($info[0] == $configInfo[0] &&
116
                $info[1] == $configInfo[1]) {
117
                return true;
118
            } else {
119
                return PEAR::raiseError("Config::registerConfigType registration of existing $configType failed.", null, PEAR_ERROR_RETURN);
120
            }
121
        }
122
        if (!is_array($configInfo)) {
123
            // make the normal assumption, that this is a standard config container added in at runtime
124
            $configInfo = array('Config/Container/' . $configType . '.php',
125
                                'Config_Container_'. $configType);
126
        }
127
        $file_exists = @include_once($configInfo[0]);
128
        if ($file_exists) {
129
            if (!class_exists($configInfo[1])) {
130
                return PEAR::raiseError("Config::registerConfigType class '$configInfo[1]' not found in $configInfo[0]", null, PEAR_ERROR_RETURN);
131
            }
132
        } else {
133
            return PEAR::raiseError("Config::registerConfigType file $configInfo[0] not found", null, PEAR_ERROR_RETURN);
134
        }
135
        $GLOBALS['CONFIG_TYPES'][strtolower($configType)] = $configInfo;
136
        return true;
137
    } // end func registerConfigType
138
 
139
    /**
140
    * Returns the root container for this config object
141
    *
142
    * @access public
143
    * @return   object  reference to config's root container object
144
    */
145
    function &getRoot()
146
    {
147
        return $this->container;
148
    } // end func getRoot
149
 
150
    /**
151
    * Sets the content of the root Config_container object.
152
    *
153
    * This method will replace the current child of the root
154
    * Config_Container object by the given object.
155
    *
156
    * @param object  $rootContainer  container to be used as the first child to root
157
    * @access public
158
    * @return   mixed    true on success or PEAR_Error
159
    */
160
    function setRoot(&$rootContainer)
161
    {
162
        if (is_object($rootContainer) && strtolower(get_class($rootContainer)) === 'config_container') {
163
            if ($rootContainer->getName() === 'root' && $rootContainer->getType() === 'section') {
164
                $this->container =& $rootContainer;
165
            } else {
166
                $this->container = new Config_Container('section', 'root');
167
                $this->container->addItem($rootContainer);
168
            }
169
            return true;
170
        } else {
171
            return PEAR::raiseError("Config::setRoot only accepts object of Config_Container type.", null, PEAR_ERROR_RETURN);
172
        }
173
    } // end func setRoot
174
 
175
    /**
176
    * Parses the datasource contents
177
    *
178
    * This method will parse the datasource given and fill the root
179
    * Config_Container object with other Config_Container objects.
180
    *
181
    * @param mixed   $datasrc     Datasource to parse
182
    * @param string  $configType  Type of configuration
183
    * @param array   $options     Options for the parser
184
    * @access public
185
    * @return mixed PEAR_Error on error or Config_Container object
186
    */
187
    function &parseConfig($datasrc, $configType, $options = array())
188
    {
189
        $configType = strtolower($configType);
190
        if (!$this->isConfigTypeRegistered($configType)) {
191
            return PEAR::raiseError("Configuration type '$configType' is not registered in Config::parseConfig.", null, PEAR_ERROR_RETURN);
192
        }
193
        $includeFile = $GLOBALS['CONFIG_TYPES'][$configType][0];
194
        $className = $GLOBALS['CONFIG_TYPES'][$configType][1];
195
        include_once($includeFile);
196
 
197
        $parser = new $className($options);
198
        $error = $parser->parseDatasrc($datasrc, $this);
199
        if ($error !== true) {
200
            return $error;
201
        }
202
        $this->parserOptions = $parser->options;
203
        $this->datasrc = $datasrc;
204
        $this->configType = $configType;
205
        return $this->container;
206
    } // end func &parseConfig
207
 
208
    /**
209
    * Writes the container contents to the datasource.
210
    *
211
    * @param mixed   $datasrc     Datasource to write to
212
    * @param string  $configType  Type of configuration
213
    * @param array   $options     Options for config container
214
    * @access public
215
    * @return mixed PEAR_Error on error or true if ok
216
    */
217
    function writeConfig($datasrc = null, $configType = null, $options = array())
218
    {
219
        if (empty($datasrc)) {
220
            $datasrc = $this->datasrc;
221
        }
222
        if (empty($configType)) {
223
            $configType = $this->configType;
224
        }
225
        if (empty($options)) {
226
            $options = $this->parserOptions;
227
        }
228
        return $this->container->writeDatasrc($datasrc, $configType, $options);
229
    } // end func writeConfig
230
} // end class Config
231
?>