-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseConfig.php
127 lines (100 loc) · 3.62 KB
/
DatabaseConfig.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
<?php
class AetherDatabaseConfig {
private static $configuration = array();
public static $folder=null;
/**
*
* @param string $name
* @param bool $required
*/
public static function retrieve($name, $required = true) {
$group = explode('.', $name, 2);
$group = $group[0];
if (!isset(self::$configuration[$group])) {
// Load the configuration group
$defaultFolder = dirname(__FILE__) . '/config/';
if (self::$folder==null)
self::$folder = $defaultFolder;
$file = self::$folder . $group . '.php';
$defaultFile = $defaultFolder . $group . '.php';
if (!file_exists($file)) {
// Try default folder
if (!file_exists($defaultFile)) {
if ($required === true)
throw new Exception("$file does not exists");
else
return false;
}
else
$file = $defaultFile;
}
include($file);
if (!isset($config) || empty($config))
throw new Exception("Could not find config array in $file");
self::$configuration[$group] = $config;
}
$value = self::retrieveKey(self::$configuration, $name);
if ($required === true && ($value === NULL || empty($value)))
throw new Exception("Could not find $name in $file");
return $value;
}
private static function retrieveKey($array, $keys) {
if (empty($array) || empty($keys))
return NULL;
$keys = explode('.', $keys);
while(!empty($keys)) {
$key = array_shift($keys);
if (isset($array[$key])) {
if (is_array($array[$key]) && !empty($keys)) {
// Dig down array to prepare the next loop
$array = $array[$key];
} else {
return $array[$key];
}
} else {
// Requested key was not found
break;
}
}
return NULL;
}
public static function autoLoad($class) {
if (class_exists($class, false))
return true;
// Get this directory
$dir = dirname(__FILE__) . '/';
// Split up the class name into logical parts
// MUST BE CAMELCASE!
$matches = preg_split('/([A-Z][^A-Z]+)/', $class, -1, PREG_SPLIT_NO_EMPTY |
PREG_SPLIT_DELIM_CAPTURE);
// We dont use the aether part to find the filename
if ($matches[0] == 'Aether') {
array_shift($matches);
$class = implode($matches);
}
$suffix = array_pop($matches);
if ($suffix == 'Driver') {
$type = 'driver/';
if (count($matches) == 1)
$file = array_shift($matches) . '.php';
elseif (count($matches) == 2)
$file = array_shift($matches) . '/' . array_shift($matches) . '.php';
else
return false;
}
else {
// Try to check if there is a file with the name of the class
if (file_exists($dir . $class . '.php')) {
require($dir . $class . '.php');
return true;
}
else
return false;
}
// Check that the file exists
if (!file_exists($dir . $type . $file))
return false;
require $dir . $type . $file;
return true;
}
}