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 getDefaultEnvironment() |
52
|
|
|
{ |
53
|
|
|
return $this->defaultEnvironment; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getAllValuesForEnvironment($environment = null) |
57
|
|
|
{ |
58
|
|
|
$result = array(); |
59
|
|
|
|
60
|
|
|
$variables = $this->getAllVariables(); |
61
|
|
|
|
62
|
|
|
foreach($variables as $variable) |
63
|
|
|
{ |
64
|
|
|
try |
65
|
|
|
{ |
66
|
|
|
$value = $this->read($variable, $environment); |
67
|
|
|
} |
68
|
|
|
catch(\RuntimeException $e) |
69
|
|
|
{ |
70
|
|
|
$value = Configuration::NOT_FOUND; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$result[$variable] = $value; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function overrideVariable($variable, $value) |
80
|
|
|
{ |
81
|
|
|
$this->overridenVariables[$variable] = $value; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function setCustomData($customDataName, $value) |
87
|
|
|
{ |
88
|
|
|
$key = '${' . $customDataName . '}'; |
89
|
|
|
$this->customData[$key] = $value; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function handleCustomData($value) |
95
|
|
|
{ |
96
|
|
|
if(! is_string($value)) |
97
|
|
|
{ |
98
|
|
|
return $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return strtr($value, $this->customData); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.