Update phing build script to use composer installation only
[bdrem.git] / src / bdrem / Config.php
1 <?php
2 /**
3  * Part of bdrem
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Bdrem
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/bdrem.htm
13  */
14 namespace bdrem;
15
16 /**
17  * Configuration options for bdrem
18  *
19  * @category  Tools
20  * @package   Bdrem
21  * @author    Christian Weiske <cweiske@cweiske.de>
22  * @copyright 2014 Christian Weiske
23  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
24  * @version   Release: @package_version@
25  * @link      http://cweiske.de/bdrem.htm
26  */
27 class Config
28 {
29     /**
30      * Current date, YYYY-MM-DD
31      * @var string
32      */
33     public $date;
34
35     /**
36      * Days to show before $date
37      * @var integer
38      */
39     public $daysPrev;
40
41     /**
42      * Days to show after $date
43      * @var integer
44      */
45     public $daysNext;
46
47     /**
48      * Development helper
49      * @var boolean
50      */
51     public $debug;
52
53     /**
54      * Locale to render the dates in, e.g. "de_DE.UTF-8"
55      * @var string
56      */
57     public $locale;
58
59     /**
60      * Renderer name to use (e.g. "console")
61      * @var string
62      */
63     public $renderer;
64
65     /**
66      * Event source configuration.
67      * - First value is source name ("Ldap", "Sql")
68      * - Second value is the source configuration
69      * @var array
70      */
71     public $source;
72
73     /**
74      * Do not output anything if there are no events to show
75      * @var boolean
76      */
77     public $stopOnEmpty;
78
79     /**
80      * List of config file paths that were tried to load
81      * @var array
82      */
83     public $cfgFiles = array();
84
85     /**
86      * If a configuration file could be found
87      * @var boolean
88      */
89     public $cfgFileExists;
90
91
92
93     /**
94      * Init configuration file path loading
95      */
96     public function __construct()
97     {
98         $this->loadConfigFilePaths();
99     }
100
101     /**
102      * Load the configuration from the first configuration file found.
103      *
104      * @return void
105      */
106     public function load()
107     {
108         foreach ($this->cfgFiles as $file) {
109             if (file_exists($file)) {
110                 $this->cfgFileExists = true;
111                 return $this->loadFile($file);
112             }
113         }
114         $this->cfgFileExists = false;
115     }
116
117     /**
118      * Load possible configuration file paths into $this->cfgFiles.
119      *
120      * @return void
121      */
122     protected function loadConfigFilePaths()
123     {
124         $pharFile = \Phar::running();
125         if ($pharFile == '') {
126             $this->cfgFiles[] = __DIR__ . '/../../data/bdrem.config.php';
127         } else {
128             //remove phar:// from the path
129             $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
130         }
131
132         //TODO: add ~/.config/bdrem.php
133
134         $this->cfgFiles[] = '/etc/bdrem.php';
135     }
136
137     /**
138      * Load a single configuration file and set the config class variables
139      *
140      * @param string $filename Configuration file path
141      *
142      * @return void
143      */
144     protected function loadFile($filename)
145     {
146         include $filename;
147         $vars = get_defined_vars();
148         foreach ($vars as $k => $value) {
149             if (!isset($this->$k) || $this->$k === null) {
150                 $this->$k = $value;
151             }
152         }
153     }
154
155     /**
156      * Load a event source from $this->source.
157      * Class name has to be \bdrem\Source_$source
158      *
159      * @return object Source object
160      */
161     public function loadSource()
162     {
163         if ($this->source === null) {
164             throw new \Exception('No source defined');
165         }
166
167         $settings = $this->source;
168         $class = '\\bdrem\\Source_' . array_shift($settings);
169
170         return new $class($settings[0]);
171     }
172
173     /**
174      * Set the current date
175      *
176      * @param string $date Date in any format
177      *
178      * @return void
179      */
180     public function setDate($date)
181     {
182         if ($date === null) {
183             $this->date = date('Y-m-d');
184         } else {
185             $dt = new \DateTime($date);
186             $this->date = $dt->format('Y-m-d');
187         }
188     }
189
190     /**
191      * Get a configuration variable
192      *
193      * @param string $varname Configuration variable name
194      * @param string $default Default value in case the variable is not set
195      *                        or is empty
196      *
197      * @return mixed Configuration value or default
198      */
199     public function get($varname, $default = '')
200     {
201         if (!isset($this->$varname) || $this->$varname == '') {
202             return $default;
203         }
204         return $this->$varname;
205     }
206 }
207 ?>