1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stitcher\Task; |
4
|
|
|
|
5
|
|
|
use Stitcher\Page\Page; |
6
|
|
|
use Stitcher\Page\PageParser; |
7
|
|
|
use Stitcher\Page\PageRenderer; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
abstract class AbstractParse implements Task |
11
|
|
|
{ |
12
|
|
|
protected $outputDirectory; |
13
|
|
|
protected $configurationFile; |
14
|
|
|
protected $pageParser; |
15
|
|
|
protected $pageRenderer; |
16
|
|
|
|
17
|
12 |
|
public function __construct( |
18
|
|
|
string $outputDirectory, |
19
|
|
|
string $configurationFile, |
20
|
|
|
PageParser $pageParser, |
21
|
|
|
PageRenderer $pageRenderer |
22
|
|
|
) { |
23
|
12 |
|
$this->outputDirectory = rtrim($outputDirectory, '/'); |
24
|
12 |
|
$this->configurationFile = $configurationFile; |
25
|
12 |
|
$this->pageParser = $pageParser; |
26
|
12 |
|
$this->pageRenderer = $pageRenderer; |
27
|
12 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $outputDirectory |
31
|
|
|
* @param string $configurationFile |
32
|
|
|
* @param \Stitcher\Page\PageParser $pageParser |
33
|
|
|
* @param \Stitcher\Page\PageRenderer $pageRenderer |
34
|
|
|
* |
35
|
|
|
* @return static |
36
|
|
|
*/ |
37
|
9 |
|
public static function make( |
38
|
|
|
string $outputDirectory, |
39
|
|
|
string $configurationFile, |
40
|
|
|
PageParser $pageParser, |
41
|
|
|
PageRenderer $pageRenderer |
42
|
|
|
) { |
43
|
9 |
|
return new static($outputDirectory, $configurationFile, $pageParser, $pageRenderer); |
44
|
|
|
} |
45
|
|
|
|
46
|
10 |
|
protected function parsePageConfiguration($config): array |
47
|
|
|
{ |
48
|
10 |
|
$pages = []; |
49
|
|
|
|
50
|
10 |
|
foreach ($config as $pageId => $pageConfiguration) { |
51
|
10 |
|
$pageConfiguration['id'] = $pageConfiguration['id'] ?? $pageId; |
52
|
|
|
|
53
|
10 |
|
$pages += $this->pageParser->parse($pageConfiguration)->toArray(); |
54
|
|
|
} |
55
|
|
|
|
56
|
10 |
|
return $pages; |
57
|
|
|
} |
58
|
|
|
|
59
|
10 |
|
protected function renderPages($pages): void |
60
|
|
|
{ |
61
|
10 |
|
$fs = new Filesystem(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string $pageId |
65
|
|
|
* @var Page $page |
66
|
|
|
*/ |
67
|
10 |
|
foreach ($pages as $pageId => $page) { |
68
|
10 |
|
$fileName = $pageId === '/' ? 'index' : $pageId; |
69
|
|
|
|
70
|
10 |
|
$renderedPage = $this->pageRenderer->render($page); |
71
|
|
|
|
72
|
10 |
|
$fs->dumpFile("{$this->outputDirectory}/{$fileName}.html", $renderedPage); |
73
|
|
|
} |
74
|
10 |
|
} |
75
|
|
|
} |
76
|
|
|
|