/
Config.php
176 lines (147 loc) · 5.1 KB
/
Config.php
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
<?php
namespace SSpkS;
use \Symfony\Component\Yaml\Yaml;
use \Symfony\Component\Yaml\Exception\ParseException;
/**
* Configuration class
*
* @property array $site Site properties
* @property array $paths Different paths
* @property array excludedSynoServices Synology services to exclude from package list
* @property array packages Defaults for packages
* @property string basePath Path to site root (where index.php is located)
* @property string baseUrl URL to site root (where index.php is located)
* @property string baseUrlRelative Relative URL to site root (without scheme or hostname)
* @property string SSPKS_COMMIT current commit hash taken from ENV variables
* @property string SSPKS_BRANCH current branch taken from ENV variables
*/
class Config implements \Iterator
{
private $iterPos;
private $basePath;
private $cfgFile;
private $config;
public function __construct($basePath, $cfgFile = 'conf/sspks.yaml')
{
$this->iterPos = 0;
$this->basePath = $basePath;
$this->cfgFile = $this->basePath . DIRECTORY_SEPARATOR . $cfgFile;
if (!file_exists($this->cfgFile)) {
throw new \Exception('Config file "' . $this->cfgFile . '" not found!');
}
try {
/** @var array $config */
$config = Yaml::parse(file_get_contents($this->cfgFile));
} catch (ParseException $e) {
throw new \Exception($e->getMessage());
}
/** Init variables that are not actual config variables */
$config['SSPKS_COMMIT'] = '';
$config['SSPKS_BRANCH'] = '';
/** Override config values with environment variables if present */
if ($this->envVarIsNotEmpty('SSPKS_COMMIT')) {
$config['SSPKS_COMMIT'] = $_ENV['SSPKS_COMMIT'];
}
if ($this->envVarIsNotEmpty('SSPKS_BRANCH')) {
$config['SSPKS_BRANCH'] = $_ENV['SSPKS_BRANCH'];
}
if ($this->envVarIsNotEmpty('SSPKS_SITE_NAME')) {
$config['site']['name'] = $_ENV['SSPKS_SITE_NAME'];
}
if ($this->envVarIsNotEmpty('SSPKS_SITE_THEME')) {
$config['site']['theme'] = $_ENV['SSPKS_SITE_THEME'];
}
if ($this->envVarIsNotEmpty('SSPKS_SITE_REDIRECTINDEX')) {
$config['site']['redirectindex'] = $_ENV['SSPKS_SITE_REDIRECTINDEX'];
}
if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_FILE_MASK')) {
$config['packages']['file_mask'] = $_ENV['SSPKS_PACKAGES_FILE_MASK'];
}
if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_MAINTAINER')) {
$config['packages']['maintainer'] = $_ENV['SSPKS_PACKAGES_MAINTAINER'];
}
if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_MAINTAINER_URL')) {
$config['packages']['maintainer_url'] = $_ENV['SSPKS_PACKAGES_MAINTAINER_URL'];
}
if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_DISTRIBUTOR')) {
$config['packages']['distributor'] = $_ENV['SSPKS_PACKAGES_DISTRIBUTOR'];
}
if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_DISTRIBUTOR_URL')) {
$config['packages']['distributor_url'] = $_ENV['SSPKS_PACKAGES_DISTRIBUTOR_URL'];
}
if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_SUPPORT_URL')) {
$config['packages']['support_url'] = $_ENV['SSPKS_PACKAGES_SUPPORT_URL'];
}
$this->config = $config;
$this->config['basePath'] = $this->basePath;
}
/**
* Checks wether an env variable exists and is not an empty string.
*
* @param string $name Name of requested environment variable.
* @return boolean value.
*/
public function envVarIsNotEmpty($name)
{
return (array_key_exists($name, $_ENV) && $_ENV[$name]);
}
/**
* Getter magic method.
*
* @param string $name Name of requested value.
* @return mixed Requested value.
*/
public function __get($name)
{
return $this->config[$name];
}
/**
* Setter magic method.
*
* @param string $name Name of variable to set.
* @param mixed $value Value to set.
*/
public function __set($name, $value)
{
$this->config[$name] = $value;
}
/**
* Isset feature magic method.
*
* @param string $name Name of requested value.
* @return bool TRUE if value exists, FALSE otherwise.
*/
public function __isset($name)
{
return isset($this->config[$name]);
}
/**
* Unset feature magic method.
*
* @param string $name Name of value to unset.
*/
public function __unset($name)
{
unset($this->config[$name]);
}
public function rewind()
{
$this->iterPos = 0;
}
public function current()
{
return $this->config[array_keys($this->config)[$this->iterPos]];
}
public function key()
{
return array_keys($this->config)[$this->iterPos];
}
public function next()
{
$this->iterPos++;
}
public function valid()
{
return isset(array_keys($this->config)[$this->iterPos]);
}
}