1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Configure; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait implementing reading from config-file and storing options in |
7
|
|
|
* $this->config. |
8
|
|
|
*/ |
9
|
|
|
trait ConfigureTrait |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var [] $config store the configuration in this array. |
|
|
|
|
13
|
|
|
*/ |
14
|
|
|
private $config = []; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Read configuration from file or array, if a file, first check in |
20
|
|
|
* ANAX_APP_PATH/config and then in ANAX_INSTALL_PATH/config. |
21
|
|
|
* |
22
|
|
|
* @param array|string $what is an array with key/value config options |
23
|
|
|
* or a file to be included which returns such |
24
|
|
|
* an array. |
25
|
|
|
* |
26
|
|
|
* @throws Exception when argument if not a file nor an array. |
27
|
|
|
* |
28
|
|
|
* @return self for chaining. |
29
|
|
|
*/ |
30
|
4 |
|
public function configure($what) |
31
|
|
|
{ |
32
|
4 |
|
if (is_array($what)) { |
33
|
2 |
|
$this->config = $what; |
34
|
2 |
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
View Code Duplication |
if (defined("ANAX_APP_PATH")) { |
|
|
|
|
38
|
2 |
|
$path = ANAX_APP_PATH . "/config/$what"; |
39
|
2 |
|
if (is_readable($path)) { |
40
|
1 |
|
$this->config = require $path; |
41
|
1 |
|
return $this; |
42
|
|
|
} |
43
|
1 |
|
} |
44
|
|
|
|
45
|
1 |
View Code Duplication |
if (defined("ANAX_INSTALL_PATH")) { |
|
|
|
|
46
|
1 |
|
$path = ANAX_INSTALL_PATH . "/config/$what"; |
47
|
1 |
|
if (is_readable($path)) { |
48
|
|
|
$this->config = require $path; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
1 |
|
} |
52
|
|
|
|
53
|
1 |
|
if (is_readable($what)) { |
54
|
|
|
$this->config = require $what; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
throw new Exception("Configure item '$what' is not an array nor a readable file."); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Helper function for reading values from the configuration. |
65
|
|
|
* |
66
|
|
|
* @param string $key matching a key in the config array. |
67
|
|
|
* @param string $default value returned when config item is not found. |
68
|
|
|
* |
69
|
|
|
* @return mixed or null if key does not exists. |
70
|
|
|
*/ |
71
|
|
|
public function getConfig($key, $default = null) |
72
|
|
|
{ |
73
|
|
|
return isset($this->config[$key]) |
74
|
|
|
? $this->config[$key] |
75
|
|
|
: $default; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.