1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SSpkS; |
4
|
|
|
|
5
|
|
|
use \Symfony\Component\Yaml\Yaml; |
6
|
|
|
use \Symfony\Component\Yaml\Exception\ParseException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Configuration class |
10
|
|
|
* |
11
|
|
|
* @property array $site Site properties |
12
|
|
|
* @property array $paths Different paths |
13
|
|
|
* @property array excludedSynoServices Synology services to exclude from package list |
14
|
|
|
* @property string basePath Path to site root (where index.php is located) |
15
|
|
|
* @property string baseUrl URL to site root (where index.php is located) |
16
|
|
|
*/ |
17
|
|
|
class Config implements \Iterator |
18
|
|
|
{ |
19
|
|
|
private $iterPos; |
20
|
|
|
private $basePath; |
21
|
|
|
private $cfgFile; |
22
|
|
|
private $config; |
23
|
|
|
|
24
|
3 |
|
public function __construct($basePath, $cfgFile = 'conf/sspks.yaml') |
25
|
|
|
{ |
26
|
3 |
|
$this->iterPos = 0; |
27
|
3 |
|
$this->basePath = $basePath; |
28
|
3 |
|
$this->cfgFile = $this->basePath . DIRECTORY_SEPARATOR . $cfgFile; |
29
|
|
|
|
30
|
3 |
|
if (!file_exists($this->cfgFile)) { |
31
|
1 |
|
throw new \Exception('Config file "' . $this->cfgFile . '" not found!'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
try { |
35
|
|
|
/** @var array $config */ |
36
|
2 |
|
$config = Yaml::parse(file_get_contents($this->cfgFile)); |
37
|
2 |
|
} catch (ParseException $e) { |
38
|
1 |
|
throw new \Exception($e->getMessage()); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
$this->config = $config; |
42
|
1 |
|
$this->config['basePath'] = $this->basePath; |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Getter magic method. |
47
|
|
|
* |
48
|
|
|
* @param string $name Name of requested value. |
49
|
|
|
* @return mixed Requested value. |
50
|
|
|
*/ |
51
|
1 |
|
public function __get($name) |
52
|
|
|
{ |
53
|
1 |
|
return $this->config[$name]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Setter magic method. |
58
|
|
|
* |
59
|
|
|
* @param string $name Name of variable to set. |
60
|
|
|
* @param mixed $value Value to set. |
61
|
|
|
*/ |
62
|
|
|
public function __set($name, $value) |
63
|
|
|
{ |
64
|
|
|
$this->config[$name] = $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Isset feature magic method. |
69
|
|
|
* |
70
|
|
|
* @param string $name Name of requested value. |
71
|
|
|
* @return bool TRUE if value exists, FALSE otherwise. |
72
|
|
|
*/ |
73
|
|
|
public function __isset($name) |
74
|
|
|
{ |
75
|
|
|
return isset($this->config[$name]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Unset feature magic method. |
80
|
|
|
* |
81
|
|
|
* @param string $name Name of value to unset. |
82
|
|
|
*/ |
83
|
|
|
public function __unset($name) |
84
|
|
|
{ |
85
|
|
|
unset($this->config[$name]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function rewind() |
89
|
|
|
{ |
90
|
|
|
$this->iterPos = 0; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function current() |
94
|
|
|
{ |
95
|
|
|
return $this->config[array_keys($this->config)[$this->iterPos]]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function key() |
99
|
|
|
{ |
100
|
|
|
return array_keys($this->config)[$this->iterPos]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function next() |
104
|
|
|
{ |
105
|
|
|
$this->iterPos++; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function valid() |
109
|
|
|
{ |
110
|
|
|
return isset(array_keys($this->config)[$this->iterPos]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|