1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Symplify |
7
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Symplify\PHP7_Sculpin\Renderable; |
11
|
|
|
|
12
|
|
|
use SplFileInfo; |
13
|
|
|
use Symplify\PHP7_Sculpin\Output\FileSystemWriter; |
14
|
|
|
use Symplify\PHP7_Sculpin\Configuration\Configuration; |
15
|
|
|
use Symplify\PHP7_Sculpin\Renderable\Configuration\ConfigurationDecorator; |
16
|
|
|
use Symplify\PHP7_Sculpin\Renderable\File\File; |
17
|
|
|
use Symplify\PHP7_Sculpin\Renderable\File\FileFactory; |
18
|
|
|
use Symplify\PHP7_Sculpin\Renderable\File\PostFile; |
19
|
|
|
use Symplify\PHP7_Sculpin\Renderable\Latte\LatteDecorator; |
20
|
|
|
use Symplify\PHP7_Sculpin\Renderable\Markdown\MarkdownDecorator; |
21
|
|
|
use Symplify\PHP7_Sculpin\Renderable\Routing\RouteDecorator; |
22
|
|
|
|
23
|
|
|
final class RenderableFilesProcessor |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var FileFactory |
27
|
|
|
*/ |
28
|
|
|
private $fileFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RouteDecorator |
32
|
|
|
*/ |
33
|
|
|
private $routeDecorator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ConfigurationDecorator |
37
|
|
|
*/ |
38
|
|
|
private $configurationDecorator; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var MarkdownDecorator |
42
|
|
|
*/ |
43
|
|
|
private $markdownDecorator; |
44
|
|
|
/** |
45
|
|
|
* @var LatteDecorator |
46
|
|
|
*/ |
47
|
|
|
private $latteDecorator; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var FileSystemWriter |
51
|
|
|
*/ |
52
|
|
|
private $fileSystemWriter; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Configuration |
56
|
|
|
*/ |
57
|
|
|
private $configuration; |
58
|
|
|
|
59
|
2 |
|
public function __construct( |
60
|
|
|
FileFactory $fileFactory, |
61
|
|
|
RouteDecorator $routeDecorator, |
62
|
|
|
ConfigurationDecorator $configurationDecorator, |
63
|
|
|
MarkdownDecorator $markdownDecorator, |
64
|
|
|
LatteDecorator $latteDecorator, |
65
|
|
|
FileSystemWriter $fileSystemWriter, |
66
|
|
|
Configuration $configuration |
67
|
|
|
) { |
68
|
2 |
|
$this->fileFactory = $fileFactory; |
69
|
2 |
|
$this->routeDecorator = $routeDecorator; |
70
|
2 |
|
$this->configurationDecorator = $configurationDecorator; |
71
|
2 |
|
$this->markdownDecorator = $markdownDecorator; |
72
|
2 |
|
$this->latteDecorator = $latteDecorator; |
73
|
2 |
|
$this->fileSystemWriter = $fileSystemWriter; |
74
|
2 |
|
$this->configuration = $configuration; |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param SplFileInfo[] $fileInfos |
79
|
|
|
*/ |
80
|
2 |
|
public function processFiles(array $fileInfos) |
81
|
|
|
{ |
82
|
2 |
|
if (!count($fileInfos)) { |
83
|
1 |
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
$files = $this->createFileObjectsFromFileInfos($fileInfos); |
87
|
|
|
|
88
|
2 |
|
$this->setPostsToConfiguration($files); |
89
|
2 |
|
$this->setRoutesToFiles($files); |
90
|
|
|
|
91
|
2 |
|
$this->setFileConfigurationToFile($files); |
92
|
2 |
|
$this->formatFileContentFromMarkdownToHtml($files); |
93
|
2 |
|
$this->formatFileContentFromLatteToHtml($files); |
94
|
|
|
|
95
|
2 |
|
$this->fileSystemWriter->copyRenderableFiles($files); |
96
|
2 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $fileInfos |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
2 |
|
private function createFileObjectsFromFileInfos(array $fileInfos):array |
104
|
|
|
{ |
105
|
2 |
|
$files = []; |
106
|
2 |
|
foreach ($fileInfos as $id => $fileInfo) { |
107
|
2 |
|
$files[$id] = $this->fileFactory->create($fileInfo); |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
return $files; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param File[] $files |
115
|
|
|
*/ |
116
|
2 |
|
private function setPostsToConfiguration(array $files) |
117
|
|
|
{ |
118
|
2 |
|
if (reset($files) instanceof PostFile) { |
119
|
|
|
$this->configuration->addOption('posts', $files); |
120
|
|
|
} |
121
|
2 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param File[] $files |
125
|
|
|
*/ |
126
|
2 |
|
private function setRoutesToFiles(array $files) |
127
|
|
|
{ |
128
|
2 |
|
foreach ($files as $file) { |
129
|
2 |
|
$this->routeDecorator->decorateFile($file); |
130
|
|
|
} |
131
|
2 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param File[] $files |
135
|
|
|
*/ |
136
|
2 |
|
private function setFileConfigurationToFile(array $files) |
137
|
|
|
{ |
138
|
2 |
|
foreach ($files as $file) { |
139
|
2 |
|
$this->configurationDecorator->decorateFile($file); |
140
|
|
|
} |
141
|
2 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param File[] $files |
145
|
|
|
*/ |
146
|
2 |
|
private function formatFileContentFromMarkdownToHtml(array $files) |
147
|
|
|
{ |
148
|
2 |
|
foreach ($files as $file) { |
149
|
2 |
|
$this->markdownDecorator->decorateFile($file); |
150
|
|
|
} |
151
|
2 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param File[] $files |
155
|
|
|
*/ |
156
|
2 |
|
private function formatFileContentFromLatteToHtml(array $files) |
157
|
|
|
{ |
158
|
2 |
|
foreach ($files as $file) { |
159
|
2 |
|
$this->latteDecorator->decorateFile($file); |
160
|
|
|
} |
161
|
2 |
|
} |
162
|
|
|
} |
163
|
|
|
|