mapguesser/src/View/Linker.php

124 lines
3.8 KiB
PHP
Raw Normal View History

<?php namespace MapGuesser\View;
class Linker
{
private string $view;
public function __construct(string $view)
{
$this->view = $view;
}
public function generate(): void
{
$input = ROOT . '/views/' . $this->view . '.php';
$temporaryFiles = [];
2020-06-27 02:25:54 +02:00
$css = [];
$js = [];
$sections = [];
$extra = ['', ''];
do {
$parser = new Parser($input);
$fragment = $parser->parse();
$extends = $fragment->getExtends();
2020-06-27 02:25:54 +02:00
$css = array_merge($css, $fragment->getCss());
$js = array_merge($js, $fragment->getJs());
$sections = array_merge($sections, $fragment->getSections()); //TODO: detect if section defined multiple times
$extra[0] = $fragment->getExtra()[0] . $extra[0];
$extra[1] = $extra[1] . $fragment->getExtra()[1];
if ($extends === null) {
2020-06-27 02:25:54 +02:00
$this->writeFinal($css, $js, $extra, $input, ROOT . '/cache/views/' . $this->view . '.php');
break;
}
$tmpFile = tempnam(sys_get_temp_dir(), 'mapg-view-');
$temporaryFiles[] = $tmpFile;
$this->extendTemplate($sections, ROOT . '/views/' . $extends . '.php', $tmpFile);
$input = $tmpFile;
} while (true);
foreach ($temporaryFiles as $tmpFile) {
unlink($tmpFile);
}
}
private function extendTemplate(array $sections, string $file, string $output): void
{
$inputFileHandle = fopen($file, 'r');
if (!$inputFileHandle) {
throw new \Exception('Cannot open file ' . $file);
}
$outputFileHandle = fopen($output, 'w');
if (!$outputFileHandle) {
throw new \Exception('Cannot open file ' . $output . 'for writing.');
}
$lineNumber = 0;
while (($line = fgets($inputFileHandle)) !== false) {
++$lineNumber;
if (preg_match('/^\s*@yields\(\'([\w\/]+)\'\)\s*$/', $line, $matches)) {
if (isset($sections[$matches[1]])) {
fwrite($outputFileHandle, $sections[$matches[1]]);
}
} else {
fwrite($outputFileHandle, $line);
}
}
fclose($inputFileHandle);
fclose($outputFileHandle);
}
2020-06-27 02:25:54 +02:00
private function writeFinal(array $css, array $js, array $extra, string $file, string $output): void
{
$dirname = pathinfo($output, PATHINFO_DIRNAME);
if (!is_dir($dirname)) {
mkdir($dirname, 0755, true);
}
$inputFileHandle = fopen($file, 'r');
if (!$inputFileHandle) {
throw new \Exception('Cannot open file ' . $file);
}
$outputFileHandle = fopen($output, 'w');
if (!$outputFileHandle) {
throw new \Exception('Cannot open file ' . $output . 'for writing.');
}
2020-06-27 02:25:54 +02:00
if (count($css) > 0) {
fwrite($outputFileHandle, '<?php' . PHP_EOL . '$cssFiles = [];' . PHP_EOL);
foreach ($css as $cssFile) {
fwrite($outputFileHandle, '$cssFiles[] = ' . $cssFile . ';' . PHP_EOL);
}
fwrite($outputFileHandle, '?>' . PHP_EOL);
}
if (count($js) > 0) {
fwrite($outputFileHandle, '<?php' . PHP_EOL . '$jsFiles = [];' . PHP_EOL);
foreach ($js as $jsFile) {
fwrite($outputFileHandle, '$jsFiles[] = ' . $jsFile . ';' . PHP_EOL);
}
fwrite($outputFileHandle, '?>' . PHP_EOL);
}
fwrite($outputFileHandle, $extra[0]);
while (($line = fgets($inputFileHandle)) !== false) {
fwrite($outputFileHandle, $line);
}
fwrite($outputFileHandle, $extra[1]);
fclose($inputFileHandle);
fclose($outputFileHandle);
}
}