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\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
|
|
|
public function __construct( |
50
|
|
|
SourceFileStorage $sourceFileStorage, |
51
|
|
|
Configuration $configuration, |
52
|
|
|
FileSystemWriter $fileSystemWriter, |
53
|
|
|
RenderableFilesProcessor $renderableFilesProcessor, |
54
|
|
|
DynamicStringLoader $dynamicStringLoader |
55
|
|
|
) { |
56
|
|
|
$this->sourceFileStorage = $sourceFileStorage; |
57
|
|
|
$this->configuration = $configuration; |
58
|
|
|
$this->fileSystemWriter = $fileSystemWriter; |
59
|
|
|
$this->renderableFilesProcessor = $renderableFilesProcessor; |
60
|
|
|
$this->dynamicStringLoader = $dynamicStringLoader; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function runCommand(RunCommand $runCommand) |
64
|
|
|
{ |
65
|
|
|
$this->loadSourcesFromSourceDirectory($runCommand->getSourceDirectory()); |
66
|
|
|
$this->clearOutputDirectory($runCommand->getOutputDirectory()); |
67
|
|
|
|
68
|
|
|
// 1. copy static files |
69
|
|
|
$this->fileSystemWriter->copyStaticFiles($this->sourceFileStorage->getStaticFiles()); |
70
|
|
|
|
71
|
|
|
// 2. collect configuration |
72
|
|
|
$this->configuration->loadOptionsFromFiles($this->sourceFileStorage->getConfigurationFiles()); |
73
|
|
|
|
74
|
|
|
// 3. collect layouts |
75
|
|
|
$this->loadLayoutsToLatteLoader($this->sourceFileStorage->getLayoutFiles()); |
76
|
|
|
|
77
|
|
|
// 4. completely process post |
78
|
|
|
$this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getPostFiles()); |
79
|
|
|
|
80
|
|
|
// 5. render files |
81
|
|
|
$this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getRenderableFiles()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function clearOutputDirectory(string $outputDirectory) |
85
|
|
|
{ |
86
|
|
|
FileSystem::delete($outputDirectory); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function loadSourcesFromSourceDirectory(string $sourceDirectory) |
90
|
|
|
{ |
91
|
|
|
$finder = $this->findFilesInSourceDirectory($sourceDirectory); |
92
|
|
|
$this->sourceFileStorage->loadSourcesFromFinder($finder); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function findFilesInSourceDirectory(string $sourceDirectory) : Finder |
96
|
|
|
{ |
97
|
|
|
return Finder::findFiles('*') |
98
|
|
|
->from($sourceDirectory); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param SplFileInfo[] $layoutFiles |
103
|
|
|
*/ |
104
|
|
|
private function loadLayoutsToLatteLoader(array $layoutFiles) |
105
|
|
|
{ |
106
|
|
|
foreach ($layoutFiles as $layoutFile) { |
107
|
|
|
$name = $layoutFile->getBasename('.'.$layoutFile->getExtension()); |
108
|
|
|
$content = file_get_contents($layoutFile->getRealPath()); |
109
|
|
|
$this->dynamicStringLoader->addTemplate($name, $content); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|