1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Event\BuildProcessComplete; |
11
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
12
|
|
|
use allejo\stakx\Filesystem\Folder; |
13
|
|
|
use allejo\stakx\Manager\AssetManager; |
14
|
|
|
use allejo\stakx\Manager\ThemeManager; |
15
|
|
|
use allejo\stakx\Templating\TemplateBridgeInterface; |
16
|
|
|
use Highlight\Highlighter; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
|
19
|
|
|
class Website |
20
|
|
|
{ |
21
|
|
|
private $configuration; |
22
|
|
|
private $logger; |
23
|
|
|
private $templateBridge; |
24
|
|
|
private $compiler; |
25
|
|
|
private $assetManager; |
26
|
|
|
private $eventDispatcher; |
27
|
|
|
|
28
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
29
|
|
|
Compiler $compiler, |
30
|
|
|
Configuration $configuration, |
31
|
|
|
AssetManager $assetManager, |
32
|
|
|
TemplateBridgeInterface $templateBridge, |
33
|
|
|
EventDispatcherInterface $eventDispatcher, |
34
|
|
|
Logger $logger |
35
|
|
|
) { |
36
|
|
|
$this->configuration = $configuration; |
37
|
|
|
$this->logger = $logger; |
38
|
|
|
$this->templateBridge = $templateBridge; |
39
|
|
|
$this->compiler = $compiler; |
40
|
|
|
$this->assetManager = $assetManager; |
41
|
|
|
$this->eventDispatcher = $eventDispatcher; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Compile the website. |
46
|
|
|
* |
47
|
|
|
* @return true if the website built successfully |
|
|
|
|
48
|
|
|
*/ |
49
|
|
|
public function build() |
50
|
|
|
{ |
51
|
|
|
if (empty($this->getConfiguration()->getPageViewFolders())) |
52
|
|
|
{ |
53
|
|
|
$this->logger->error('No PageViews were configured for this site. Check the `pageviews` key in your _config.yml.'); |
54
|
|
|
|
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Configure the environment |
59
|
|
|
$this->createFolderStructure(); |
60
|
|
|
$this->configureHighlighter(); |
61
|
|
|
|
62
|
|
|
// Our output directory |
63
|
|
|
$outputDirectory = new Folder($this->getConfiguration()->getTargetFolder()); |
64
|
|
|
$outputDirectory->setTargetDirectory($this->getConfiguration()->getBaseUrl()); |
65
|
|
|
|
66
|
|
|
// Compile everything |
67
|
|
|
$theme = $this->getConfiguration()->getTheme(); |
68
|
|
|
|
69
|
|
|
$this->compiler->setTargetFolder($outputDirectory); |
70
|
|
|
$this->compiler->setThemeName($theme); |
71
|
|
|
$this->compiler->compileAll(); |
72
|
|
|
|
73
|
|
|
if (Service::hasRunTimeFlag(RuntimeStatus::IN_PROFILE_MODE)) |
74
|
|
|
{ |
75
|
|
|
if (!$this->templateBridge->hasProfiler()) |
76
|
|
|
{ |
77
|
|
|
$this->logger->writeln('This template engine currently does not support a profiler.'); |
78
|
|
|
} |
79
|
|
|
else |
80
|
|
|
{ |
81
|
|
|
$profilerText = $this->templateBridge->getProfilerOutput($this->compiler); |
82
|
|
|
$this->logger->writeln($profilerText); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// At this point, we are looking at static files to copy over meaning we need to ignore all of the files that |
87
|
|
|
// make up the source of a stakx website |
88
|
|
|
$assetsToIgnore = array_merge( |
89
|
|
|
Configuration::$stakxSourceFiles, |
90
|
|
|
$this->getConfiguration()->getExcludes() |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
// |
94
|
|
|
// Theme Management |
95
|
|
|
// |
96
|
|
|
if ($theme !== null) |
97
|
|
|
{ |
98
|
|
|
$this->logger->notice("Looking for '${theme}' theme..."); |
99
|
|
|
|
100
|
|
|
$tm = new ThemeManager($theme, $this->eventDispatcher, $this->logger); |
|
|
|
|
101
|
|
|
$tm->configureFinder($this->getConfiguration()->getIncludes(), $assetsToIgnore); |
102
|
|
|
$tm->setFolder($outputDirectory); |
103
|
|
|
$tm->copyFiles(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// |
107
|
|
|
// Static file management |
108
|
|
|
// |
109
|
|
|
$this->assetManager->configureFinder($this->getConfiguration()->getIncludes(), $assetsToIgnore); |
110
|
|
|
$this->assetManager->setFolder($outputDirectory); |
111
|
|
|
$this->assetManager->copyFiles(); |
112
|
|
|
|
113
|
|
|
$this->eventDispatcher->dispatch(BuildProcessComplete::NAME, new BuildProcessComplete()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return Configuration |
118
|
|
|
*/ |
119
|
|
|
public function getConfiguration() |
120
|
|
|
{ |
121
|
|
|
return $this->configuration; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Prepare the Stakx environment by creating necessary cache folders. |
126
|
|
|
*/ |
127
|
|
|
private function createFolderStructure() |
128
|
|
|
{ |
129
|
|
|
$targetDir = fs::absolutePath($this->getConfiguration()->getTargetFolder()); |
|
|
|
|
130
|
|
|
|
131
|
|
|
if (!Service::hasRunTimeFlag(RuntimeStatus::BOOT_WITHOUT_CLEAN)) |
132
|
|
|
{ |
133
|
|
|
fs::remove($targetDir); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (!Service::hasRunTimeFlag(RuntimeStatus::USING_CACHE)) |
137
|
|
|
{ |
138
|
|
|
fs::remove(fs::absolutePath(Configuration::CACHE_FOLDER, 'twig')); |
|
|
|
|
139
|
|
|
fs::mkdir(fs::absolutePath(fs::appendPath(Configuration::CACHE_FOLDER, 'twig'))); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
fs::mkdir($targetDir); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Configure the Highlighter object for highlighting code blocks. |
147
|
|
|
*/ |
148
|
|
|
private function configureHighlighter() |
149
|
|
|
{ |
150
|
|
|
$enabled = $this->getConfiguration()->isHighlighterEnabled(); |
151
|
|
|
|
152
|
|
|
if (!$enabled) |
153
|
|
|
{ |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
Service::setRuntimeFlag(RuntimeStatus::USING_HIGHLIGHTER); |
158
|
|
|
|
159
|
|
|
foreach ($this->getConfiguration()->getHighlighterCustomLanguages() as $lang => $path) |
160
|
|
|
{ |
161
|
|
|
$fullPath = fs::absolutePath($path); |
162
|
|
|
|
163
|
|
|
if (!fs::exists($fullPath)) |
164
|
|
|
{ |
165
|
|
|
$this->logger->warning('The following language definition could not be found: {lang}', [ |
166
|
|
|
'lang' => $path, |
167
|
|
|
]); |
168
|
|
|
continue; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
Highlighter::registerLanguage($lang, $fullPath); |
172
|
|
|
$this->logger->debug('Loading custom language {lang} from {path}...', [ |
173
|
|
|
'lang' => $lang, |
174
|
|
|
'path' => $path, |
175
|
|
|
]); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.