|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Karma\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use Karma\Configuration; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractReader implements Configuration |
|
8
|
|
|
{ |
|
9
|
|
|
protected |
|
10
|
|
|
$defaultEnvironment; |
|
11
|
|
|
|
|
12
|
|
|
private |
|
13
|
|
|
$overridenVariables, |
|
|
|
|
|
|
14
|
|
|
$customData; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->defaultEnvironment = 'dev'; |
|
19
|
|
|
$this->overridenVariables = array(); |
|
20
|
|
|
$this->customData = array(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function read($variable, $environment = null) |
|
24
|
|
|
{ |
|
25
|
|
|
$value = null; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
if(array_key_exists($variable, $this->overridenVariables)) |
|
28
|
|
|
{ |
|
29
|
|
|
$value = $this->overridenVariables[$variable]; |
|
30
|
|
|
} |
|
31
|
|
|
else |
|
32
|
|
|
{ |
|
33
|
|
|
$value = $this->readRaw($variable, $environment); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $this->handleCustomData($value); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
abstract protected function readRaw($variable, $environment = null); |
|
40
|
|
|
|
|
41
|
|
|
public function setDefaultEnvironment($environment) |
|
42
|
|
|
{ |
|
43
|
|
|
if(! empty($environment) && is_string($environment)) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->defaultEnvironment = $environment; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getAllValuesForEnvironment($environment = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$result = array(); |
|
54
|
|
|
|
|
55
|
|
|
$variables = $this->getAllVariables(); |
|
56
|
|
|
|
|
57
|
|
|
foreach($variables as $variable) |
|
58
|
|
|
{ |
|
59
|
|
|
try |
|
60
|
|
|
{ |
|
61
|
|
|
$value = $this->read($variable, $environment); |
|
62
|
|
|
} |
|
63
|
|
|
catch(\RuntimeException $e) |
|
64
|
|
|
{ |
|
65
|
|
|
$value = Configuration::NOT_FOUND; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$result[$variable] = $value; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function overrideVariable($variable, $value) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->overridenVariables[$variable] = $value; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function setCustomData($customDataName, $value) |
|
82
|
|
|
{ |
|
83
|
|
|
$key = '${' . $customDataName . '}'; |
|
84
|
|
|
$this->customData[$key] = $value; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function handleCustomData($value) |
|
90
|
|
|
{ |
|
91
|
|
|
if(! is_string($value)) |
|
92
|
|
|
{ |
|
93
|
|
|
return $value; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return strtr($value, $this->customData); |
|
97
|
|
|
} |
|
98
|
|
|
} |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.