|
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\Builder; |
|
17
|
|
|
use Cecil\Collection\Page\Collection as PagesCollection; |
|
18
|
|
|
use Cecil\Util; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Generator abstract class. |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractGenerator implements GeneratorInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var Builder */ |
|
26
|
|
|
protected $builder; |
|
27
|
|
|
|
|
28
|
|
|
/** @var \Cecil\Config */ |
|
29
|
|
|
protected $config; |
|
30
|
|
|
|
|
31
|
|
|
/** @var PagesCollection */ |
|
32
|
|
|
protected $generatedPages; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Builder $builder) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->builder = $builder; |
|
40
|
|
|
$this->config = $builder->getConfig(); |
|
41
|
|
|
// Creates a new empty collection |
|
42
|
|
|
$this->generatedPages = new PagesCollection('generator-' . Util::formatClassName($this, ['lowercase' => true])); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Sets the builder instance. |
|
47
|
|
|
* This method is called by the GeneratorManager when using DI. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setBuilder(Builder $builder): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->builder = $builder; |
|
52
|
|
|
$this->config = $builder->getConfig(); |
|
53
|
|
|
|
|
54
|
|
|
// Initialize generatedPages if not already done |
|
55
|
|
|
if (!isset($this->generatedPages)) { |
|
56
|
|
|
$this->generatedPages = new PagesCollection('generator-' . Util::formatClassName($this, ['lowercase' => true])); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Run the `generate` method of the generator and returns pages. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function runGenerate(): PagesCollection |
|
64
|
|
|
{ |
|
65
|
|
|
$this->generate(); |
|
66
|
|
|
|
|
67
|
|
|
// set default language (e.g.: "en") if necessary |
|
68
|
|
|
$this->generatedPages->map(function (\Cecil\Collection\Page\Page $page) { |
|
69
|
|
|
if ($page->getVariable('language') === null) { |
|
70
|
|
|
$page->setVariable('language', $this->config->getLanguageDefault()); |
|
71
|
|
|
} |
|
72
|
|
|
}); |
|
73
|
|
|
|
|
74
|
|
|
return $this->generatedPages; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|