|
1
|
|
|
<?php |
|
2
|
|
|
namespace Puppy\Config; |
|
3
|
|
|
|
|
4
|
|
|
use Puppy\Config\FileReader\IFileReader; |
|
5
|
|
|
use Puppy\Config\FileReader\PhpFileReader; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Config |
|
9
|
|
|
* @package Puppy\Config |
|
10
|
|
|
* @author Raphaël Lefebvre <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class Config extends ArrayConfig |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $env; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var FileParams |
|
20
|
|
|
*/ |
|
21
|
|
|
private $fileParams; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var IFileReader |
|
24
|
|
|
*/ |
|
25
|
|
|
private $fileReader; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $env |
|
29
|
|
|
* @param FileParams $fileParams |
|
|
|
|
|
|
30
|
|
|
* @param IFileReader $fileReader |
|
|
|
|
|
|
31
|
|
|
*/ |
|
32
|
10 |
|
public function __construct($env = '', FileParams $fileParams = null, IFileReader $fileReader = null) |
|
33
|
|
|
{ |
|
34
|
10 |
|
$this->setEnv($env); |
|
35
|
10 |
|
$this->setFileParams($fileParams ? : new FileParams()); |
|
36
|
10 |
|
$this->setFileReader($fileReader ? : new PhpFileReader()); |
|
37
|
10 |
|
parent::__construct($this->getData()); |
|
38
|
10 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Getter of $env |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
10 |
|
public function getEnv() |
|
46
|
|
|
{ |
|
47
|
10 |
|
return $this->env; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Getter of $fileParams |
|
52
|
|
|
* |
|
53
|
|
|
* @return FileParams |
|
54
|
|
|
*/ |
|
55
|
10 |
|
public function getFileParams() |
|
56
|
|
|
{ |
|
57
|
10 |
|
return $this->fileParams; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Getter of $fileReader |
|
62
|
|
|
* |
|
63
|
|
|
* @return IFileReader |
|
64
|
|
|
*/ |
|
65
|
10 |
|
public function getFileReader() |
|
66
|
|
|
{ |
|
67
|
10 |
|
return $this->fileReader; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
10 |
|
public function getMainFilePath() |
|
74
|
|
|
{ |
|
75
|
10 |
|
return $this->getFileParams()->getDirPath() |
|
76
|
|
|
. DIRECTORY_SEPARATOR |
|
77
|
10 |
|
. $this->getFileParams()->getMainConfigFileName() |
|
78
|
10 |
|
. $this->getFileReader()->getFileExtension(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
10 |
|
public function getEnvFilePath() |
|
85
|
|
|
{ |
|
86
|
10 |
|
return $this->getFileParams()->getDirPath() |
|
87
|
|
|
. DIRECTORY_SEPARATOR |
|
88
|
10 |
|
. $this->getEnv() |
|
89
|
10 |
|
. $this->getFileReader()->getFileExtension(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
10 |
|
public function getLocalFilePath() |
|
96
|
|
|
{ |
|
97
|
10 |
|
return $this->getFileParams()->getDirPath() |
|
98
|
|
|
. DIRECTORY_SEPARATOR |
|
99
|
10 |
|
. $this->getFileParams()->getLocalConfigFileName() |
|
100
|
10 |
|
. $this->getFileReader()->getFileExtension(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
10 |
|
private function getData() |
|
107
|
|
|
{ |
|
108
|
10 |
|
return $this->getFilesContent( |
|
109
|
|
|
[ |
|
110
|
10 |
|
$this->getMainFilePath(), |
|
111
|
10 |
|
$this->getEnvFilePath(), |
|
112
|
10 |
|
$this->getLocalFilePath(), |
|
113
|
|
|
] |
|
114
|
10 |
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string[] $filePaths |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
10 |
|
private function getFilesContent(array $filePaths) |
|
122
|
|
|
{ |
|
123
|
10 |
|
$result = []; |
|
124
|
10 |
|
foreach ($filePaths as $filePath) { |
|
125
|
10 |
|
$filePath = $this->getRealPath($filePath); |
|
126
|
10 |
|
if ($filePath) { |
|
127
|
8 |
|
$result = array_merge($result, $this->getFileReader()->read($filePath)); |
|
128
|
8 |
|
} |
|
129
|
10 |
|
} |
|
130
|
10 |
|
return $result; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $filePath |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
10 |
|
private function getRealPath($filePath) |
|
138
|
|
|
{ |
|
139
|
10 |
|
$filePath = stream_resolve_include_path($filePath); |
|
140
|
10 |
|
if ($filePath && is_readable($filePath)) { |
|
141
|
8 |
|
return $filePath; |
|
142
|
|
|
} |
|
143
|
7 |
|
return ''; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Setter of $env |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $env |
|
150
|
|
|
*/ |
|
151
|
10 |
|
private function setEnv($env) |
|
152
|
|
|
{ |
|
153
|
10 |
|
$this->env = (string)$env; |
|
154
|
10 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Setter of $fileParams |
|
158
|
|
|
* |
|
159
|
|
|
* @param FileParams $fileParams |
|
160
|
|
|
*/ |
|
161
|
10 |
|
private function setFileParams(FileParams $fileParams) |
|
162
|
|
|
{ |
|
163
|
10 |
|
$this->fileParams = $fileParams; |
|
164
|
10 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Setter of $fileReader |
|
168
|
|
|
* |
|
169
|
|
|
* @param IFileReader $fileReader |
|
170
|
|
|
*/ |
|
171
|
10 |
|
private function setFileReader(IFileReader $fileReader) |
|
172
|
|
|
{ |
|
173
|
10 |
|
$this->fileReader = $fileReader; |
|
174
|
10 |
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.