From efdd5c54be501b7fd5d3c0a9220d9e636c2b452d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sat, 27 Jun 2020 12:51:00 +0200 Subject: [PATCH] MAPG-180 ability to inline CSS and JS assets --- src/View/Linker.php | 78 ++++++++++++++++++++++++---------- views/templates/mapguesser.php | 22 +--------- 2 files changed, 57 insertions(+), 43 deletions(-) diff --git a/src/View/Linker.php b/src/View/Linker.php index d426bc1..817d8ac 100644 --- a/src/View/Linker.php +++ b/src/View/Linker.php @@ -2,6 +2,8 @@ class Linker { + const INLINE_ASSET_LIMIT = 2000; + private string $view; public function __construct(string $view) @@ -14,9 +16,7 @@ class Linker $input = ROOT . '/views/' . $this->view . '.php'; $temporaryFiles = []; - $css = []; - $js = []; - $sections = []; + $sections = ['externalCss' => '', 'inlineCss' => '', 'externalJs' => '', 'inlineJs' => '']; $extra = ['', '']; do { @@ -25,14 +25,14 @@ class Linker $extends = $fragment->getExtends(); - $css = array_merge($css, $fragment->getCss()); - $js = array_merge($js, $fragment->getJs()); + $this->generateAssets($fragment, $sections); + $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) { - $this->writeFinal($css, $js, $extra, $input, ROOT . '/cache/views/' . $this->view . '.php'); + $this->writeFinal($extra, $input, ROOT . '/cache/views/' . $this->view . '.php'); break; } @@ -78,7 +78,7 @@ class Linker fclose($outputFileHandle); } - private function writeFinal(array $css, array $js, array $extra, string $file, string $output): void + private function writeFinal(array $extra, string $file, string $output): void { $dirname = pathinfo($output, PATHINFO_DIRNAME); if (!is_dir($dirname)) { @@ -95,22 +95,6 @@ class Linker throw new \Exception('Cannot open file ' . $output . 'for writing.'); } - if (count($css) > 0) { - fwrite($outputFileHandle, '' . PHP_EOL); - } - - if (count($js) > 0) { - fwrite($outputFileHandle, '' . PHP_EOL); - } - fwrite($outputFileHandle, $extra[0]); while (($line = fgets($inputFileHandle)) !== false) { fwrite($outputFileHandle, $line); @@ -120,4 +104,52 @@ class Linker fclose($inputFileHandle); fclose($outputFileHandle); } + + private function generateAssets(ParsedFragment $fragment, array &$sections) + { + foreach ($fragment->getCss() as $cssFile) { + $asset = $this->parseAsset($cssFile, 'css'); + if (isset($asset['code'])) { + $sections['inlineCss'] .= '' . PHP_EOL; + } elseif (isset($asset['file'])) { + $sections['externalCss'] .= '' . PHP_EOL; + } + } + + foreach ($fragment->getJs() as $jsFile) { + $asset = $this->parseAsset($jsFile, 'js'); + if (isset($asset['code'])) { + $sections['inlineJs'] .= '' . PHP_EOL; + } elseif (isset($asset['file'])) { + $sections['externalJs'] .= '' . PHP_EOL; + } + } + } + + private function parseAsset(string $asset, string $type): array + { + $output = []; + + eval('$asset = ' . $asset . ';'); + + if ( + empty($_ENV['DEV']) && + preg_match('/^' . $type . '\/.*/', $asset) && + filesize(ROOT . '/public/static/' . $asset) < self::INLINE_ASSET_LIMIT + ) { + $output['code'] = file_get_contents(ROOT . '/public/static/' . $asset); + } else { + if (!preg_match('/^http(s)?/', $asset)) { + $output['file'] = $_ENV['STATIC_ROOT'] . '/' . $asset . '?rev=' . REVISION; + } else { + $output['file'] = $asset; + } + } + + return $output; + } } diff --git a/views/templates/mapguesser.php b/views/templates/mapguesser.php index 6dfe3b1..9f1793f 100644 --- a/views/templates/mapguesser.php +++ b/views/templates/mapguesser.php @@ -5,16 +5,7 @@ <?= $_ENV['APP_NAME'] ?> - - - - - - + @yields('externalCss') @yields('inlineCss') @@ -53,16 +44,7 @@ @yields('pageScript') - - - - - - + @yields('externalJs') @yields('inlineJs')