1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace allejo\stakx\Object; |
4
|
|
|
|
5
|
|
|
use allejo\stakx\Core\StakxLogger; |
6
|
|
|
use allejo\stakx\Manager\AssetManager; |
7
|
|
|
use allejo\stakx\Manager\PageManager; |
8
|
|
|
use allejo\stakx\Manager\ThemeManager; |
9
|
|
|
use allejo\stakx\System\FileExplorer; |
10
|
|
|
use allejo\stakx\System\Filesystem; |
11
|
|
|
use allejo\stakx\Manager\CollectionManager; |
12
|
|
|
use allejo\stakx\Manager\DataManager; |
13
|
|
|
use allejo\stakx\System\Folder; |
14
|
|
|
use JasonLewis\ResourceWatcher\Event; |
15
|
|
|
use JasonLewis\ResourceWatcher\Resource\FileResource; |
16
|
|
|
use JasonLewis\ResourceWatcher\Tracker; |
17
|
|
|
use JasonLewis\ResourceWatcher\Watcher; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
class Website |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The location of where the compiled website will be written to |
24
|
|
|
* |
25
|
|
|
* @var Folder |
26
|
|
|
*/ |
27
|
|
|
private $outputDirectory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The main configuration to be used to build the specified website |
31
|
|
|
* |
32
|
|
|
* @var Configuration |
33
|
|
|
*/ |
34
|
|
|
private $configuration; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* When set to true, the Stakx website will be built without a configuration file |
38
|
|
|
* |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
private $confLess; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* When set to true, Twig templates will not have access to filters or functions which provide access to the |
45
|
|
|
* filesystem |
46
|
|
|
* |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
private $safeMode; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* When set to true, Stakx will not clean the _site folder after a rebuild |
53
|
|
|
* |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
private $noClean; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var StakxLogger |
60
|
|
|
*/ |
61
|
|
|
private $output; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var AssetManager |
65
|
|
|
*/ |
66
|
|
|
private $am; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var CollectionManager |
70
|
|
|
*/ |
71
|
|
|
private $cm; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var DataManager |
75
|
|
|
*/ |
76
|
|
|
private $dm; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var Filesystem |
80
|
|
|
*/ |
81
|
|
|
private $fs; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var PageManager |
85
|
|
|
*/ |
86
|
|
|
private $pm; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var ThemeManager |
90
|
|
|
*/ |
91
|
|
|
private $tm; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Website constructor. |
95
|
|
|
* |
96
|
|
|
* @param OutputInterface $output |
97
|
|
|
*/ |
98
|
|
|
public function __construct (OutputInterface $output) |
99
|
|
|
{ |
100
|
|
|
$this->output = new StakxLogger($output); |
101
|
|
|
$this->cm = new CollectionManager(); |
102
|
|
|
$this->dm = new DataManager(); |
103
|
|
|
$this->pm = new PageManager(); |
104
|
|
|
$this->fs = new Filesystem(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Compile the website. |
109
|
|
|
* |
110
|
|
|
* @param bool $tracking Whether or not to keep track of files as they're compiled to save time in 'watch' |
111
|
|
|
*/ |
112
|
|
|
public function build ($tracking = false) |
113
|
|
|
{ |
114
|
|
|
// Configure the environment |
115
|
|
|
$this->createFolderStructure(!$this->noClean); |
116
|
|
|
|
117
|
|
|
// Our output directory |
118
|
|
|
$this->outputDirectory = new Folder($this->getConfiguration()->getTargetFolder()); |
119
|
|
|
$this->outputDirectory->setTargetDirectory($this->getConfiguration()->getBaseUrl()); |
120
|
|
|
|
121
|
|
|
// Parse DataItems |
122
|
|
|
$this->dm->setLogger($this->output); |
123
|
|
|
$this->dm->enableTracking($tracking); |
124
|
|
|
$this->dm->parseDataItems($this->getConfiguration()->getDataFolders()); |
125
|
|
|
$this->dm->parseDataSets($this->getConfiguration()->getDataSets()); |
126
|
|
|
|
127
|
|
|
// Prepare Collections |
128
|
|
|
$this->cm->setLogger($this->output); |
129
|
|
|
$this->cm->enableTracking($tracking); |
130
|
|
|
$this->cm->parseCollections($this->getConfiguration()->getCollectionsFolders()); |
131
|
|
|
|
132
|
|
|
// Handle PageViews |
133
|
|
|
$this->pm->setLogger($this->output); |
134
|
|
|
$this->pm->setTargetFolder($this->outputDirectory); |
135
|
|
|
$this->pm->setCollections($this->cm->getCollections()); |
136
|
|
|
$this->pm->setRedirectTemplate($this->getConfiguration()->getRedirectTemplate()); |
137
|
|
|
$this->pm->enableTracking($tracking); |
138
|
|
|
$this->pm->parsePageViews($this->getConfiguration()->getPageViewFolders()); |
139
|
|
|
$this->pm->configureTwig($this->getConfiguration(), array( |
140
|
|
|
'safe' => $this->safeMode, |
141
|
|
|
'globals' => array( |
142
|
|
|
array('name' => 'site', 'value' => $this->getConfiguration()->getConfiguration()), |
143
|
|
|
array('name' => 'collections', 'value' => $this->cm->getJailedCollections()), |
144
|
|
|
array('name' => 'menu', 'value' => $this->pm->getSiteMenu()), |
145
|
|
|
array('name' => 'pages', 'value' => $this->pm->getFlatPages()), |
146
|
|
|
array('name' => 'data', 'value' => $this->dm->getDataItems()) |
147
|
|
|
) |
148
|
|
|
)); |
149
|
|
|
$this->pm->compileAll(); |
150
|
|
|
|
151
|
|
|
// At this point, we are looking at static files to copy over meaning we need to ignore all of the files that |
152
|
|
|
// make up the source of a stakx website |
153
|
|
|
$assetsToIgnore = array_merge( |
154
|
|
|
Configuration::$stakxSourceFiles, |
155
|
|
|
$this->getConfiguration()->getExcludes() |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
// |
159
|
|
|
// Theme Management |
160
|
|
|
// |
161
|
|
|
$theme = $this->configuration->getTheme(); |
162
|
|
|
|
163
|
|
|
if (!is_null($theme)) |
164
|
|
|
{ |
165
|
|
|
$this->output->notice("Looking for '${theme}' theme..."); |
166
|
|
|
|
167
|
|
|
$this->tm = new ThemeManager($theme); |
168
|
|
|
$this->tm->configureFinder($this->getConfiguration()->getIncludes(), $assetsToIgnore); |
169
|
|
|
$this->tm->setLogger($this->output); |
170
|
|
|
$this->tm->enableTracking($tracking); |
171
|
|
|
$this->tm->setFolder($this->outputDirectory); |
172
|
|
|
$this->tm->copyFiles(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// |
176
|
|
|
// Static file management |
177
|
|
|
// |
178
|
|
|
$this->am = new AssetManager(); |
179
|
|
|
$this->am->configureFinder($this->getConfiguration()->getIncludes(), $assetsToIgnore); |
180
|
|
|
$this->am->setLogger($this->output); |
181
|
|
|
$this->am->setFolder($this->outputDirectory); |
182
|
|
|
$this->am->enableTracking($tracking); |
183
|
|
|
$this->am->copyFiles(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function watch () |
187
|
|
|
{ |
188
|
|
|
$this->output->writeln('Building website...'); |
189
|
|
|
$this->build(true); |
190
|
|
|
$this->output->writeln(sprintf('Watching %s', getcwd())); |
191
|
|
|
|
192
|
|
|
$fileExplorer = FileExplorer::create( |
193
|
|
|
getcwd(), |
194
|
|
|
array_merge($this->getConfiguration()->getExcludes(), array( |
195
|
|
|
$this->getConfiguration()->getTargetFolder() |
196
|
|
|
)), |
197
|
|
|
$this->getConfiguration()->getIncludes() |
198
|
|
|
); |
199
|
|
|
$tracker = new Tracker(); |
200
|
|
|
$watcher = new Watcher($tracker, $this->fs); |
201
|
|
|
$listener = $watcher->watch(getcwd(), $fileExplorer->getExplorer()); |
202
|
|
|
$targetPath = $this->getConfiguration()->getTargetFolder(); |
203
|
|
|
|
204
|
|
|
$this->output->writeln('Watch started successfully'); |
205
|
|
|
|
206
|
|
|
$listener->onAnything(function (Event $event, FileResource $resouce, $path) use ($targetPath) |
207
|
|
|
{ |
208
|
|
|
$filePath = $this->fs->getRelativePath($path); |
209
|
|
|
|
210
|
|
|
try |
211
|
|
|
{ |
212
|
|
|
switch ($event->getCode()) |
213
|
|
|
{ |
214
|
|
|
case Event::RESOURCE_CREATED: |
215
|
|
|
$this->creationWatcher($filePath); |
216
|
|
|
break; |
217
|
|
|
|
218
|
|
|
case Event::RESOURCE_MODIFIED: |
219
|
|
|
$this->modificationWatcher($filePath); |
220
|
|
|
break; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
catch (\Exception $e) |
224
|
|
|
{ |
225
|
|
|
$this->output->error(sprintf("Your website failed to build with the following error: %s", |
226
|
|
|
$e->getMessage() |
227
|
|
|
)); |
228
|
|
|
} |
229
|
|
|
}); |
230
|
|
|
|
231
|
|
|
$watcher->start(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return Configuration |
236
|
|
|
*/ |
237
|
|
|
public function getConfiguration () |
238
|
|
|
{ |
239
|
|
|
return $this->configuration; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $configFile |
244
|
|
|
* |
245
|
|
|
* @throws \LogicException |
246
|
|
|
*/ |
247
|
|
|
public function setConfiguration ($configFile) |
248
|
|
|
{ |
249
|
|
|
if (!$this->fs->exists($configFile) && !$this->isConfLess()) |
250
|
|
|
{ |
251
|
|
|
$this->output->error("You are trying to build a website in a directory without a configuration file. Is this what you meant to do?"); |
252
|
|
|
$this->output->error("To build a website without a configuration, use the '--no-conf' option"); |
253
|
|
|
|
254
|
|
|
throw new \LogicException("Cannot build a website without a configuration when not in Configuration-less mode"); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if ($this->isConfLess()) |
258
|
|
|
{ |
259
|
|
|
$configFile = ""; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$this->configuration = new Configuration(); |
263
|
|
|
$this->configuration->setLogger($this->output); |
264
|
|
|
$this->configuration->parseConfiguration($configFile); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get whether or not the website is being built in Configuration-less mode |
269
|
|
|
* |
270
|
|
|
* @return bool True when being built with no configuration file |
271
|
|
|
*/ |
272
|
|
|
public function isConfLess () |
273
|
|
|
{ |
274
|
|
|
return $this->confLess; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Set whether or not the website should be built with a configuration |
279
|
|
|
* |
280
|
|
|
* @param bool $status True when a website should be built without a configuration |
281
|
|
|
*/ |
282
|
|
|
public function setConfLess ($status) |
283
|
|
|
{ |
284
|
|
|
$this->confLess = $status; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Get whether or not the website is being built in safe mode. |
289
|
|
|
* |
290
|
|
|
* Safe mode is defined as disabling file system access from Twig and disabling user Twig extensions |
291
|
|
|
* |
292
|
|
|
* @return bool True when the website is being built in safe mode |
293
|
|
|
*/ |
294
|
|
|
public function isSafeMode () |
295
|
|
|
{ |
296
|
|
|
return $this->safeMode; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Set whether a website should be built in safe mode |
301
|
|
|
* |
302
|
|
|
* @param bool $bool True if a website should be built in safe mode |
303
|
|
|
*/ |
304
|
|
|
public function setSafeMode ($bool) |
305
|
|
|
{ |
306
|
|
|
$this->safeMode = $bool; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @return boolean |
311
|
|
|
*/ |
312
|
|
|
public function isNoClean() |
313
|
|
|
{ |
314
|
|
|
return $this->noClean; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param boolean $noClean |
319
|
|
|
*/ |
320
|
|
|
public function setNoClean($noClean) |
321
|
|
|
{ |
322
|
|
|
$this->noClean = $noClean; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
private function creationWatcher ($filePath) |
326
|
|
|
{ |
327
|
|
|
$this->output->writeln(sprintf("File creation detected: %s", $filePath)); |
328
|
|
|
|
329
|
|
|
if ($this->pm->isHandled($filePath)) |
330
|
|
|
{ |
331
|
|
|
$this->pm->createNewItem($filePath); |
332
|
|
|
$this->pm->refreshItem($filePath); |
333
|
|
|
} |
334
|
|
|
else if ($this->cm->isHandled($filePath)) |
335
|
|
|
{ |
336
|
|
|
$contentItem = $this->cm->createNewItem($filePath); |
337
|
|
|
|
338
|
|
|
$this->pm->updateTwigVariable('collections', $this->cm->getCollections()); |
|
|
|
|
339
|
|
|
$this->pm->updatePageView($contentItem); |
340
|
|
|
$this->pm->compileContentItem($contentItem); |
341
|
|
|
$this->pm->compileSome(array( |
342
|
|
|
'namespace' => 'collections', |
343
|
|
|
'dependency' => $contentItem->getCollection() |
344
|
|
|
)); |
345
|
|
|
} |
346
|
|
View Code Duplication |
else if ($this->dm->isHandled($filePath)) |
|
|
|
|
347
|
|
|
{ |
348
|
|
|
$change = $this->dm->createNewItem($filePath); |
349
|
|
|
|
350
|
|
|
$this->pm->updateTwigVariable('data', $this->dm->getDataItems()); |
|
|
|
|
351
|
|
|
$this->pm->compileSome(array( |
352
|
|
|
'namespace' => 'data', |
353
|
|
|
'dependency' => $change |
354
|
|
|
)); |
355
|
|
|
} |
356
|
|
|
else if (!is_null($this->tm) && $this->tm->isHandled($filePath)) |
357
|
|
|
{ |
358
|
|
|
$this->tm->createNewItem($filePath); |
359
|
|
|
} |
360
|
|
|
else if ($this->am->isHandled($filePath)) |
361
|
|
|
{ |
362
|
|
|
$this->am->createNewItem($filePath); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
private function modificationWatcher ($filePath) |
367
|
|
|
{ |
368
|
|
|
$this->output->writeln(sprintf("File change detected: %s", $filePath)); |
369
|
|
|
|
370
|
|
|
if ($this->pm->isTracked($filePath)) |
371
|
|
|
{ |
372
|
|
|
$this->pm->refreshItem($filePath); |
373
|
|
|
} |
374
|
|
|
else if ($this->cm->isTracked($filePath)) |
375
|
|
|
{ |
376
|
|
|
$contentItem = &$this->cm->getContentItem($filePath); |
377
|
|
|
$contentItem->refreshFileContent(); |
378
|
|
|
|
379
|
|
|
$this->pm->compileContentItem($contentItem); |
380
|
|
|
$this->pm->compileSome(array( |
381
|
|
|
'namespace' => 'collections', |
382
|
|
|
'dependency' => $contentItem->getCollection() |
383
|
|
|
)); |
384
|
|
|
} |
385
|
|
View Code Duplication |
else if ($this->dm->isTracked($filePath)) |
|
|
|
|
386
|
|
|
{ |
387
|
|
|
$change = $this->dm->refreshItem($filePath); |
388
|
|
|
|
389
|
|
|
$this->pm->updateTwigVariable('data', $this->dm->getDataItems()); |
|
|
|
|
390
|
|
|
$this->pm->compileSome(array( |
391
|
|
|
'namespace' => 'data', |
392
|
|
|
'dependency' => $change |
393
|
|
|
)); |
394
|
|
|
} |
395
|
|
|
else if (!is_null($this->tm) && $this->tm->isTracked($filePath)) |
396
|
|
|
{ |
397
|
|
|
$this->tm->refreshItem($filePath); |
398
|
|
|
} |
399
|
|
|
else if ($this->am->isTracked($filePath)) |
400
|
|
|
{ |
401
|
|
|
$this->am->refreshItem($filePath); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Prepare the Stakx environment by creating necessary cache folders |
407
|
|
|
* |
408
|
|
|
* @param bool $cleanDirectory Clean the target directory |
409
|
|
|
*/ |
410
|
|
|
private function createFolderStructure ($cleanDirectory) |
411
|
|
|
{ |
412
|
|
|
$tarDir = $this->fs->absolutePath($this->configuration->getTargetFolder()); |
413
|
|
|
|
414
|
|
|
if ($cleanDirectory) |
415
|
|
|
{ |
416
|
|
|
$this->fs->remove($tarDir); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
$this->fs->remove($this->fs->absolutePath('.stakx-cache')); |
420
|
|
|
$this->fs->mkdir($this->fs->absolutePath('.stakx-cache/twig')); |
421
|
|
|
$this->fs->mkdir($tarDir); |
422
|
|
|
} |
423
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: