1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stitcher\Task; |
4
|
|
|
|
5
|
|
|
use Pageon\Html\SiteMap; |
6
|
|
|
use Stitcher\Page\Page; |
7
|
|
|
use Stitcher\Page\PageCollection; |
8
|
|
|
use Stitcher\Page\PageParser; |
9
|
|
|
use Stitcher\Page\PageRenderer; |
10
|
|
|
use Stitcher\Task; |
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
|
13
|
|
|
abstract class AbstractParse implements Task |
14
|
|
|
{ |
15
|
|
|
/** @var string */ |
16
|
|
|
protected $publicDirectory; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $configurationFile; |
20
|
|
|
|
21
|
|
|
/** @var PageParser */ |
22
|
|
|
protected $pageParser; |
23
|
|
|
|
24
|
|
|
/** @var PageRenderer */ |
25
|
|
|
protected $pageRenderer; |
26
|
|
|
|
27
|
|
|
/** @var Task[] */ |
28
|
|
|
protected $tasks = []; |
29
|
|
|
|
30
|
|
|
/** @var SiteMap */ |
31
|
|
|
protected $siteMap; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
string $publicDirectory, |
35
|
|
|
string $configurationFile, |
36
|
|
|
PageParser $pageParser, |
37
|
|
|
PageRenderer $pageRenderer, |
38
|
|
|
SiteMap $siteMap |
39
|
|
|
) { |
40
|
|
|
$this->publicDirectory = rtrim($publicDirectory, '/'); |
41
|
|
|
$this->configurationFile = $configurationFile; |
42
|
|
|
$this->pageParser = $pageParser; |
43
|
|
|
$this->pageRenderer = $pageRenderer; |
44
|
|
|
$this->siteMap = $siteMap; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function make( |
48
|
|
|
string $publicDirectory, |
49
|
|
|
string $configurationFile, |
50
|
|
|
PageParser $pageParser, |
51
|
|
|
PageRenderer $pageRenderer, |
52
|
|
|
SiteMap $siteMap |
53
|
|
|
) : self { |
54
|
|
|
return new static( |
55
|
|
|
$publicDirectory, |
56
|
|
|
$configurationFile, |
57
|
|
|
$pageParser, |
58
|
|
|
$pageRenderer, |
59
|
|
|
$siteMap |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function addSubTask(Task $task) |
64
|
|
|
{ |
65
|
|
|
$this->tasks[] = $task; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function parsePageConfiguration($config): PageCollection |
71
|
|
|
{ |
72
|
|
|
$pages = []; |
73
|
|
|
|
74
|
|
|
foreach ($config as $pageId => $pageConfiguration) { |
75
|
|
|
$pageConfiguration['id'] = $pageConfiguration['id'] ?? $pageId; |
76
|
|
|
|
77
|
|
|
$pages += $this->pageParser->parse($pageConfiguration)->toArray(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return new PageCollection($pages); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function renderPages(PageCollection $pages): void |
84
|
|
|
{ |
85
|
|
|
$fs = new Filesystem(); |
86
|
|
|
|
87
|
|
|
$pages->each(function (Page $page, string $pageId) use ($fs) { |
88
|
|
|
$fileName = $pageId === '/' ? 'index' : $pageId; |
89
|
|
|
|
90
|
|
|
$renderedPage = $this->pageRenderer->render($page); |
91
|
|
|
|
92
|
|
|
$this->siteMap->addPath($pageId); |
93
|
|
|
|
94
|
|
|
$fs->dumpFile( |
95
|
|
|
"{$this->publicDirectory}/{$fileName}.html", |
96
|
|
|
$renderedPage |
97
|
|
|
); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function executeSubTasks(): void |
102
|
|
|
{ |
103
|
|
|
foreach ($this->tasks as $task) { |
104
|
|
|
$task->execute(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|