This repository has been archived by the owner on May 23, 2020. It is now read-only.
/
Config.php
311 lines (282 loc) · 6.84 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/*
* Class: Config
*
* Configuration class that parses the applications configuration file.
* Imlemented as a singelton (<getInstance>).
*/
class Config
{
/*
* Constructor: __construct
*
* Load the configuration file for the application.
*/
protected function __construct()
{
$this->application_path = getcwd() . "/../application";
// Read user settings from config file.
$this->settings = parse_ini_file($this->application_path .
"/config/config.ini", true);
// Read routes from routes.php.
require_once($this->application_path . "/config/routes.php");
$this->routes = $routes;
}
/*
* Method: get
*
* Get settings from the configuration file. Also see
* <getValueWithDefault>.
*
* Parameters:
* section - The section to use.
* key - The setting keyword to look up.
*
* Returns:
* Returns the configuration value if found, null otherwise.
*/
public function get($section, $key)
{
return $this->getValueWithDefault($section, $key, null);
}
/*
* Method: getBasepath
*
* The basepath is the URL-location of this application.
*
* Parameters:
* absolute - If this is set to true the absolute url-basepath will be returned
* (e.g. _http://example.com/wickedapp/_), else just the relative url
* will be returned
*
* Returns:
* The basepath to the application.
*/
public function getBasepath($absolute = false)
{
$basepath = trim($this->getValueWithDefault('application', 'basepath', ''), '/');
if ($absolute) {
return trim('http'.(empty($_SERVER['HTTPS']) ? '': 's').
'://'.$_SERVER['HTTP_HOST'].'/'.$basepath, '/');
} else if (!empty($basepath)) {
return '/' . $basepath . '/';
} else {
return '/';
}
}
/*
* Method: getCacheDir
*
* Get the configured directory for storing cache files, defaults to
* _application/cache_.
*
* Returns:
* Full path to the cache directory.
*/
public function getCacheDir()
{
// FIXME: Hardcoded application path.
$default = $this->application_path . "/cache";
return $this->getValueWithDefault('application', 'cache_dir', $default);
}
/*
* Method: getInstance
*
* Gets the single instance of this class (it's a singelton).
*
* Returns:
* The only instance of this class.
*/
public static function getInstance()
{
if (!is_object(self::$INSTANCE)) {
self::$INSTANCE = new self();
}
return self::$INSTANCE;
}
/*
* Method: useCache
*
* Returns:
* True if config is turned on in the config-file otherwise false.
*/
public function useCache()
{
return $this->getValueWithDefault('application', 'use_cache', false);
}
/*
* Method: useImageCache
*
* Returns:
* True if config is turned on in the config-file otherwise false.
*/
public function useImageCache()
{
return $this->getValueWithDefault('application', 'use_image_cache', true);
}
public function getApplicationPath()
{
return $this->application_path;
}
public function getUrlCaseSensitive()
{
return $this->getValueWithDefault('application', 'url_case_sensitive', true);
}
/*
* Method: getDefaultController
*
* Get the default controller for the application, e.g. when no controller
* is specified use this one.
*
* Returns:
* The default controller as specified in the configuration file, or
* null if not specified.
*/
public function getDefaultController()
{
return $this->getValueWithDefault('application', 'default_controller',
null);
}
/*
* Method: getDefaultAction
*
* Get the default action to use when no action is specified.
*
* Returns:
* The default action as specified in the configuration file, defaults
* to 'index' if not specified in the configuration file.
*/
public function getDefaultAction()
{
return $this->getValueWithDefault('application', 'default_action',
'index');
}
/*
* Method: getErrorController
*
* FIXME: Reimund?
*/
public function getErrorController()
{
return false;
}
/*
* Method: getDatabaseDriver
*
* Returns:
* The specified database driver (e.g. mysql) from the configuration
* file.
*/
public function getDatabaseDriver()
{
return $this->getValueWithDefault('database', 'driver', 'mysql');
}
/*
* Method: getDatabaseName
*
* Returns:
* The specified database database name from the configuration file.
*/
public function getDatabaseName()
{
return $this->getValueWithDefault('database', 'database_name', 'app');
}
/*
* Method: getDatabaseUsername
*
* Returns:
* The specified database username from the configuration file (defaults
* to 'root').
*/
public function getDatabaseUsername()
{
return $this->getValueWithDefault('database', 'username', 'root');
}
/*
* Method: getDatabasePassword
*
* Returns:
* The specified database password from the configuration file (defaults
* to '' (empty)).
*/
public function getDatabasePassword()
{
return $this->getValueWithDefault('database', 'password', '');
}
/*
* Method: getDatabaseHostname
*
* Returns:
* The specified database hostname from the configuration file (defaults
* to 'localhost').
*/
public function getDatabaseHostname()
{
return $this->getValueWithDefault('database', 'hostname', 'localhost');
}
/*
* Method: getDatabaseFile
*
* Returns:
* The specified database filename from the configuration file (defaults
* to '' (empty)).
*/
public function getDatabaseFile()
{
return $this->getValueWithDefault('database', 'file', '');
}
/*
* Method getRoutes
*
* Gets the routes specified in routes.php file in the config directory.
*
* Returns:
* An array with all routes that are specified in the routes.php
* configuration file.
*/
public function getRoutes()
{
return $this->routes;
}
/*
* Method: shouldDebug
*
* Whether to go into debug mode or not.
*
* Returns:
* As specified in the configuration file defaults to true.
*/
public function shouldDebug()
{
return $this->getValueWithDefault('application', 'debug', true);
}
private static $INSTANCE = null;
private $settings;
private $routes;
/*
* Method: getValueWithDefault
*
* Get a setting from the configuration but fallback on a default value if
* the setting can not be found in the configuration file.
*
* Parameters:
* section - The section to look under.
* name - The keyword to look up.
* default - The default value.
*
* Returns:
* The value specified in the configuration file, or _default_ if no
* value was specified in the configuration file.
*/
protected function getValueWithDefault($section, $name, $default)
{
if ($this->settings && isset($this->settings[$section]) &&
isset($this->settings[$section][$name]))
{
return $this->settings[$section][$name];
} else {
return $default;
}
}
}
?>