Smutty
MVC Framework
View Code: Smutty_Config
Browse: All · Classes · Plugins
1
<?php
2
3
/**
4
* this class creates allows access to config files in
5
* an easy way. normally for Smutty a singleton instance
6
* is maintained (accessed through getInstance())
7
*
8
* @see getInstance()
9
*
10
*/
11
12
class Smutty_Config extends Smutty_Object {
13
14
/** data store */
15
private $data;
16
17
/** the singleton */
18
private static $instance = null;
19
20
/** get cache */
21
private $getCache;
22
23
/**
24
* constructor. creates a new config class.
25
*
26
* @return nothing
27
*
28
*/
29
31
$this->data = array();
32
$this->getCache = array();
33
}
34
35
/**
36
* loads the config with data from the specified file. this will
37
* not overwrite any data, so it can be called multiple times
38
* to load data from multiple config files.
39
*
40
* @param String $path path to file to load
41
* @return nothing
42
*
43
*/
44
46
if ( !$f = @fopen($path,'r') )
47
Smutty_Error::fatal( 'config file not found: ' . $path, 'SmuttyConfig' );
48
while ( $line = fgets($f) )
49
$this->loadLine( $line );
50
fclose( $f );
51
}
52
53
/**
54
* this function loads the default config files
55
*
56
* @return nothing
57
*
58
*/
59
61
62
$this->loadConfig( 'library/smutty.cfg' );
63
$this->loadConfig( 'application/app.cfg' );
64
65
}
66
67
/**
68
* processes a single line of configuration info
69
*
70
* @param String $line the line to parse
71
* @return nothing
72
*
73
*/
74
76
if ( $line[0] == '#' )
77
return; // skip comments
78
// break the line into name and value
79
preg_match( '/^([\w_\.]*\*{0,1})\s*=\s*([\d\w\/_ ,]*)\s*/', $line, $matches );
80
if ( $matches ) {
81
$name = trim( $matches[1] );
82
$value = trim( $matches[2] );
83
if ( $name && $value )
84
$this->data[ $name ] = $value;
85
}
86
}
87
88
/**
89
* looks for a config setting matching the given
90
* name. you can use a "*" as a wildcard
91
*
92
* @param String $name setting name
93
* @return String the config value
94
*
95
*/
96
98
99
// check for an exact match
100
if ( isset($this->data[$name]) )
101
return $this->data[$name];
102
103
// cache hit?
104
elseif ( isset($this->getCache[$name]) )
105
return $this->getCache[$name];
106
107
// are we looking to for multiple matches?
108
elseif ( strpos($name,'*') ) {
109
$vars = array();
110
$regexp = $this->getWildcardRegExp( $name );
111
foreach ( $this->data as $key => $value )
112
if ( preg_match($regexp,$key) )
113
$vars[ $key ] = $value;
114
$this->getCache[$name] = $vars;
115
return $vars;
116
}
117
118
// lastly, check for wildcards in the spec
119
// eg. route.ctlr.prefix* = whatever
120
else foreach ( $this->data as $key => $value )
121
if ( strpos($key,'*') ) {
122
$regexp = $this->getWildcardRegExp( $key );
123
if ( preg_match($regexp,$name) ) {
124
$this->getCache[$name] = $value;
125
return $value;
126
}
127
}
128
129
$this->getCache[$name] = '';
130
return '';
131
132
}
133
134
/**
135
* converts a string into a regexp where wildcards (*'s)
136
* will be matched in it.
137
*
138
* @param String $name the string to convert
139
* @return String the regexp
140
*
141
*/
142
144
145
$regexp = '/^' . str_replace('.','\\.',$name) . '$/';
146
$regexp = str_replace( '*','.*', $regexp );
147
148
return $regexp;
149
150
}
151
152
/**
153
* returns the singleton for this object
154
*
155
* @return Smutty_Config the singleton
156
*
157
*/
158
159
public static function &getInstance() {
160
if ( self::$instance == null )
161
self::$instance = new Smutty_Config();
162
return self::$instance;
163
}
164
165
}
166
167
?>
The code shown here is the code that is currently running this site. If you want to view the latest SVN version of the code then go to the Subversion repository.