Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Part of the PEAR Config package
4
 *
5
 * PHP Version 4
6
 *
7
 * @category Configuration
8
 * @package  Config
9
 * @author   Phillip Oertel <me@phillipoertel.com>
10
 * @license  http://www.php.net/license PHP License
11
 * @version  SVN: $Id: PHPConstants.php 306571 2010-12-22 06:50:39Z cweiske $
12
 * @link     http://pear.php.net/package/Config
13
 */
14
require_once 'Config/Container.php';
15
 
16
/**
17
 * Config parser for PHP constant files
18
 *
19
 * @category Configuration
20
 * @package  Config
21
 * @author   Phillip Oertel <me@phillipoertel.com>
22
 * @license  http://www.php.net/license PHP License
23
 * @link     http://pear.php.net/package/Config
24
 */
25
class Config_Container_PHPConstants extends Config_Container
26
{
27
    /**
28
     * Valid config options:
29
     * - "lowercase" - boolean - config names are lowercased when reading them
30
     *
31
     * @var  array
32
     */
33
    var $options = array(
34
        'lowercase' => false
35
    );
36
 
37
    /**
38
     * Constructor
39
     *
40
     * @param string $options (optional)Options to be used by renderer
41
     *
42
     * @access public
43
     */
44
    function Config_Container_PHPConstants($options = array())
45
    {
46
        $this->options = array_merge($this->options, $options);
47
    } // end constructor
48
 
49
    /**
50
     * Parses the data of the given configuration file
51
     *
52
     * @param string $datasrc Path to the configuration file
53
     * @param object &$obj    Reference to a config object
54
     *
55
     * @return mixed PEAR_ERROR, if error occurs or true if ok
56
     *
57
     * @access public
58
     */
59
    function &parseDatasrc($datasrc, &$obj)
60
    {
61
        $return = true;
62
 
63
        if (!file_exists($datasrc)) {
64
            return PEAR::raiseError(
65
                'Datasource file does not exist.',
66
                null, PEAR_ERROR_RETURN
67
            );
68
        }
69
 
70
        $fileContent = file_get_contents($datasrc, true);
71
 
72
        if (!$fileContent) {
73
            return PEAR::raiseError(
74
                "File '$datasrc' could not be read.",
75
                null, PEAR_ERROR_RETURN
76
            );
77
        }
78
 
79
        $rows = explode("\n", $fileContent);
80
        for ($i=0, $max=count($rows); $i<$max; $i++) {
81
            $line = $rows[$i];
82
 
83
            //blanks?
84
 
85
            // sections
86
            if (preg_match("/^\/\/\s*$/", $line)) {
87
                preg_match("/^\/\/\s*(.+)$/", $rows[$i+1], $matches);
88
                $obj->container->createSection(trim($matches[1]));
89
                $i += 2;
90
                continue;
91
            }
92
 
93
            // comments
94
            if (preg_match("/^\/\/\s*(.+)$/", $line, $matches)
95
                || preg_match("/^#\s*(.+)$/", $line, $matches)
96
            ) {
97
                $obj->container->createComment(trim($matches[1]));
98
                continue;
99
            }
100
 
101
            // directives
102
            $regex = "/^\s*define\s*\('([A-Z1-9_]+)',\s*'*(.[^\']*)'*\)/";
103
            preg_match($regex, $line, $matches);
104
            if (!empty($matches)) {
105
                $name = trim($matches[1]);
106
                if ($this->options['lowercase']) {
107
                    $name = strtolower($name);
108
                }
109
                $obj->container->createDirective(
110
                    $name, trim($matches[2])
111
                );
112
            }
113
        }
114
 
115
        return $return;
116
 
117
    } // end func parseDatasrc
118
 
119
    /**
120
     * Returns a formatted string of the object
121
     *
122
     * @param object &$obj Container object to be output as string
123
     *
124
     * @return string
125
     *
126
     * @access public
127
     */
128
    function toString(&$obj)
129
    {
130
        $string = '';
131
 
132
        switch ($obj->type)
133
        {
134
        case 'blank':
135
            $string = "\n";
136
            break;
137
 
138
        case 'comment':
139
            $string = '// '.$obj->content."\n";
140
            break;
141
 
142
        case 'directive':
143
            $content = $obj->content;
144
            // don't quote numeric values, true/false and constants
145
            if (is_bool($content)) {
146
                $content = var_export($content, true);
147
            } else if (!is_numeric($content)
148
                && !in_array($content, array('false', 'true'))
149
                && !preg_match('/^[A-Z_]+$/', $content)
150
            ) {
151
                $content = "'" . str_replace("'", '\\\'', $content) . "'";
152
            }
153
            $string = 'define('
154
                . '\'' . strtoupper($obj->name) . '\''
155
                . ', ' . $content . ');'
156
                . chr(10);
157
            break;
158
 
159
        case 'section':
160
            if (!$obj->isRoot()) {
161
                $string  = chr(10);
162
                $string .= '//'.chr(10);
163
                $string .= '// '.$obj->name.chr(10);
164
                $string .= '//'.chr(10);
165
            }
166
            if (count($obj->children) > 0) {
167
                for ($i = 0, $max = count($obj->children); $i < $max; $i++) {
168
                    $string .= $this->toString($obj->getChild($i));
169
                }
170
            }
171
            break;
172
        default:
173
            $string = '';
174
        }
175
        return $string;
176
    } // end func toString
177
 
178
    /**
179
     * Writes the configuration to a file
180
     *
181
     * @param mixed  $datasrc Info on datasource such as path to the file
182
     * @param string &$obj    Configuration object to write
183
     *
184
     * @return mixed PEAR_Error on failure or boolean true if all went well
185
     *
186
     * @access public
187
     */
188
    function writeDatasrc($datasrc, &$obj)
189
    {
190
        $fp = @fopen($datasrc, 'w');
191
        if (!$fp) {
192
            return PEAR::raiseError(
193
                'Cannot open datasource for writing.',
194
                1, PEAR_ERROR_RETURN
195
            );
196
        }
197
 
198
        $string  = "<?php";
199
        $string .= "\n\n";
200
        $string .= '/**' . chr(10);
201
        $string .= ' *' . chr(10);
202
        $string .= ' * AUTOMATICALLY GENERATED CODE - DO NOT EDIT BY HAND' . chr(10);
203
        $string .= ' *' . chr(10);
204
        $string .= '**/' . chr(10);
205
        $string .= $this->toString($obj);
206
        $string .= "\n?>"; // <? : Fix my syntax coloring
207
 
208
        $len = strlen($string);
209
        @flock($fp, LOCK_EX);
210
        @fwrite($fp, $string, $len);
211
        @flock($fp, LOCK_UN);
212
        @fclose($fp);
213
 
214
        // need an error check here
215
 
216
        return true;
217
    } // end func writeDatasrc
218
 
219
 
220
} // end class Config_Container_PHPConstants
221
 
222
?>