1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\System\Filesystem; |
11
|
|
|
use allejo\stakx\Utilities\ArrayUtilities; |
12
|
|
|
use Psr\Log\LoggerAwareInterface; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
15
|
|
|
use Symfony\Component\Yaml\Yaml; |
16
|
|
|
|
17
|
|
|
class Configuration implements LoggerAwareInterface |
18
|
|
|
{ |
19
|
|
|
const DEFAULT_NAME = '_config.yml'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A list of regular expressions or files directly related to stakx websites that should not be copied over to the |
23
|
|
|
* compiled website as an asset. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
public static $stakxSourceFiles = array('/^_(?!themes).*/', '/.twig$/'); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* An array representation of the main Yaml configuration. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $configuration; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var LoggerInterface |
38
|
|
|
*/ |
39
|
|
|
private $output; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Filesystem |
43
|
|
|
*/ |
44
|
|
|
private $fs; |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Configuration constructor. |
48
|
|
|
*/ |
49
|
30 |
|
public function __construct() |
50
|
|
|
{ |
51
|
30 |
|
$this->configuration = array(); |
52
|
30 |
|
$this->fs = new Filesystem(); |
|
|
|
|
53
|
30 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Parse a given configuration file and configure this Configuration instance. |
57
|
|
|
* |
58
|
|
|
* This function should be called with 'null' passed when "configuration-less" mode is used |
59
|
|
|
* |
60
|
|
|
* @param string|null $configFile The path to the configuration file. If null, the default configuration will be |
61
|
|
|
* used |
62
|
|
|
*/ |
63
|
30 |
|
public function parseConfiguration($configFile = null) |
64
|
|
|
{ |
65
|
30 |
|
if ($this->fs->exists($configFile)) |
66
|
|
|
{ |
67
|
|
|
try |
68
|
|
|
{ |
69
|
2 |
|
$this->configuration = Yaml::parse(file_get_contents($configFile)); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
catch (ParseException $e) |
72
|
|
|
{ |
73
|
|
|
$this->output->error('Parsing the configuration failed: {message}', array( |
74
|
|
|
'message' => $e->getMessage(), |
75
|
|
|
)); |
76
|
|
|
$this->output->error('Using default configuration...'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
30 |
|
$this->defaultConfiguration(); |
81
|
30 |
|
$this->handleDeprecations(); |
82
|
30 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
17 |
|
public function setLogger(LoggerInterface $logger) |
88
|
|
|
{ |
89
|
17 |
|
$this->output = $logger; |
90
|
17 |
|
} |
91
|
|
|
|
92
|
14 |
|
public function isDebug() |
93
|
|
|
{ |
94
|
14 |
|
return $this->returnConfigOption('debug', false); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl' |
99
|
|
|
* |
100
|
|
|
* @return mixed|null |
101
|
|
|
*/ |
102
|
4 |
|
public function getBaseUrl() |
103
|
|
|
{ |
104
|
4 |
|
$base = $this->returnConfigOption('base'); |
|
|
|
|
105
|
4 |
|
$baseUrl = $this->returnConfigOption('baseurl'); |
106
|
|
|
|
107
|
4 |
|
if (is_null($base) || (!empty($baseUrl))) |
108
|
|
|
{ |
109
|
3 |
|
return $baseUrl; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return $base; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string[] |
117
|
|
|
*/ |
118
|
1 |
|
public function getDataFolders() |
119
|
|
|
{ |
120
|
1 |
|
return $this->returnConfigOption('data'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string[] |
125
|
|
|
*/ |
126
|
1 |
|
public function getDataSets() |
127
|
|
|
{ |
128
|
1 |
|
return $this->returnConfigOption('datasets'); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
public function getIncludes() |
132
|
|
|
{ |
133
|
1 |
|
return $this->returnConfigOption('include', array()); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function getExcludes() |
137
|
|
|
{ |
138
|
1 |
|
return $this->returnConfigOption('exclude', array()); |
139
|
|
|
} |
140
|
|
|
|
141
|
14 |
|
public function getTheme() |
142
|
|
|
{ |
143
|
14 |
|
return $this->returnConfigOption('theme'); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
public function getConfiguration() |
147
|
|
|
{ |
148
|
1 |
|
return $this->configuration; |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
public function getPageViewFolders() |
152
|
|
|
{ |
153
|
1 |
|
return $this->returnConfigOption('pageviews'); |
154
|
|
|
} |
155
|
|
|
|
156
|
3 |
|
public function getTargetFolder() |
157
|
|
|
{ |
158
|
3 |
|
return $this->returnConfigOption('target'); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
public function getCollectionsFolders() |
162
|
|
|
{ |
163
|
1 |
|
return $this->returnConfigOption('collections'); |
164
|
|
|
} |
165
|
|
|
|
166
|
14 |
|
public function getTwigAutoescape() |
|
|
|
|
167
|
|
|
{ |
168
|
14 |
|
return $this->configuration['twig']['autoescape']; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getRedirectTemplate() |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
return $this->configuration['templates']['redirect']; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Return the specified configuration option if available, otherwise return the default. |
178
|
|
|
* |
179
|
|
|
* @param string $name The configuration option to lookup |
180
|
|
|
* @param mixed|null $default The default value returned if the configuration option isn't found |
181
|
|
|
* |
182
|
|
|
* @return mixed|null |
183
|
|
|
*/ |
184
|
30 |
|
private function returnConfigOption($name, $default = null) |
185
|
|
|
{ |
186
|
30 |
|
return isset($this->configuration[$name]) ? $this->configuration[$name] : $default; |
187
|
|
|
} |
188
|
|
|
|
189
|
30 |
|
private function defaultConfiguration() |
190
|
|
|
{ |
191
|
|
|
$defaultConfig = array( |
192
|
30 |
|
'baseurl' => '', |
193
|
30 |
|
'target' => '_site', |
194
|
|
|
'twig' => array( |
195
|
|
|
'autoescape' => false, |
196
|
|
|
), |
197
|
|
|
'include' => array( |
198
|
|
|
'.htaccess', |
199
|
|
|
), |
200
|
|
|
'exclude' => array( |
201
|
30 |
|
'node_modules/', |
202
|
30 |
|
'stakx-theme.yml', |
203
|
30 |
|
self::DEFAULT_NAME, |
204
|
|
|
), |
205
|
|
|
'templates' => array( |
206
|
|
|
'redirect' => false, |
207
|
|
|
), |
208
|
|
|
); |
209
|
|
|
|
210
|
30 |
|
$this->configuration = ArrayUtilities::array_merge_defaults($defaultConfig, $this->configuration, 'name'); |
211
|
30 |
|
} |
212
|
|
|
|
213
|
30 |
|
private function handleDeprecations() |
214
|
|
|
{ |
215
|
|
|
// @TODO 1.0.0 handle 'base' deprecation in _config.yml |
216
|
30 |
|
$base = $this->returnConfigOption('base'); |
217
|
|
|
|
218
|
30 |
|
if (!is_null($base)) |
219
|
|
|
{ |
220
|
2 |
|
$this->output->warning("The 'base' configuration option has been replaced by 'baseurl' and will be removed in in version 1.0.0."); |
221
|
|
|
} |
222
|
30 |
|
} |
223
|
|
|
} |
224
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.