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\HttpServer\HttpServer; |
15
|
|
|
use Symplify\PHP7_Sculpin\Output\FileSystemWriter; |
16
|
|
|
use Symplify\PHP7_Sculpin\Application\Command\RunCommand; |
17
|
|
|
use Symplify\PHP7_Sculpin\Configuration\Configuration; |
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
|
3 |
|
public function __construct( |
61
|
|
|
SourceFileStorage $sourceFileStorage, |
62
|
|
|
Configuration $configuration, |
63
|
|
|
FileSystemWriter $fileSystemWriter, |
64
|
|
|
RenderableFilesProcessor $renderableFilesProcessor, |
65
|
|
|
DynamicStringLoader $dynamicStringLoader, |
66
|
|
|
HttpServer $httpServer |
67
|
|
|
) { |
68
|
3 |
|
$this->sourceFileStorage = $sourceFileStorage; |
69
|
3 |
|
$this->configuration = $configuration; |
70
|
3 |
|
$this->fileSystemWriter = $fileSystemWriter; |
71
|
3 |
|
$this->renderableFilesProcessor = $renderableFilesProcessor; |
72
|
3 |
|
$this->dynamicStringLoader = $dynamicStringLoader; |
73
|
3 |
|
$this->httpServer = $httpServer; |
74
|
3 |
|
$this->sinceTime = '1970-01-01T00:00:00Z'; |
75
|
3 |
|
} |
76
|
|
|
|
77
|
3 |
|
public function runCommand(RunCommand $runCommand) |
78
|
|
|
{ |
79
|
3 |
|
$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
|
3 |
|
private function processCommand(RunCommand $runCommand) |
94
|
|
|
{ |
95
|
3 |
|
FilesystemChecker::ensureDirectoryExists($runCommand->getSourceDirectory()); |
96
|
|
|
|
97
|
2 |
|
$this->loadSourcesFromSourceDirectory($runCommand->getSourceDirectory()); |
98
|
|
|
|
99
|
|
|
// 1. copy static files |
100
|
2 |
|
$this->fileSystemWriter->copyStaticFiles($this->sourceFileStorage->getStaticFiles()); |
101
|
|
|
|
102
|
|
|
// 2. collect configuration |
103
|
2 |
|
$this->configuration->loadOptionsFromFiles($this->sourceFileStorage->getConfigurationFiles()); |
104
|
|
|
|
105
|
|
|
// 3. collect layouts |
106
|
2 |
|
$this->loadLayoutsToLatteLoader($this->sourceFileStorage->getLayoutFiles()); |
107
|
|
|
|
108
|
|
|
// 4. completely process post |
109
|
2 |
|
$this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getPostFiles()); |
110
|
|
|
|
111
|
|
|
// 5. render files |
112
|
2 |
|
$this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getRenderableFiles()); |
113
|
2 |
|
} |
114
|
|
|
|
115
|
2 |
|
private function loadSourcesFromSourceDirectory(string $sourceDirectory) |
116
|
|
|
{ |
117
|
2 |
|
$finder = $this->findFilesInSourceDirectory($sourceDirectory); |
118
|
2 |
|
$this->sourceFileStorage->loadSourcesFromFinder($finder); |
119
|
2 |
|
} |
120
|
|
|
|
121
|
2 |
|
private function findFilesInSourceDirectory(string $sourceDirectory) : Finder |
122
|
|
|
{ |
123
|
2 |
|
$sinceTimeLast = $this->sinceTime; |
124
|
|
|
|
125
|
2 |
|
$this->sinceTime = date('c'); |
126
|
|
|
|
127
|
2 |
|
return Finder::findFiles('*') |
128
|
2 |
|
->from($sourceDirectory) |
129
|
2 |
|
->date('>=', $sinceTimeLast); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param SplFileInfo[] $layoutFiles |
134
|
|
|
*/ |
135
|
2 |
|
private function loadLayoutsToLatteLoader(array $layoutFiles) |
136
|
|
|
{ |
137
|
2 |
|
foreach ($layoutFiles as $layoutFile) { |
138
|
|
|
$name = $layoutFile->getBasename('.'.$layoutFile->getExtension()); |
139
|
|
|
$content = file_get_contents($layoutFile->getRealPath()); |
140
|
|
|
$this->dynamicStringLoader->addTemplate($name, $content); |
141
|
|
|
} |
142
|
2 |
|
} |
143
|
|
|
} |
144
|
|
|
|