blob: 693307eec22154cfdba4e462b826cf4563c75758 (
plain)
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
|
<?php
/**
* Part of bdrem
*
* PHP version 5
*
* @category Tools
* @package Bdrem
* @author Christian Weiske <cweiske@cweiske.de>
* @copyright 2014 Christian Weiske
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
* @link http://cweiske.de/bdrem.htm
*/
namespace bdrem;
/**
* Configuration options for bdrem
*
* @category Tools
* @package Bdrem
* @author Christian Weiske <cweiske@cweiske.de>
* @copyright 2014 Christian Weiske
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
* @version Release: @package_version@
* @link http://cweiske.de/bdrem.htm
*/
class Config
{
/**
* Current date, YYYY-MM-DD
* @var string
*/
public $date;
/**
* Days to show before $date
* @var integer
*/
public $daysPrev;
/**
* Days to show after $date
* @var integer
*/
public $daysNext;
/**
* Development helper
* @var boolean
*/
public $debug;
/**
* Locale to render the dates in, e.g. "de_DE.UTF-8"
* @var string
*/
public $locale;
/**
* Renderer name to use (e.g. "console")
* @var string
*/
public $renderer;
/**
* Event source configuration.
* - First value is source name ("Ldap", "Sql")
* - Second value is the source configuration
* @var array
*/
public $source;
/**
* Do not output anything if there are no events to show
* @var boolean
*/
public $stopOnEmpty;
/**
* List of config file paths that were tried to load
* @var array
*/
public $cfgFiles = array();
/**
* If a configuration file could be found
* @var boolean
*/
public $cfgFileExists;
/**
* Init configuration file path loading
*/
public function __construct()
{
$this->loadConfigFilePaths();
}
/**
* Load the configuration from the first configuration file found.
*
* @return void
*/
public function load()
{
foreach ($this->cfgFiles as $file) {
if (file_exists($file)) {
$this->cfgFileExists = true;
return $this->loadFile($file);
}
}
$this->cfgFileExists = false;
}
/**
* Load possible configuration file paths into $this->cfgFiles.
*
* @return void
*/
protected function loadConfigFilePaths()
{
$pharFile = \Phar::running();
if ($pharFile == '') {
$this->cfgFiles[] = __DIR__ . '/../../data/bdrem.config.php';
} else {
//remove phar:// from the path
$this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
}
//TODO: add ~/.config/bdrem.php
$this->cfgFiles[] = '/etc/bdrem.php';
}
/**
* Load a single configuration file and set the config class variables
*
* @param string $filename Configuration file path
*
* @return void
*/
protected function loadFile($filename)
{
include $filename;
$vars = get_defined_vars();
foreach ($vars as $k => $value) {
if (!isset($this->$k) || $this->$k === null) {
$this->$k = $value;
}
}
}
/**
* Load a event source from $this->source.
* Class name has to be \bdrem\Source_$source
*
* @return object Source object
*/
public function loadSource()
{
if ($this->source === null) {
throw new \Exception('No source defined');
}
$settings = $this->source;
$class = '\\bdrem\\Source_' . array_shift($settings);
return new $class($settings[0]);
}
/**
* Set the current date
*
* @param string $date Date in any format
*
* @return void
*/
public function setDate($date)
{
if ($date === null) {
$this->date = date('Y-m-d');
} else {
$dt = new \DateTime($date);
$this->date = $dt->format('Y-m-d');
}
}
/**
* Get a configuration variable
*
* @param string $varname Configuration variable name
* @param string $default Default value in case the variable is not set
* or is empty
*
* @return mixed Configuration value or default
*/
public function get($varname, $default = '')
{
if (!isset($this->$varname) || $this->$varname == '') {
return $default;
}
return $this->$varname;
}
}
?>
|