|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace allejo\stakx\Object; |
|
4
|
|
|
|
|
5
|
|
|
use allejo\stakx\Core\StakxLogger; |
|
6
|
|
|
use allejo\stakx\System\Filesystem; |
|
7
|
|
|
use allejo\stakx\Utilities\ArrayUtilities; |
|
8
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
|
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
12
|
|
|
|
|
13
|
|
|
class Configuration implements LoggerAwareInterface |
|
14
|
|
|
{ |
|
15
|
|
|
const DEFAULT_NAME = "_config.yml"; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* An array representation of the main Yaml configuration |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $configuration; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var StakxLogger |
|
26
|
|
|
*/ |
|
27
|
|
|
private $output; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Filesystem |
|
31
|
|
|
*/ |
|
32
|
|
|
private $fs; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Configuration constructor. |
|
36
|
|
|
*/ |
|
37
|
15 |
|
public function __construct() |
|
38
|
|
|
{ |
|
39
|
15 |
|
$this->configuration = array(); |
|
40
|
15 |
|
$this->fs = new Filesystem(); |
|
41
|
15 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function parseConfiguration ($configFile) |
|
44
|
|
|
{ |
|
45
|
|
|
if ($this->fs->exists($configFile)) |
|
46
|
|
|
{ |
|
47
|
|
|
try |
|
48
|
|
|
{ |
|
49
|
|
|
$this->configuration = Yaml::parse(file_get_contents($configFile)); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
catch (ParseException $e) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->output->error("Parsing the configuration failed: {message}", array( |
|
54
|
|
|
"message" => $e->getMessage() |
|
55
|
|
|
)); |
|
56
|
|
|
$this->output->error("Using default configuration..."); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->defaultConfiguration(); |
|
61
|
|
|
$this->handleDeprecations(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setLogger(LoggerInterface $logger) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->output = $logger; |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public function isDebug () |
|
73
|
|
|
{ |
|
74
|
1 |
|
return $this->returnConfigOption('debug', false); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl' |
|
|
|
|
|
|
79
|
|
|
* |
|
80
|
|
|
* @return mixed|null |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function getBaseUrl () |
|
83
|
|
|
{ |
|
84
|
2 |
|
$base = $this->returnConfigOption('base'); |
|
85
|
|
|
|
|
86
|
2 |
|
if (is_null($base)) |
|
87
|
2 |
|
{ |
|
88
|
2 |
|
return $this->returnConfigOption('baseurl'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $base; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string[] |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function getDataFolders () |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->returnConfigOption('data'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string[] |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function getDataSets () |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $this->returnConfigOption('datasets'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
public function getIncludes () |
|
111
|
|
|
{ |
|
112
|
1 |
|
return $this->returnConfigOption('include', array()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
public function getExcludes () |
|
116
|
|
|
{ |
|
117
|
1 |
|
return $this->returnConfigOption('exclude', array()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
public function getTheme () |
|
121
|
|
|
{ |
|
122
|
1 |
|
return $this->returnConfigOption('theme'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
public function getConfiguration () |
|
126
|
|
|
{ |
|
127
|
1 |
|
return $this->configuration; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
public function getPageViewFolders () |
|
131
|
|
|
{ |
|
132
|
1 |
|
return $this->returnConfigOption('pageviews'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
public function getTargetFolder () |
|
136
|
|
|
{ |
|
137
|
3 |
|
return $this->returnConfigOption('target'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
public function getCollectionsFolders () |
|
141
|
|
|
{ |
|
142
|
1 |
|
return $this->returnConfigOption('collections'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
public function getTwigAutoescape () |
|
146
|
|
|
{ |
|
147
|
1 |
|
return $this->configuration['twig']['autoescape']; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Return the specified configuration option if available, otherwise return the default |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $name The configuration option to lookup |
|
154
|
|
|
* @param mixed|null $default The default value returned if the configuration option isn't found |
|
155
|
|
|
* |
|
156
|
|
|
* @return mixed|null |
|
157
|
|
|
*/ |
|
158
|
13 |
|
private function returnConfigOption ($name, $default = null) |
|
159
|
|
|
{ |
|
160
|
13 |
|
return (isset($this->configuration[$name]) ? $this->configuration[$name] : $default); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
private function defaultConfiguration () |
|
164
|
|
|
{ |
|
165
|
|
|
$defaultConfig = array( |
|
166
|
|
|
'baseurl' => '', |
|
167
|
|
|
'target' => '_site', |
|
168
|
|
|
'twig' => array( |
|
169
|
|
|
'autoescape' => false |
|
170
|
|
|
), |
|
171
|
|
|
'include' => array( |
|
172
|
|
|
'.htaccess' |
|
173
|
|
|
), |
|
174
|
|
|
'exclude' => array( |
|
175
|
|
|
'/^_.*/', |
|
176
|
|
|
'*.twig', |
|
177
|
|
|
'node_modules/', |
|
178
|
|
|
'stakx-theme.yml' |
|
179
|
|
|
) |
|
180
|
|
|
); |
|
181
|
|
|
|
|
182
|
|
|
if (is_array($this->configuration)) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->configuration = ArrayUtilities::array_merge_defaults($defaultConfig, $this->configuration, 'name'); |
|
185
|
|
|
} |
|
186
|
|
|
else |
|
187
|
|
|
{ |
|
188
|
|
|
$this->configuration = $defaultConfig; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
private function handleDeprecations () |
|
193
|
|
|
{ |
|
194
|
|
|
// @TODO 1.0.0 handle 'base' deprecation in _config.yml |
|
|
|
|
|
|
195
|
|
|
$base = $this->returnConfigOption('base'); |
|
196
|
|
|
|
|
197
|
|
|
if (!is_null($base)) |
|
198
|
|
|
{ |
|
199
|
|
|
$this->output->warning("The 'base' configuration option has been replaced by 'baseurl' and will be removed in in version 1.0.0."); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.