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, '*.json', |
28
|
|
|
CORE_DIR . $directory . $environment . DS, '*.json', |
29
|
|
|
APP_DIR . $directory, '*.json', |
30
|
|
|
APP_DIR . $directory . $environment . DS, '*.json' |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
foreach($searchDirs as $searchDir) { |
34
|
|
|
foreach(Ajde_Fs_Find::findFiles($searchDir, '*.json') as $configFile) |
35
|
|
|
{ |
36
|
|
|
if (!$configData = json_decode(file_get_contents($configFile), true)) { |
37
|
|
|
throw new Exception('Config file ' . $configFile . ' contains invalid JSON'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->merge(pathinfo($configFile, PATHINFO_FILENAME), $configData); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function defaults() |
46
|
|
|
{ |
47
|
|
|
// URI fragments |
48
|
|
|
$this->set("app.protocol", (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS'])) ? 'https://' : 'http://'); |
49
|
|
|
$this->set("app.domain", $_SERVER['SERVER_NAME']); |
50
|
|
|
$this->set("app.path", str_replace('index.php', '', $_SERVER['PHP_SELF'])); |
51
|
|
|
$this->set("app.root", $this->get("app.protocol") . $this->site_domain . $this->site_path); |
|
|
|
|
52
|
|
|
$this->set("i18n.root", $this->get("app.root")); |
53
|
|
|
|
54
|
|
|
// Set default timezone now |
55
|
|
|
date_default_timezone_set($this->timezone); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: