|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Cecil/Cecil package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Cecil\Generator; |
|
12
|
|
|
|
|
13
|
|
|
use Cecil\Collection\Page\Page; |
|
14
|
|
|
use Cecil\Collection\Page\Type; |
|
15
|
|
|
use Cecil\Exception\Exception; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Generator\VirtualPages. |
|
19
|
|
|
*/ |
|
20
|
|
|
class VirtualPages extends AbstractGenerator implements GeneratorInterface |
|
21
|
|
|
{ |
|
22
|
|
|
protected $configKey = 'virtualpages'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
1 |
|
public function generate(): void |
|
28
|
|
|
{ |
|
29
|
1 |
|
$pagesConfig = $this->collectPagesFromConfig($this->configKey); |
|
30
|
|
|
|
|
31
|
1 |
|
if (!$pagesConfig) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
foreach ($pagesConfig as $frontmatter) { |
|
36
|
1 |
|
if (isset($frontmatter['published']) && $frontmatter['published'] === false) { |
|
37
|
1 |
|
continue; |
|
38
|
|
|
} |
|
39
|
1 |
|
if (!array_key_exists('path', $frontmatter)) { |
|
40
|
|
|
throw new Exception(sprintf( |
|
41
|
|
|
'Each pages in "%s" config\'s section must have a "path".', |
|
42
|
|
|
$this->configKey |
|
43
|
|
|
)); |
|
44
|
|
|
} |
|
45
|
1 |
|
$path = Page::slugify($frontmatter['path']); |
|
46
|
1 |
|
foreach ($this->config->getLanguages() as $language) { |
|
47
|
|
|
$id = !empty($path) ? $path : 'index'; |
|
48
|
1 |
|
if ($language['code'] !== $this->config->getLanguageDefault()) { |
|
49
|
1 |
|
$id .= '.'.$language['code']; |
|
50
|
|
|
if (isset($frontmatter['multilingual']) && $frontmatter['multilingual'] === false) { |
|
51
|
1 |
|
continue; |
|
52
|
1 |
|
} |
|
53
|
1 |
|
} |
|
54
|
1 |
|
if ($this->builder->getPages()->has($id)) { |
|
55
|
1 |
|
continue; |
|
56
|
|
|
} |
|
57
|
1 |
|
$page = (new Page($id)) |
|
58
|
|
|
->setPath($path) |
|
59
|
|
|
->setType(Type::PAGE) |
|
60
|
|
|
->setVariable('language', $language['code']) |
|
61
|
|
|
->setVariable('langref', $path); |
|
62
|
|
|
$page->setVariables($frontmatter); |
|
63
|
|
|
$this->generatedPages->add($page); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
/** |
|
69
|
|
|
* Collects virtual pages configuration. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $configKey |
|
72
|
|
|
* |
|
73
|
|
|
* @return array|null |
|
74
|
|
|
*/ |
|
75
|
|
|
private function collectPagesFromConfig(string $configKey): ?array |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->config->get($configKey); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|