From 18bf9aaadecbc7f2102ea2d4479290d41adba633 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sat, 27 Jun 2020 02:25:54 +0200 Subject: [PATCH 1/4] MAPG-180 declare CSS and JS files --- src/View/Linker.php | 24 +++++++++++++++++++++-- src/View/ParsedFragment.php | 18 ++++++++++++++++- src/View/Parser.php | 35 ++++++++++++++++++++++++++++++++-- views/admin/map_editor.php | 25 +++++++++--------------- views/game.php | 15 ++++----------- views/login/google_signup.php | 8 +------- views/login/signup.php | 8 +------- views/maps.php | 17 ++++------------- views/templates/mapguesser.php | 2 ++ 9 files changed, 93 insertions(+), 59 deletions(-) diff --git a/src/View/Linker.php b/src/View/Linker.php index 360d69c..d426bc1 100644 --- a/src/View/Linker.php +++ b/src/View/Linker.php @@ -14,6 +14,8 @@ class Linker $input = ROOT . '/views/' . $this->view . '.php'; $temporaryFiles = []; + $css = []; + $js = []; $sections = []; $extra = ['', '']; @@ -23,12 +25,14 @@ class Linker $extends = $fragment->getExtends(); + $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) { - $this->writeFinal($extra, $input, ROOT . '/cache/views/' . $this->view . '.php'); + $this->writeFinal($css, $js, $extra, $input, ROOT . '/cache/views/' . $this->view . '.php'); break; } @@ -74,7 +78,7 @@ class Linker fclose($outputFileHandle); } - private function writeFinal(array $extra, string $file, string $output): void + private function writeFinal(array $css, array $js, array $extra, string $file, string $output): void { $dirname = pathinfo($output, PATHINFO_DIRNAME); if (!is_dir($dirname)) { @@ -91,6 +95,22 @@ 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); diff --git a/src/View/ParsedFragment.php b/src/View/ParsedFragment.php index 52bbb5b..a270a32 100644 --- a/src/View/ParsedFragment.php +++ b/src/View/ParsedFragment.php @@ -4,13 +4,19 @@ class ParsedFragment { private ?string $extends; + private array $css; + + private array $js; + private array $sections; private array $extra; - public function __construct(?string $extends, array $sections, array $extra) + public function __construct(?string $extends, array $css, array $js, array $sections, array $extra) { $this->extends = $extends; + $this->css = $css; + $this->js = $js; $this->sections = $sections; $this->extra = $extra; } @@ -20,6 +26,16 @@ class ParsedFragment return $this->extends; } + public function getCss(): array + { + return $this->css; + } + + public function getJs(): array + { + return $this->js; + } + public function getSections(): array { return $this->sections; diff --git a/src/View/Parser.php b/src/View/Parser.php index 6cbde82..0356027 100644 --- a/src/View/Parser.php +++ b/src/View/Parser.php @@ -11,11 +11,12 @@ class Parser public function parse(): ParsedFragment { - $sectionOpen = null; $extraOpen = false; $extends = null; + $js = []; + $css = []; $sections = []; $extra = ['', '']; @@ -28,6 +29,18 @@ class Parser while (($line = fgets($fileHandle)) !== false) { ++$lineNumber; + if (($cssMatched = $this->matchCss($line)) !== null) { + $css[] = $cssMatched; + + continue; + } + + if (($jsMatched = $this->matchJs($line)) !== null) { + $js[] = $jsMatched; + + continue; + } + if (($extendsMatched = $this->matchExtends($line)) !== null) { if ($extends !== null) { throw new \Exception('Error in file ' . $this->file . ' in line ' . $lineNumber . ' - There is already an \'@extends\' declared.'); @@ -90,7 +103,25 @@ class Parser fclose($fileHandle); - return new ParsedFragment($extends, $sections, $extra); + return new ParsedFragment($extends, $css, $js, $sections, $extra); + } + + private function matchCss(string $line): ?string + { + if (preg_match('/^\s*@css\((.*)\)\s*$/', $line, $matches)) { + return $matches[1]; + } + + return null; + } + + private function matchJs(string $line): ?string + { + if (preg_match('/^\s*@js\((.*)\)\s*$/', $line, $matches)) { + return $matches[1]; + } + + return null; } private function matchExtends(string $line): ?string diff --git a/views/admin/map_editor.php b/views/admin/map_editor.php index 1ca983f..c0d2e5a 100644 --- a/views/admin/map_editor.php +++ b/views/admin/map_editor.php @@ -1,19 +1,12 @@ -@extra - -@endextra +@css('node_modules/leaflet/dist/leaflet.css') +@css('css/map_editor.css') +@css('node_modules/leaflet.markercluster/dist/MarkerCluster.css') +@css('node_modules/leaflet.markercluster/dist/MarkerCluster.Default.css') + +@js('node_modules/leaflet/dist/leaflet.js') +@js('node_modules/leaflet.markercluster/dist/leaflet.markercluster.js') +@js('https://maps.googleapis.com/maps/api/js?key=' . $_ENV['GOOGLE_MAPS_JS_API_KEY']) +@js('js/map_editor.js') @extends('templates/layout_full') diff --git a/views/game.php b/views/game.php index bc4c63a..81dfc08 100644 --- a/views/game.php +++ b/views/game.php @@ -1,14 +1,7 @@ -@extra - -@endextra +@css('css/game.css') + +@js('https://maps.googleapis.com/maps/api/js?key=' . $_ENV['GOOGLE_MAPS_JS_API_KEY']) +@js('js/game.js') @extends('templates/layout_full') diff --git a/views/login/google_signup.php b/views/login/google_signup.php index 0f9fe9c..dd7b498 100644 --- a/views/login/google_signup.php +++ b/views/login/google_signup.php @@ -1,10 +1,4 @@ -@extra - -@endextra +@js('js/login/google_signup.js') @extends('templates/layout_normal') diff --git a/views/login/signup.php b/views/login/signup.php index 6912a27..ef63f04 100644 --- a/views/login/signup.php +++ b/views/login/signup.php @@ -1,10 +1,4 @@ -@extra - -@endextra +@js('js/login/signup.js') @extends('templates/layout_normal') diff --git a/views/maps.php b/views/maps.php index 002d3bb..22ecc11 100644 --- a/views/maps.php +++ b/views/maps.php @@ -1,16 +1,7 @@ -@extra - -@endextra +@css('css/maps.css') +@js('js/maps.js') +TODO: condition! +@js('js/maps_admin.js') @extends('templates/layout_normal') diff --git a/views/templates/mapguesser.php b/views/templates/mapguesser.php index 083fbb7..313d332 100644 --- a/views/templates/mapguesser.php +++ b/views/templates/mapguesser.php @@ -16,6 +16,7 @@ + @yields('inlineCss') @@ -62,6 +63,7 @@ + @yields('inlineJs') - @yields('pagescript') + @yields('pageScript') 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 3/4] 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')