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