Subversion-Projekte lars-tiefland.niewerth

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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