MAPG-180 declare CSS and JS files

This commit is contained in:
bence committed 2020-06-27 02:25:54 +02:00
1 parent 5e645a1313
commit 18bf9aaade
9 files changed
+93 -59

No files matched your search

+22 -2
View File
@@ -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' . 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);
+17 -1
View File
@@ -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;
+33 -2
View File
@@ -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