/
sgConfiguration.class.php
270 lines (231 loc) · 6.87 KB
/
sgConfiguration.class.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
<?php
/*
TODO Bring these in with the autoloader
*/
require(dirname(__FILE__) . '/sgContext.class.php');
require(dirname(__FILE__) . '/lib/sgToolkit.class.php');
require(dirname(__FILE__) . '/vendor/Twig/lib/Twig/Autoloader.php');
class sgConfiguration
{
private static $instance;
protected static $config = array();
protected static $enabledPlugins = array();
private function __construct()
{
sgContext::getInstance()->setRootDir(self::getRootDir());
sgContext::getInstance()->setLibDir(self::getLibDir());
self::loadConfig('settings', dirname(__FILE__) . '/config/config.php');
self::_initPlugins(dirname(__FILE__), self::get('settings.enabled_plugins')); //init core plugins
$projectConfig = null;
if (file_exists(realpath(sgContext::getInstance()->getRootDir() . '/config/config.php')))
{
$projectConfig = include realpath(sgContext::getInstance()->getRootDir() . '/config/config.php');
if (isset($projectConfig['enabled_plugins']))
{
self::_initPlugins(sgContext::getInstance()->getRootDir(), $projectConfig['enabled_plugins']); //init project plugins
}
self::loadConfigFromArray('settings', $projectConfig);
}
if (file_exists(realpath(sgContext::getInstance()->getRootDir() . '/config/routing.php')))
{
self::loadConfig('routing', sgContext::getInstance()->getRootDir() . '/config/routing.php');
}
self::_initAutoloader();
self::_initPluginConfigurations();
$this->init();
return;
}
public function init() {}
public function execute()
{
sgGlue::stick(sgConfiguration::getRouting());
}
public static function getRootDir()
{
try
{
$r = new ReflectionClass('ProjectConfiguration');
}
catch (Exception $e)
{
return getcwd();
}
return realpath(dirname($r->getFileName()) . '/..');
}
public static function getLibDir()
{
try
{
$r = new ReflectionClass('sgConfiguration');
}
catch (Exception $e)
{
return false;
}
return realpath(dirname($r->getFileName()));
}
private static function _initAutoloader()
{
if (!sgAutoloader::checkCache())
{
$paths = array(sgContext::getLibDir());
foreach (self::$enabledPlugins as $plugin)
{
$paths[] = sgContext::getRootDir() . '/plugins/' . $plugin->name . '/';
}
$paths[] = sgContext::getRootDir() . '/config/';
$paths[] = sgContext::getRootDir() . '/lib/';
$paths[] = sgContext::getRootDir() . '/controllers/';
$paths[] = sgContext::getRootDir() . '/models/';
sgAutoloader::setExclusions(self::get('settings.autoload_exclusions', array()));
sgAutoloader::loadPaths($paths);
}
Twig_Autoloader::register();
}
public static function getInstance()
{
if (!isset(self::$instance))
{
//$c = __CLASS__;
/*
TODO figure out a better way to do this (LSB in 5.3 maybe)
*/
if (class_exists('ProjectConfiguration'))
{
self::$instance = new ProjectConfiguration();
}
else
{
$c = __CLASS__;
self::$instance = new $c;
}
}
return self::$instance;
}
public static function loadConfig($configType, $path, $checkFileExists = false)
{
$loadedConfig = self::loadConfigFile($path, $checkFileExists);
if ($loadedConfig)
{
self::loadConfigFromArray($configType, $loadedConfig);
}
else
{
return false;
}
}
public static function loadConfigFile($path, $checkFileExists = false)
{
$path = realpath($path);
if ($checkFileExists && !file_exists($path)) // TODO This probably doesn't matter now, since I am using include
{
return false;
}
$loadedConfig = include $path;
if (!is_array($loadedConfig) || empty($loadedConfig))
{
throw new Exception('Configuration file "' . $path . '" does not return an array.');
}
return $loadedConfig;
}
public static function loadConfigFromArray($configType, array $config)
{
$newConfig = array($configType => $config);
self::$config = sgToolkit::arrayMergeRecursiveDistinct(self::$config, $newConfig);
}
public static function getPlugins()
{
return self::$enabledPlugins;
}
public static function getPlugin($pluginName)
{
return self::$enabledPlugins[$pluginName];
}
public static function executePluginHook($plugins, $hook)
{
if (!is_array($plugins))
{
$plugins = array($plugins);
}
foreach ($plugins as $plugin)
{
if (isset($plugin->configuration) && method_exists($plugin->configuration, $hook))
{
//php 5.3 allows $class::init(), but I still want 5.2.x support
call_user_func(array($plugin->configuration, $hook));
}
}
}
/*
TODO Still not in love with the way this works, but it's getting better
*/
private static function _initPlugins($dir, array $plugins)
{
foreach ($plugins as $pluginName)
{
$plugin = new StdClass();
$plugin->name = $pluginName;
$plugin->path = "$dir/plugins/$pluginName";
sgAutoloader::loadPaths(array($plugin->path));
$class = $plugin->name . "Configuration";
if (class_exists($class))
{
$configuration = new $class();
sgToolkit::executeMethod($configuration, 'preConfig');
$plugin->configuration = $configuration;
}
self::loadConfig('settings', "{$plugin->path}/config/config.php", true);
self::loadConfig('routing', "{$plugin->path}/config/routing.php", true);
if (isset($configuration))
{
sgToolkit::executeMethod($configuration, 'postConfig');
}
self::$enabledPlugins[$plugin->name] = $plugin;
}
}
private static function _initPluginConfigurations()
{
$plugins = self::getPlugins();
self::executePluginHook($plugins, 'init');
}
public static function set($configType, $setting, $value)
{
self::$config[$configType][$setting] = $value;
}
private static function _executeGet($config, &$keys = null, &$currentConfig = null)
{
if (is_null($keys))
{
$keys = explode('.', $config);
$currentConfig = self::$config;
}
$key = array_shift($keys);
if (!empty($keys))
{
return self::_executeGet($config, $keys, $currentConfig[$key]);
}
if (isset($currentConfig[$key]))
{
return $currentConfig[$key];
}
return false;
}
// $config is dot-notation string
// example: settings.cache_templates
public static function get($config, $default = false)
{
if ($value = self::_executeGet($config))
{
return $value;
}
return $default;
}
public static function getRouting()
{
return self::get('routing');
}
public function __clone()
{
trigger_error('Only one instance of a singleton is allowed.', E_USER_ERROR);
}
}