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