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\FileFactory; |
17
|
|
|
use Symplify\PHP7_Sculpin\Renderable\File\PostFile; |
18
|
|
|
use Symplify\PHP7_Sculpin\Renderable\Latte\LatteDecorator; |
19
|
|
|
use Symplify\PHP7_Sculpin\Renderable\Markdown\MarkdownDecorator; |
20
|
|
|
use Symplify\PHP7_Sculpin\Renderable\Routing\RouteDecorator; |
21
|
|
|
use Symplify\PHP7_Sculpin\Renderable\Sorting\PostSorter; |
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 PostSorter |
37
|
|
|
*/ |
38
|
|
|
private $postSorter; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ConfigurationDecorator |
42
|
|
|
*/ |
43
|
|
|
private $configurationDecorator; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var MarkdownDecorator |
47
|
|
|
*/ |
48
|
|
|
private $markdownDecorator; |
49
|
|
|
/** |
50
|
|
|
* @var LatteDecorator |
51
|
|
|
*/ |
52
|
|
|
private $latteDecorator; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var FileSystemWriter |
56
|
|
|
*/ |
57
|
|
|
private $fileSystemWriter; |
58
|
|
|
/** |
59
|
|
|
* @var Configuration |
60
|
|
|
*/ |
61
|
|
|
private $configuration; |
62
|
|
|
|
63
|
|
|
public function __construct( |
64
|
|
|
FileFactory $fileFactory, |
65
|
|
|
RouteDecorator $routeDecorator, |
66
|
|
|
PostSorter $postSorter, |
67
|
|
|
ConfigurationDecorator $configurationDecorator, |
68
|
|
|
MarkdownDecorator $markdownDecorator, |
69
|
|
|
LatteDecorator $latteDecorator, |
70
|
|
|
FileSystemWriter $fileSystemWriter, |
71
|
|
|
Configuration $configuration |
72
|
|
|
) { |
73
|
|
|
$this->fileFactory = $fileFactory; |
74
|
|
|
$this->routeDecorator = $routeDecorator; |
75
|
|
|
$this->postSorter = $postSorter; |
76
|
|
|
$this->configurationDecorator = $configurationDecorator; |
77
|
|
|
$this->markdownDecorator = $markdownDecorator; |
78
|
|
|
$this->latteDecorator = $latteDecorator; |
79
|
|
|
$this->fileSystemWriter = $fileSystemWriter; |
80
|
|
|
$this->configuration = $configuration; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param SplFileInfo[] $files |
85
|
|
|
*/ |
86
|
|
|
public function processFiles(array $files) |
87
|
|
|
{ |
88
|
|
|
if (!count($files)) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// 1. create file objects |
93
|
|
|
foreach ($files as $id => $file) { |
94
|
|
|
$files[$id] = $this->fileFactory->create($file); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// 2. set routes |
98
|
|
|
foreach ($files as $file) { |
99
|
|
|
$this->routeDecorator->decorateFile($file); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// 3. order (should be performed before render) |
103
|
|
|
if ($files[0] instanceof PostFile) { |
104
|
|
|
$files = $this->postSorter->sort($files); |
|
|
|
|
105
|
|
|
|
106
|
|
|
// set posts to configuration |
107
|
|
|
$this->configuration->addOption('posts', $files); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// 4. detect and set options |
111
|
|
|
foreach ($files as $file) { |
112
|
|
|
$this->configurationDecorator->decorateFile($file); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// 5. format Markdown to HTML |
116
|
|
|
foreach ($files as $file) { |
117
|
|
|
$this->markdownDecorator->decorateFile($file); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// 6. format Latte to HTML |
121
|
|
|
foreach ($files as $file) { |
122
|
|
|
$this->latteDecorator->decorateFile($file); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// 7. save files |
126
|
|
|
$this->fileSystemWriter->copyRenderableFiles($files); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.