1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cecil. |
5
|
|
|
* |
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cecil\Generator; |
15
|
|
|
|
16
|
|
|
use Cecil\Collection\Page\Page; |
17
|
|
|
use Cecil\Collection\Page\Type; |
18
|
|
|
use Cecil\Exception\RuntimeException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* VirtualPages class. |
22
|
|
|
* |
23
|
|
|
* This class is responsible for generating virtual pages based on the configuration provided. |
24
|
|
|
* It extends the AbstractGenerator and implements the GeneratorInterface. |
25
|
|
|
* It collects pages from the configuration under the 'pages.virtual' key and creates Page objects |
26
|
|
|
* for each virtual page defined in the configuration. |
27
|
|
|
* Each page can have its own frontmatter, and the generator ensures that the pages are created |
28
|
|
|
* with the correct path and language settings. |
29
|
|
|
* If a page is marked as unpublished or does not have a path defined, it will be skipped. |
30
|
|
|
* If a page already exists with the same ID, it will also be skipped. |
31
|
|
|
* The generated pages are added to the collection of generated pages for further processing. |
32
|
|
|
*/ |
33
|
|
|
class VirtualPages extends AbstractGenerator implements GeneratorInterface |
34
|
|
|
{ |
35
|
|
|
protected $configKey = 'pages.virtual'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
1 |
|
public function generate(): void |
41
|
|
|
{ |
42
|
1 |
|
$pagesConfig = $this->collectPagesFromConfig($this->configKey); |
43
|
|
|
|
44
|
1 |
|
if (!$pagesConfig) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
foreach ($pagesConfig as $frontmatter) { |
49
|
1 |
|
if (isset($frontmatter['published']) && $frontmatter['published'] === false) { |
50
|
1 |
|
continue; |
51
|
|
|
} |
52
|
1 |
|
if (!isset($frontmatter['path'])) { |
53
|
|
|
throw new RuntimeException(\sprintf('Each pages in "%s" config\'s section must have a "path".', $this->configKey)); |
54
|
|
|
} |
55
|
1 |
|
$path = Page::slugify($frontmatter['path']); |
56
|
1 |
|
foreach ($this->config->getLanguages() as $language) { |
57
|
1 |
|
$pageId = !empty($path) ? $path : 'index'; |
58
|
1 |
|
if ($language['code'] !== $this->config->getLanguageDefault()) { |
59
|
1 |
|
$pageId = \sprintf('%s/%s', $language['code'], $pageId); |
60
|
|
|
// disable multilingual support |
61
|
1 |
|
if (isset($frontmatter['multilingual']) && $frontmatter['multilingual'] === false) { |
62
|
1 |
|
continue; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
// abord if the page id already exists |
66
|
1 |
|
if ($this->builder->getPages()->has($pageId)) { |
67
|
1 |
|
continue; |
68
|
|
|
} |
69
|
1 |
|
$page = (new Page($pageId)) |
70
|
1 |
|
->setPath($path) |
71
|
1 |
|
->setType(Type::PAGE->value) |
72
|
1 |
|
->setVariable('language', $language['code']) |
73
|
1 |
|
->setVariable('langref', $path); |
74
|
1 |
|
$page->setVariables($frontmatter); |
75
|
1 |
|
$this->generatedPages->add($page); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Collects virtual pages configuration. |
82
|
|
|
*/ |
83
|
1 |
|
private function collectPagesFromConfig(string $configKey): ?array |
84
|
|
|
{ |
85
|
1 |
|
return $this->config->get($configKey); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|