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\FileSystem; |
13
|
|
|
use Nette\Utils\Finder; |
14
|
|
|
use SplFileInfo; |
15
|
|
|
use Symplify\PHP7_Sculpin\HttpServer\HttpServer; |
16
|
|
|
use Symplify\PHP7_Sculpin\Output\FileSystemWriter; |
17
|
|
|
use Symplify\PHP7_Sculpin\Application\Command\RunCommand; |
18
|
|
|
use Symplify\PHP7_Sculpin\Configuration\Configuration; |
19
|
|
|
use Symplify\PHP7_Sculpin\Renderable\Latte\DynamicStringLoader; |
20
|
|
|
use Symplify\PHP7_Sculpin\Renderable\RenderableFilesProcessor; |
21
|
|
|
use Symplify\PHP7_Sculpin\Source\SourceFileStorage; |
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
|
|
|
public function __construct( |
56
|
|
|
SourceFileStorage $sourceFileStorage, |
57
|
|
|
Configuration $configuration, |
58
|
|
|
FileSystemWriter $fileSystemWriter, |
59
|
|
|
RenderableFilesProcessor $renderableFilesProcessor, |
60
|
|
|
DynamicStringLoader $dynamicStringLoader, |
61
|
|
|
HttpServer $httpServer |
62
|
|
|
) { |
63
|
|
|
$this->sourceFileStorage = $sourceFileStorage; |
64
|
|
|
$this->configuration = $configuration; |
65
|
|
|
$this->fileSystemWriter = $fileSystemWriter; |
66
|
|
|
$this->renderableFilesProcessor = $renderableFilesProcessor; |
67
|
|
|
$this->dynamicStringLoader = $dynamicStringLoader; |
68
|
|
|
$this->httpServer = $httpServer; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function runCommand(RunCommand $runCommand) |
72
|
|
|
{ |
73
|
|
|
$this->processCommand($runCommand); |
74
|
|
|
|
75
|
|
|
if ($runCommand->isRunServer()) { |
76
|
|
|
$this->httpServer->init(); |
77
|
|
|
|
78
|
|
|
$this->httpServer->addPeriodicTimer(1, function () use ($runCommand) { |
79
|
|
|
clearstatcache(); |
80
|
|
|
$this->processCommand($runCommand); |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
$this->httpServer->run(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function processCommand(RunCommand $runCommand) |
88
|
|
|
{ |
89
|
|
|
$this->loadSourcesFromSourceDirectory($runCommand->getSourceDirectory()); |
90
|
|
|
|
91
|
|
|
// 1. copy static files |
92
|
|
|
$this->fileSystemWriter->copyStaticFiles($this->sourceFileStorage->getStaticFiles()); |
93
|
|
|
|
94
|
|
|
// 2. collect configuration |
95
|
|
|
$this->configuration->loadOptionsFromFiles($this->sourceFileStorage->getConfigurationFiles()); |
96
|
|
|
|
97
|
|
|
// 3. collect layouts |
98
|
|
|
$this->loadLayoutsToLatteLoader($this->sourceFileStorage->getLayoutFiles()); |
99
|
|
|
|
100
|
|
|
// 4. completely process post |
101
|
|
|
$this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getPostFiles()); |
102
|
|
|
|
103
|
|
|
// 5. render files |
104
|
|
|
$this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getRenderableFiles()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function loadSourcesFromSourceDirectory(string $sourceDirectory) |
108
|
|
|
{ |
109
|
|
|
$finder = $this->findFilesInSourceDirectory($sourceDirectory); |
110
|
|
|
$this->sourceFileStorage->loadSourcesFromFinder($finder); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function findFilesInSourceDirectory(string $sourceDirectory) : Finder |
114
|
|
|
{ |
115
|
|
|
return Finder::findFiles('*') |
116
|
|
|
->from($sourceDirectory); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param SplFileInfo[] $layoutFiles |
121
|
|
|
*/ |
122
|
|
|
private function loadLayoutsToLatteLoader(array $layoutFiles) |
123
|
|
|
{ |
124
|
|
|
foreach ($layoutFiles as $layoutFile) { |
125
|
|
|
$name = $layoutFile->getBasename('.'.$layoutFile->getExtension()); |
126
|
|
|
$content = file_get_contents($layoutFile->getRealPath()); |
127
|
|
|
$this->dynamicStringLoader->addTemplate($name, $content); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|