1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Ajde_Config_Repository extends Ajde_Object_Standard |
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* TODO |
7
|
|
|
* |
8
|
|
|
* @param string $directory |
9
|
|
|
*/ |
10
|
|
|
public function __construct($directory) |
11
|
|
|
{ |
12
|
|
|
$this->readConfigDir($directory); |
13
|
|
|
$this->defaults(); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* TODO |
18
|
|
|
* |
19
|
|
|
* @param string $directory |
20
|
|
|
* @throws Exception |
21
|
|
|
*/ |
22
|
|
|
public function readConfigDir($directory) |
23
|
|
|
{ |
24
|
|
|
$environment = Ajde_Environment::current(); |
25
|
|
|
|
26
|
|
|
$searchDirs = [ |
27
|
|
|
CORE_DIR . $directory, |
28
|
|
|
CORE_DIR . $directory . $environment . DS, |
29
|
|
|
APP_DIR . $directory, |
30
|
|
|
APP_DIR . $directory . $environment . DS |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
foreach ($searchDirs as $searchDir) { |
34
|
|
|
foreach (Ajde_Fs_Find::findFiles($searchDir, '*.json') as $configFile) { |
35
|
|
|
if (!$configData = json_decode(file_get_contents($configFile), true)) { |
36
|
|
|
throw new Exception('Config file ' . $configFile . ' contains invalid JSON'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->merge(pathinfo($configFile, PATHINFO_FILENAME), $configData); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function defaults() |
45
|
|
|
{ |
46
|
|
|
// URI fragments |
47
|
|
|
$this->set("app.protocol", (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS'])) ? 'https://' : 'http://'); |
48
|
|
|
$this->set("app.domain", $_SERVER['SERVER_NAME']); |
49
|
|
|
$this->set("app.path", str_replace('index.php', '', $_SERVER['PHP_SELF'])); |
50
|
|
|
$this->set("app.rootUrl", $this->get("app.protocol") . $this->get("app.domain") . $this->get("app.path")); |
51
|
|
|
$this->set("i18n.rootUrl", $this->get("app.root")); |
52
|
|
|
|
53
|
|
|
// Set default timezone now |
54
|
|
|
date_default_timezone_set($this->get("app.timezone")); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|