|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Cecil; |
|
10
|
|
|
|
|
11
|
|
|
use Cecil\Exception\Exception; |
|
12
|
|
|
use Dflydev\DotAccessData\Data; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Config. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Config |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Config. |
|
21
|
|
|
* |
|
22
|
|
|
* @var Data |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $data; |
|
25
|
|
|
/** |
|
26
|
|
|
* Source directory. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $sourceDir; |
|
31
|
|
|
/** |
|
32
|
|
|
* Destination directory. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $destinationDir; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Config constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param Config|array|null $config |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($config = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$data = new Data(include __DIR__.'/../config/default.php'); |
|
46
|
|
|
|
|
47
|
|
|
if ($config) { |
|
48
|
|
|
if ($config instanceof self) { |
|
49
|
|
|
$data->importData($config->getAll()); |
|
50
|
|
|
} elseif (is_array($config)) { |
|
51
|
|
|
$data->import($config); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Apply environment variables. |
|
57
|
|
|
*/ |
|
58
|
|
|
$applyEnv = function ($array) use ($data) { |
|
59
|
|
|
$iterator = new \RecursiveIteratorIterator( |
|
60
|
|
|
new \RecursiveArrayIterator($array), |
|
61
|
|
|
\RecursiveIteratorIterator::SELF_FIRST |
|
62
|
|
|
); |
|
63
|
|
|
foreach ($iterator as $leafValue) { |
|
64
|
|
|
$path = []; |
|
65
|
|
|
foreach (range(0, $iterator->getDepth()) as $depth) { |
|
66
|
|
|
$path[] = $iterator->getSubIterator($depth)->key(); |
|
67
|
|
|
} |
|
68
|
|
|
$sPath = implode('_', $path); |
|
69
|
|
|
if ($getEnv = getenv('CECIL_'.strtoupper($sPath))) { |
|
70
|
|
|
$data->set(str_replace('_', '.', strtolower($sPath)), $getEnv); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
}; |
|
74
|
|
|
$applyEnv($data->export()); |
|
75
|
|
|
|
|
76
|
|
|
$this->setFromData($data); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Import array config to current config. |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $config |
|
83
|
|
|
*/ |
|
84
|
|
|
public function import($config) |
|
85
|
|
|
{ |
|
86
|
|
|
if (is_array($config)) { |
|
87
|
|
|
$data = $this->getAll(); |
|
88
|
|
|
$origin = $data->export(); |
|
89
|
|
|
$data->import($config); |
|
90
|
|
|
$data->import($origin); |
|
91
|
|
|
$this->setFromData($data); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set config data. |
|
97
|
|
|
* |
|
98
|
|
|
* @param Data $data |
|
99
|
|
|
* |
|
100
|
|
|
* @return $this |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function setFromData(Data $data) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($this->data !== $data) { |
|
105
|
|
|
$this->data = $data; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get config data. |
|
113
|
|
|
* |
|
114
|
|
|
* @return Data |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getAll() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->data; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get data as array. |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getAllAsArray() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->data->export(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Return a config value. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $key |
|
135
|
|
|
* @param string $default |
|
136
|
|
|
* |
|
137
|
|
|
* @return array|mixed|null |
|
138
|
|
|
*/ |
|
139
|
|
|
public function get($key, $default = '') |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->data->get($key, $default); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Set source directory. |
|
146
|
|
|
* |
|
147
|
|
|
* @param null $sourceDir |
|
148
|
|
|
* |
|
149
|
|
|
* @throws Exception |
|
150
|
|
|
* |
|
151
|
|
|
* @return $this |
|
152
|
|
|
*/ |
|
153
|
|
View Code Duplication |
public function setSourceDir($sourceDir = null) |
|
154
|
|
|
{ |
|
155
|
|
|
if ($sourceDir === null) { |
|
156
|
|
|
$sourceDir = getcwd(); |
|
157
|
|
|
} |
|
158
|
|
|
if (!is_dir($sourceDir)) { |
|
159
|
|
|
throw new \InvalidArgumentException(sprintf("'%s' is not a valid source directory.", $sourceDir)); |
|
160
|
|
|
} |
|
161
|
|
|
$this->sourceDir = $sourceDir; |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get source directory. |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getSourceDir() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->sourceDir; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Set destination directory. |
|
178
|
|
|
* |
|
179
|
|
|
* @param null $destinationDir |
|
180
|
|
|
* |
|
181
|
|
|
* @throws Exception |
|
182
|
|
|
* |
|
183
|
|
|
* @return $this |
|
184
|
|
|
*/ |
|
185
|
|
View Code Duplication |
public function setDestinationDir($destinationDir = null) |
|
186
|
|
|
{ |
|
187
|
|
|
if ($destinationDir === null) { |
|
188
|
|
|
$destinationDir = $this->sourceDir; |
|
189
|
|
|
} |
|
190
|
|
|
if (!is_dir($destinationDir)) { |
|
191
|
|
|
throw new \InvalidArgumentException(sprintf("'%s' is not a valid destination directory.", $destinationDir)); |
|
192
|
|
|
} |
|
193
|
|
|
$this->destinationDir = $destinationDir; |
|
194
|
|
|
|
|
195
|
|
|
return $this; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Get destination directory. |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getDestinationDir() |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->destinationDir; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Path helpers. |
|
210
|
|
|
*/ |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Return content directory path. |
|
214
|
|
|
* |
|
215
|
|
|
* @return string |
|
216
|
|
|
*/ |
|
217
|
|
|
public function getContentPath() |
|
218
|
|
|
{ |
|
219
|
|
|
return $this->getSourceDir().'/'.$this->get('content.dir'); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Return templates directory path. |
|
224
|
|
|
* |
|
225
|
|
|
* @return string |
|
226
|
|
|
*/ |
|
227
|
|
|
public function getLayoutsPath() |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->getSourceDir().'/'.$this->get('layouts.dir'); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Return themes directory path. |
|
234
|
|
|
* |
|
235
|
|
|
* @return string |
|
236
|
|
|
*/ |
|
237
|
|
|
public function getThemesPath() |
|
238
|
|
|
{ |
|
239
|
|
|
return $this->getSourceDir().'/'.$this->get('themes.dir'); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Return internal templates directory path. |
|
244
|
|
|
* |
|
245
|
|
|
* @return string |
|
246
|
|
|
*/ |
|
247
|
|
|
public function getInternalLayoutsPath() |
|
248
|
|
|
{ |
|
249
|
|
|
return __DIR__.'/../'.$this->get('layouts.internal.dir'); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Return output directory path. |
|
254
|
|
|
* |
|
255
|
|
|
* @return string |
|
256
|
|
|
*/ |
|
257
|
|
|
public function getOutputPath() |
|
258
|
|
|
{ |
|
259
|
|
|
return $this->getDestinationDir().'/'.$this->get('site.output.dir'); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Return static files directory path. |
|
264
|
|
|
* |
|
265
|
|
|
* @return string |
|
266
|
|
|
*/ |
|
267
|
|
|
public function getStaticPath() |
|
268
|
|
|
{ |
|
269
|
|
|
return $this->getSourceDir().'/'.$this->get('static.dir'); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Themes helpers. |
|
274
|
|
|
*/ |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Return theme(s). |
|
278
|
|
|
* |
|
279
|
|
|
* @return array|null |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getTheme() |
|
282
|
|
|
{ |
|
283
|
|
|
if ($themes = $this->get('theme')) { |
|
284
|
|
|
if (is_array($themes)) { |
|
285
|
|
|
return $themes; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
return [$themes]; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Has a (valid) theme(s)? |
|
294
|
|
|
* |
|
295
|
|
|
* @throws Exception |
|
296
|
|
|
* |
|
297
|
|
|
* @return bool |
|
298
|
|
|
*/ |
|
299
|
|
|
public function hasTheme() |
|
300
|
|
|
{ |
|
301
|
|
|
if ($themes = $this->getTheme()) { |
|
302
|
|
|
foreach ($themes as $theme) { |
|
303
|
|
|
if (!Util::getFS()->exists($this->getThemeDirPath($theme, 'layouts'))) { |
|
304
|
|
|
throw new Exception(sprintf( |
|
305
|
|
|
"Theme directory '%s/%s/layouts' not found!", |
|
306
|
|
|
$this->getThemesPath(), |
|
307
|
|
|
$theme |
|
308
|
|
|
)); |
|
309
|
|
|
} |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
return true; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
return false; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Return the path of a specific theme's directory. |
|
320
|
|
|
* |
|
321
|
|
|
* @param string $theme |
|
322
|
|
|
* @param string $dir |
|
323
|
|
|
* |
|
324
|
|
|
* @return string |
|
325
|
|
|
*/ |
|
326
|
|
|
public function getThemeDirPath($theme, $dir = 'layouts') |
|
327
|
|
|
{ |
|
328
|
|
|
return $this->getThemesPath().'/'.$theme.'/'.$dir; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
public function getOutputFile($format) { |
|
332
|
|
|
if ($this->get("site.output.formats.$format.uglyurl")) { |
|
333
|
|
|
return null; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
return $this->get("site.output.formats.$format.filename") |
|
337
|
|
|
.'.'.$this->get("site.output.formats.$format.extension"); |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|