1 | <?php |
||
21 | class Config implements \Iterator |
||
22 | { |
||
23 | private $iterPos; |
||
24 | private $basePath; |
||
25 | private $cfgFile; |
||
26 | private $config; |
||
27 | |||
28 | 29 | public function __construct($basePath, $cfgFile = 'conf/sspks.yaml') |
|
29 | { |
||
30 | 29 | $this->iterPos = 0; |
|
31 | 29 | $this->basePath = $basePath; |
|
32 | 29 | $this->cfgFile = $this->basePath . DIRECTORY_SEPARATOR . $cfgFile; |
|
33 | |||
34 | 29 | if (!file_exists($this->cfgFile)) { |
|
35 | 1 | throw new \Exception('Config file "' . $this->cfgFile . '" not found!'); |
|
36 | } |
||
37 | |||
38 | try { |
||
39 | /** @var array $config */ |
||
40 | 28 | $config = Yaml::parse(file_get_contents($this->cfgFile)); |
|
41 | 28 | } catch (ParseException $e) { |
|
42 | 1 | throw new \Exception($e->getMessage()); |
|
43 | } |
||
44 | |||
45 | /** Init variables that are not actual config variables */ |
||
46 | 27 | $config['SSPKS_COMMIT'] = ''; |
|
47 | 27 | $config['SSPKS_BRANCH'] = ''; |
|
48 | |||
49 | /** Override config values with environment variables if present */ |
||
50 | 27 | if ($this->envVarIsNotEmpty('SSPKS_COMMIT')) { |
|
51 | $config['SSPKS_COMMIT'] = $_ENV['SSPKS_COMMIT']; |
||
52 | } |
||
53 | |||
54 | 27 | if ($this->envVarIsNotEmpty('SSPKS_BRANCH')) { |
|
55 | $config['SSPKS_BRANCH'] = $_ENV['SSPKS_BRANCH']; |
||
56 | } |
||
57 | |||
58 | 27 | if ($this->envVarIsNotEmpty('SSPKS_SITE_NAME')) { |
|
59 | $config['site']['name'] = $_ENV['SSPKS_SITE_NAME']; |
||
60 | } |
||
61 | |||
62 | 27 | if ($this->envVarIsNotEmpty('SSPKS_SITE_THEME')) { |
|
63 | $config['site']['theme'] = $_ENV['SSPKS_SITE_THEME']; |
||
64 | } |
||
65 | |||
66 | 27 | if ($this->envVarIsNotEmpty('SSPKS_SITE_REDIRECTINDEX')) { |
|
67 | $config['site']['redirectindex'] = $_ENV['SSPKS_SITE_REDIRECTINDEX']; |
||
68 | } |
||
69 | |||
70 | 27 | $this->config = $config; |
|
71 | 27 | $this->config['basePath'] = $this->basePath; |
|
72 | 27 | } |
|
73 | |||
74 | /** |
||
75 | * Checks wether an env variable exists and is not an empty string. |
||
76 | * |
||
77 | * @param string $name Name of requested environment variable. |
||
78 | * @return boolean value. |
||
79 | */ |
||
80 | 27 | public function envVarIsNotEmpty($name) |
|
84 | |||
85 | /** |
||
86 | * Getter magic method. |
||
87 | * |
||
88 | * @param string $name Name of requested value. |
||
89 | * @return mixed Requested value. |
||
90 | */ |
||
91 | 26 | public function __get($name) |
|
95 | |||
96 | /** |
||
97 | * Setter magic method. |
||
98 | * |
||
99 | * @param string $name Name of variable to set. |
||
100 | * @param mixed $value Value to set. |
||
101 | */ |
||
102 | 26 | public function __set($name, $value) |
|
106 | |||
107 | /** |
||
108 | * Isset feature magic method. |
||
109 | * |
||
110 | * @param string $name Name of requested value. |
||
111 | * @return bool TRUE if value exists, FALSE otherwise. |
||
112 | */ |
||
113 | 1 | public function __isset($name) |
|
117 | |||
118 | /** |
||
119 | * Unset feature magic method. |
||
120 | * |
||
121 | * @param string $name Name of value to unset. |
||
122 | */ |
||
123 | 1 | public function __unset($name) |
|
127 | |||
128 | 1 | public function rewind() |
|
132 | |||
133 | 1 | public function current() |
|
137 | |||
138 | 1 | public function key() |
|
142 | |||
143 | 1 | public function next() |
|
147 | |||
148 | 1 | public function valid() |
|
152 | } |
||
153 |