|
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\Application; |
|
11
|
|
|
|
|
12
|
|
|
use Nette\Utils\Finder; |
|
13
|
|
|
use SplFileInfo; |
|
14
|
|
|
use Symplify\PHP7_Sculpin\Application\Command\RunCommand; |
|
15
|
|
|
use Symplify\PHP7_Sculpin\Configuration\Configuration; |
|
16
|
|
|
use Symplify\PHP7_Sculpin\HttpServer\HttpServer; |
|
17
|
|
|
use Symplify\PHP7_Sculpin\Output\FileSystemWriter; |
|
18
|
|
|
use Symplify\PHP7_Sculpin\Renderable\Latte\DynamicStringLoader; |
|
19
|
|
|
use Symplify\PHP7_Sculpin\Renderable\RenderableFilesProcessor; |
|
20
|
|
|
use Symplify\PHP7_Sculpin\Source\SourceFileStorage; |
|
21
|
|
|
use Symplify\PHP7_Sculpin\Utils\FilesystemChecker; |
|
22
|
|
|
|
|
23
|
|
|
final class SculpinApplication |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var SourceFileStorage |
|
27
|
|
|
*/ |
|
28
|
|
|
private $sourceFileStorage; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Configuration |
|
32
|
|
|
*/ |
|
33
|
|
|
private $configuration; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var FileSystemWriter |
|
37
|
|
|
*/ |
|
38
|
|
|
private $fileSystemWriter; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var RenderableFilesProcessor |
|
42
|
|
|
*/ |
|
43
|
|
|
private $renderableFilesProcessor; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var DynamicStringLoader |
|
47
|
|
|
*/ |
|
48
|
|
|
private $dynamicStringLoader; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var HttpServer |
|
52
|
|
|
*/ |
|
53
|
|
|
private $httpServer; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
private $sinceTime; |
|
59
|
|
|
|
|
60
|
4 |
|
public function __construct( |
|
61
|
|
|
SourceFileStorage $sourceFileStorage, |
|
62
|
|
|
Configuration $configuration, |
|
63
|
|
|
FileSystemWriter $fileSystemWriter, |
|
64
|
|
|
RenderableFilesProcessor $renderableFilesProcessor, |
|
65
|
|
|
DynamicStringLoader $dynamicStringLoader, |
|
66
|
|
|
HttpServer $httpServer |
|
67
|
|
|
) { |
|
68
|
4 |
|
$this->sourceFileStorage = $sourceFileStorage; |
|
69
|
4 |
|
$this->configuration = $configuration; |
|
70
|
4 |
|
$this->fileSystemWriter = $fileSystemWriter; |
|
71
|
4 |
|
$this->renderableFilesProcessor = $renderableFilesProcessor; |
|
72
|
4 |
|
$this->dynamicStringLoader = $dynamicStringLoader; |
|
73
|
4 |
|
$this->httpServer = $httpServer; |
|
74
|
4 |
|
$this->sinceTime = '1970-01-01T00:00:00Z'; |
|
75
|
4 |
|
} |
|
76
|
|
|
|
|
77
|
4 |
|
public function runCommand(RunCommand $runCommand) |
|
78
|
|
|
{ |
|
79
|
4 |
|
$this->processCommand($runCommand); |
|
80
|
|
|
|
|
81
|
2 |
|
if ($runCommand->isRunServer()) { |
|
82
|
|
|
$this->httpServer->init(); |
|
83
|
|
|
|
|
84
|
|
|
$this->httpServer->addPeriodicTimer(1, function () use ($runCommand) { |
|
85
|
|
|
clearstatcache(); |
|
86
|
|
|
$this->processCommand($runCommand); |
|
87
|
|
|
}); |
|
88
|
|
|
|
|
89
|
|
|
$this->httpServer->run(); |
|
90
|
|
|
} |
|
91
|
2 |
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
private function processCommand(RunCommand $runCommand) |
|
94
|
|
|
{ |
|
95
|
4 |
|
$this->loadConfigurationWithDirectories($runCommand); |
|
96
|
|
|
|
|
97
|
2 |
|
FilesystemChecker::ensureDirectoryExists($runCommand->getSourceDirectory()); |
|
98
|
|
|
|
|
99
|
|
|
$this->loadSourcesFromSourceDirectory($runCommand->getSourceDirectory()); |
|
100
|
2 |
|
|
|
101
|
|
|
// 1. copy static files |
|
102
|
|
|
$this->fileSystemWriter->copyStaticFiles($this->sourceFileStorage->getStaticFiles()); |
|
103
|
2 |
|
|
|
104
|
|
|
// 2. collect configuration |
|
105
|
|
|
$this->configuration->loadFromFiles($this->sourceFileStorage->getConfigurationFiles()); |
|
106
|
2 |
|
|
|
107
|
|
|
// 3. collect layouts |
|
108
|
|
|
$this->loadLayoutsToLatteLoader($this->sourceFileStorage->getLayoutFiles()); |
|
109
|
2 |
|
|
|
110
|
|
|
// 4. completely process post |
|
111
|
|
|
$this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getPostFiles()); |
|
112
|
2 |
|
|
|
113
|
2 |
|
// 5. render files |
|
114
|
|
|
$this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getRenderableFiles()); |
|
115
|
2 |
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
private function loadConfigurationWithDirectories(RunCommand $runCommand) |
|
118
|
2 |
|
{ |
|
119
|
2 |
|
$this->configuration->setSourceDirectory($runCommand->getSourceDirectory()); |
|
120
|
|
|
$this->configuration->setOutputDirectory($runCommand->getOutputDirectory()); |
|
121
|
2 |
|
} |
|
122
|
|
|
|
|
123
|
2 |
|
private function loadSourcesFromSourceDirectory(string $sourceDirectory) |
|
124
|
|
|
{ |
|
125
|
2 |
|
$finder = $this->findFilesInSourceDirectory($sourceDirectory); |
|
126
|
|
|
$this->sourceFileStorage->loadSourcesFromFinder($finder); |
|
127
|
2 |
|
} |
|
128
|
2 |
|
|
|
129
|
2 |
|
private function findFilesInSourceDirectory(string $sourceDirectory) : Finder |
|
130
|
|
|
{ |
|
131
|
|
|
$sinceTimeLast = $this->sinceTime; |
|
132
|
|
|
|
|
133
|
|
|
$this->sinceTime = date('c'); |
|
134
|
|
|
|
|
135
|
2 |
|
return Finder::findFiles('*') |
|
136
|
|
|
->from($sourceDirectory) |
|
137
|
2 |
|
->date('>=', $sinceTimeLast); |
|
138
|
1 |
|
} |
|
139
|
1 |
|
|
|
140
|
1 |
|
/** |
|
141
|
|
|
* @param SplFileInfo[] $layoutFiles |
|
142
|
2 |
|
*/ |
|
143
|
|
|
private function loadLayoutsToLatteLoader(array $layoutFiles) |
|
144
|
|
|
{ |
|
145
|
|
|
foreach ($layoutFiles as $layoutFile) { |
|
146
|
|
|
$name = $layoutFile->getBasename('.' . $layoutFile->getExtension()); |
|
147
|
|
|
$content = file_get_contents($layoutFile->getRealPath()); |
|
148
|
|
|
$this->dynamicStringLoader->addTemplate($name, $content); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|