1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2017 SURFnet. |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
8
|
|
|
* License, or (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Affero General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage; |
20
|
|
|
|
21
|
|
|
use fkooman\RemoteStorage\Exception\ConfigException; |
22
|
|
|
use Symfony\Component\Yaml\Yaml; |
23
|
|
|
|
24
|
|
|
class Config |
25
|
|
|
{ |
26
|
|
|
/** @var array */ |
27
|
|
|
private $data; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $data |
31
|
|
|
*/ |
32
|
|
|
public function __construct(array $data) |
33
|
|
|
{ |
34
|
|
|
$this->data = $data; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $key |
39
|
|
|
*/ |
40
|
|
|
public function __isset($key) |
41
|
|
|
{ |
42
|
|
|
return array_key_exists($key, $this->data); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $key |
47
|
|
|
* |
48
|
|
|
* @return object|string|array |
49
|
|
|
*/ |
50
|
|
|
public function __get($key) |
51
|
|
|
{ |
52
|
|
|
if (!array_key_exists($key, $this->data)) { |
53
|
|
|
// consumers MUST check first if a field is available before |
54
|
|
|
// requesting it |
55
|
|
|
throw new ConfigException(sprintf('missing field "%s" in configuration', $key)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (is_array($this->data[$key])) { |
59
|
|
|
// if all we get is a "flat" array with sequential numeric keys |
60
|
|
|
// return the array instead of an object |
61
|
|
|
$k = array_keys($this->data[$key]); |
62
|
|
|
if ($k === range(0, count($k) - 1)) { |
63
|
|
|
return $this->data[$key]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return new self($this->data[$key]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->data[$key]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function asArray() |
76
|
|
|
{ |
77
|
|
|
return $this->data; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function fromFile($configFile) |
81
|
|
|
{ |
82
|
|
|
if (false === $fileContent = @file_get_contents($configFile)) { |
83
|
|
|
throw new ConfigException(sprintf('unable to read "%s"', $configFile)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return new static(Yaml::parse($fileContent)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function toFile($configFile, array $configData) |
90
|
|
|
{ |
91
|
|
|
$fileData = Yaml::dump($configData, 3); |
92
|
|
|
if (false === @file_put_contents($configFile, $fileData)) { |
93
|
|
|
throw new ConfigException(sprintf('unable to write "%s"', $configFile)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|