Total Complexity | 48 |
Total Lines | 303 |
Duplicated Lines | 0 % |
Coverage | 99.38% |
Changes | 30 | ||
Bugs | 3 | Features | 2 |
Complex classes like Generator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Generator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | 1 | final class Generator { |
|
27 | 1 | use \Nette\SmartObject; |
|
28 | |||
29 | /** @var string */ |
||
30 | protected $templateFile = __DIR__ . "/template.html"; |
||
31 | /** @var string[] */ |
||
32 | protected $ignoredFiles = []; |
||
33 | /** @var string[] */ |
||
34 | protected $ignoredFolders = [ |
||
35 | "vendor", ".git", "tests", |
||
36 | ]; |
||
37 | /** @var string */ |
||
38 | protected $source; |
||
39 | /** @var string */ |
||
40 | protected $output; |
||
41 | /** @var Finder|\SplFileInfo[] */ |
||
42 | protected $filesToProcess; |
||
43 | /** @var string[] */ |
||
44 | protected $assets = []; |
||
45 | /** @var callable[] */ |
||
46 | protected $metaNormalizers = []; |
||
47 | /** @var callable[] */ |
||
48 | public $onBeforeGenerate = []; |
||
49 | /** @var callable[] */ |
||
50 | public $onCreatePage = []; |
||
51 | /** @var callable[] */ |
||
52 | public $onAfterGenerate = []; |
||
53 | |||
54 | public function __construct(string $source, string $output) { |
||
55 | 1 | $this->setSource($source); |
|
56 | 1 | FileSystem::createDir($output); |
|
57 | 1 | $this->setOutput($output); |
|
58 | 1 | $this->onBeforeGenerate[] = [$this, "getFilesToProcess"]; |
|
59 | 1 | $this->onBeforeGenerate[] = [$this, "clearOutputFolder"]; |
|
60 | 1 | $this->onCreatePage[] = [$this, "processImages"]; |
|
61 | 1 | $this->onAfterGenerate[] = [$this, "copyAssets"]; |
|
62 | 1 | $this->addMetaNormalizer([$this, "normalizeTitle"]); |
|
63 | 1 | $this->addMetaNormalizer([$this, "normalizeStyles"]); |
|
64 | 1 | $this->addMetaNormalizer([$this, "normalizeScripts"]); |
|
65 | 1 | $this->addMetaNormalizer([$this, "updateLinks"]); |
|
66 | 1 | $this->addMetaNormalizer([$this, "addHtmlLanguage"]); |
|
67 | 1 | } |
|
68 | |||
69 | public function addMetaNormalizer(callable $callback): void { |
||
70 | 1 | $this->metaNormalizers[] = $callback; |
|
71 | 1 | } |
|
72 | |||
73 | public function getSource(): string { |
||
74 | 1 | return $this->source; |
|
75 | } |
||
76 | |||
77 | public function setSource(string $source): void { |
||
78 | 1 | if(is_dir($source)) { |
|
79 | 1 | $this->source = (string) realpath($source); |
|
80 | } |
||
81 | 1 | } |
|
82 | |||
83 | public function getOutput(): string { |
||
84 | 1 | return $this->output; |
|
85 | } |
||
86 | |||
87 | public function setOutput(string $output): void { |
||
88 | 1 | $this->output = (string) realpath($output); |
|
89 | 1 | } |
|
90 | |||
91 | /** |
||
92 | * @return string[] |
||
93 | */ |
||
94 | public function getIgnoredFiles(): array { |
||
95 | 1 | return $this->ignoredFiles; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param string[] $ignoredFiles |
||
100 | */ |
||
101 | public function setIgnoredFiles(array $ignoredFiles): void { |
||
102 | 1 | $this->ignoredFiles = []; |
|
103 | 1 | foreach($ignoredFiles as $ignoredFile) { |
|
104 | $this->ignoredFiles[] = (string) $ignoredFile; |
||
105 | } |
||
106 | 1 | } |
|
107 | |||
108 | /** |
||
109 | * @return string[] |
||
110 | */ |
||
111 | public function getIgnoredFolders(): array { |
||
112 | 1 | return $this->ignoredFolders; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param string[] $ignoredFolders |
||
117 | */ |
||
118 | public function setIgnoredFolders(array $ignoredFolders): void { |
||
119 | 1 | $this->ignoredFolders = []; |
|
120 | 1 | foreach($ignoredFolders as $ignoredFolder) { |
|
121 | 1 | $this->ignoredFolders[] = (string) $ignoredFolder; |
|
122 | } |
||
123 | 1 | } |
|
124 | |||
125 | protected function createMetaResolver(): OptionsResolver { |
||
126 | 1 | $resolver = new OptionsResolver(); |
|
127 | 1 | $resolver->setDefaults([ |
|
128 | 1 | "title" => "", |
|
129 | "htmlLang" => "", |
||
130 | "styles" => [], |
||
131 | "scripts" => [], |
||
132 | ]); |
||
133 | 1 | $isArrayOfStrings = function(array $value) { |
|
134 | 1 | return Validators::everyIs($value, "string"); |
|
135 | 1 | }; |
|
136 | 1 | $resolver->setAllowedTypes("title", "string"); |
|
137 | 1 | $resolver->setAllowedTypes("htmlLang", "string"); |
|
138 | 1 | $resolver->setAllowedTypes("styles", "array"); |
|
139 | 1 | $resolver->setAllowedValues("styles", $isArrayOfStrings); |
|
140 | 1 | $resolver->setAllowedTypes("scripts", "array"); |
|
141 | 1 | $resolver->setAllowedValues("scripts", $isArrayOfStrings); |
|
142 | 1 | return $resolver; |
|
143 | } |
||
144 | |||
145 | protected function getMetafileName(string $filename): string { |
||
146 | 1 | return str_replace(".md", ".neon", $filename); |
|
147 | } |
||
148 | |||
149 | protected function getMeta(string $filename, string &$html): array { |
||
150 | 1 | $resolver = $this->createMetaResolver(); |
|
151 | 1 | $metaFilename = $this->getMetafileName($filename); |
|
152 | 1 | $meta = []; |
|
153 | 1 | if(file_exists($metaFilename)) { |
|
154 | 1 | $meta = Neon::decode(file_get_contents($metaFilename)); |
|
155 | } |
||
156 | 1 | $result = $resolver->resolve($meta); |
|
157 | 1 | foreach($this->metaNormalizers as $normalizer) { |
|
158 | 1 | $normalizer($result, $html, $filename); |
|
159 | } |
||
160 | 1 | return $result; |
|
161 | } |
||
162 | |||
163 | protected function addAsset(string $asset): void { |
||
164 | 1 | $asset = realpath($asset); |
|
165 | 1 | if(is_string($asset) AND !in_array($asset, $this->assets, true)) { |
|
166 | 1 | $this->assets[] = $asset; |
|
167 | } |
||
168 | 1 | } |
|
169 | |||
170 | protected function normalizeTitle(array &$meta, string &$html, string $filename): void { |
||
1 ignored issue
–
show
|
|||
171 | 1 | if(strlen($meta["title"]) === 0) { |
|
172 | 1 | unset($meta["title"]); |
|
173 | 1 | $html = str_replace(" |
|
174 | 1 | <title>%%title%%</title>", "", $html); |
|
175 | } |
||
176 | 1 | } |
|
177 | |||
178 | protected function removeInvalidFiles(array &$input, string $basePath): void { |
||
179 | 1 | $input = array_filter($input, function($value) use($basePath) { |
|
180 | 1 | return file_exists("$basePath/$value"); |
|
181 | 1 | }); |
|
182 | 1 | } |
|
183 | |||
184 | protected function normalizeStyles(array &$meta, string &$html, string $filename): void { |
||
185 | 1 | $basePath = dirname($filename); |
|
186 | 1 | $this->removeInvalidFiles($meta["styles"], $basePath); |
|
187 | 1 | if(count($meta["styles"]) === 0) { |
|
188 | 1 | unset($meta["styles"]); |
|
189 | 1 | $html = str_replace(" |
|
190 | 1 | %%styles%%", "", $html); |
|
191 | 1 | return; |
|
192 | } |
||
193 | 1 | array_walk($meta["styles"], function(&$value) use($basePath) { |
|
194 | 1 | $this->addAsset("$basePath/$value"); |
|
195 | 1 | $value = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$value\">"; |
|
196 | 1 | }); |
|
197 | 1 | $meta["styles"] = implode("\n ", $meta["styles"]); |
|
198 | 1 | } |
|
199 | |||
200 | protected function normalizeScripts(array &$meta, string &$html, string $filename): void { |
||
201 | 1 | $basePath = dirname($filename); |
|
202 | 1 | $this->removeInvalidFiles($meta["scripts"], $basePath); |
|
203 | 1 | if(count($meta["scripts"]) === 0) { |
|
204 | 1 | unset($meta["scripts"]); |
|
205 | 1 | $html = str_replace(" |
|
206 | 1 | %%scripts%%", "", $html); |
|
207 | 1 | return; |
|
208 | } |
||
209 | 1 | array_walk($meta["scripts"], function(&$value) use($basePath) { |
|
210 | 1 | $this->addAsset("$basePath/$value"); |
|
211 | 1 | $value = "<script type=\"text/javascript\" src=\"$value\"></script>"; |
|
212 | 1 | }); |
|
213 | 1 | $meta["scripts"] = implode("\n ", $meta["scripts"]); |
|
214 | 1 | } |
|
215 | |||
216 | protected function updateLinks(array &$meta, string &$html, string $filename): void { |
||
1 ignored issue
–
show
|
|||
217 | 1 | $dom = new \DOMDocument(); |
|
218 | 1 | set_error_handler(function($errno) { |
|
219 | 1 | return $errno === E_WARNING; |
|
220 | 1 | }); |
|
221 | 1 | $dom->loadHTML($html); |
|
222 | 1 | restore_error_handler(); |
|
223 | 1 | $links = $dom->getElementsByTagName("a"); |
|
224 | /** @var \DOMElement $link */ |
||
225 | 1 | foreach($links as $link) { |
|
226 | 1 | $oldContent = $dom->saveHTML($link); |
|
227 | 1 | $needsUpdate = false; |
|
228 | 1 | $target = $link->getAttribute("href"); |
|
229 | 1 | $target = dirname($filename) . "/" . $target; |
|
230 | 1 | foreach($this->filesToProcess as $file) { |
|
231 | 1 | if($target === $file->getRealPath() AND Strings::endsWith($target, ".md")) { |
|
232 | 1 | $needsUpdate = true; |
|
233 | 1 | continue; |
|
234 | } |
||
235 | } |
||
236 | 1 | if(!$needsUpdate) { |
|
237 | 1 | continue; |
|
238 | } |
||
239 | 1 | $link->setAttribute("href", str_replace(".md", ".html", $link->getAttribute("href"))); |
|
240 | 1 | $newContent = $dom->saveHTML($link); |
|
241 | 1 | $html = str_replace($oldContent, $newContent, $html); |
|
242 | } |
||
243 | 1 | } |
|
244 | |||
245 | protected function addHtmlLanguage(array &$meta, string &$html, string $filename): void { |
||
2 ignored issues
–
show
|
|||
246 | 1 | if(strlen($meta["htmlLang"]) > 0) { |
|
247 | 1 | $html = str_replace("<html>", "<html lang=\"{$meta["htmlLang"]}\">", $html); |
|
248 | } |
||
249 | 1 | } |
|
250 | |||
251 | protected function createMarkdownParser(): \cebe\markdown\Markdown { |
||
253 | } |
||
254 | |||
255 | protected function createHtml(string $filename): string { |
||
256 | 1 | $parser = $this->createMarkdownParser(); |
|
257 | 1 | $source = $parser->parse(file_get_contents($filename)); |
|
258 | 1 | $html = file_get_contents($this->templateFile); |
|
259 | 1 | $html = str_replace("%%source%%", $source, $html); |
|
260 | 1 | return $html; |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * @internal |
||
265 | * @return Finder|\SplFileInfo[] |
||
266 | */ |
||
267 | public function getFilesToProcess(): Finder { |
||
268 | 1 | $this->filesToProcess = Finder::findFiles("*.md") |
|
269 | 1 | ->exclude($this->ignoredFiles) |
|
270 | 1 | ->from($this->source) |
|
271 | 1 | ->exclude($this->ignoredFolders); |
|
272 | 1 | return $this->filesToProcess; |
|
273 | } |
||
274 | |||
275 | /** |
||
276 | * @internal |
||
277 | */ |
||
278 | public function clearOutputFolder(): void { |
||
279 | 1 | FileSystem::delete($this->output); |
|
280 | 1 | } |
|
281 | |||
282 | /** |
||
283 | * @internal |
||
284 | */ |
||
285 | public function copyAssets(): void { |
||
286 | 1 | foreach($this->assets as $asset) { |
|
287 | 1 | $path = str_replace($this->source, "", $asset); |
|
288 | 1 | $target = "$this->output$path"; |
|
289 | 1 | FileSystem::copy($asset, $target); |
|
290 | 1 | echo "Copied $path"; |
|
291 | } |
||
292 | 1 | } |
|
293 | |||
294 | /** |
||
295 | * @internal |
||
296 | */ |
||
297 | public function processImages(string $html, self $generator, string $filename): void { |
||
306 | } |
||
307 | } |
||
308 | 1 | } |
|
309 | |||
310 | /** |
||
311 | * Generate the site |
||
312 | */ |
||
313 | public function generate(): void { |
||
314 | 1 | $this->onBeforeGenerate(); |
|
315 | 1 | foreach($this->filesToProcess as $file) { |
|
316 | 1 | $path = str_replace($this->source, "", dirname($file->getRealPath())); |
|
317 | 1 | $html = $this->createHtml($file->getRealPath()); |
|
318 | 1 | $meta = $this->getMeta($file->getRealPath(), $html); |
|
319 | 1 | foreach($meta as $key => $value) { |
|
320 | 1 | $html = str_replace("%%$key%%", $value, $html); |
|
321 | } |
||
322 | 1 | $basename = $file->getBasename(".md") . ".html"; |
|
323 | 1 | $filename = "$this->output$path/$basename"; |
|
324 | 1 | FileSystem::write($filename, $html); |
|
325 | 1 | echo "Created $path/$basename\n"; |
|
331 | ?> |