This repository was archived by the owner on Aug 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.php
More file actions
110 lines (80 loc) · 2.47 KB
/
settings.php
File metadata and controls
110 lines (80 loc) · 2.47 KB
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
<?php /* Chronicle.md - Copyright (C) 2013 Bruce Alderson */
/* Chronicle settings
TODO:
- check file contents
- provide defaults
- write out if missing
*/
class siteSettings {
private $files;
private $n;
/* Construct the settings object */
public function __construct() {
// default settings
$this->files = array(
'site' => (object) array(
'file' => API_BASE.'/site.json',
'defaults' => (object) array(
'URL' => '',
'homePosts' => 1,
'archivePosts' => 10,
'feedPosts' => 10,
'name' => 'Site name',
'tagline' => 'This is a tagline',
'description' => 'This is a description',
'blog' => '/blog/'
)
));
try {
$this->loadSettings();
} catch(Exception $e) {
presto_lib::_trace($e->getMessage());
throw $e;
}
}
/* Handle missing settings files (last chance) */
public function __get($n) {
presto_lib::_trace("Skipping missing '$n' settings (file not loaded)");
return "[missing file $n]";
}
/* ======================== Private helpers ======================== */
/* Load the settings files */
private function loadSettings() {
foreach ($this->files as $n => $f) {
if (!file_exists($f->file))
throw new Exception("Missing '$n' settings ($f not found)");
$config = file_get_contents($f->file);
if (!$config || empty($config))
throw new Exception("Empty configuration file $f");
$this->$n = new settingsFile($config, $n, $f->file, $f->defaults);
}
presto_lib::_trace("Loaded $n settings.");
}
}
/* One settings file */
class settingsFile {
public $d;
/* Set up the setting object */
public function __construct($s, $n, $f, $defaults = null) {
// populate settings
if (is_string($s))
$this->d = json_decode($s); // decode from string
elseif (is_array($s))
$this->d = (object) $s; // from array, objectize
elseif (is_object($s))
$this->d = $s; // from object
else
throw new Exception("Unknown configuration format found for $n: [$f] - $s");
// merge in defaults, if any
if ( $defaults && (is_object($defaults) || is_array($defaults)) )
$this->d = (object) array_merge( (array) $defaults, (array) $this->d );
}
public function hasData() { return !empty($this->d); }
// Get a setting
public function __get($n) {
if (property_exists($this->d, $n))
return $this->d->$n;
presto_lib::_trace("Skipping missing '$n' setting (property does not exist)");
return "[missing $n]";
}
}