MAPG-187 refactor view parsing and linking

This commit is contained in:
bence committed 2020-06-28 19:35:14 +02:00
1 parent 71b1e28aa9
commit e883d184b7
2 files changed
+8 -11

No files matched your search

+6 -9
View File
@@ -108,7 +108,7 @@ class Linker
private function generateAssets(ParsedFragment $fragment, array &$sections)
{
foreach ($fragment->getCss() as $cssFile) {
$asset = $this->parseAsset($cssFile, 'css');
$asset = $this->parseAsset($cssFile);
if (isset($asset['code'])) {
$sections['inlineCss'] .= '<style>' . PHP_EOL;
$sections['inlineCss'] .= $asset['code'];
@@ -119,7 +119,7 @@ class Linker
}
foreach ($fragment->getJs() as $jsFile) {
$asset = $this->parseAsset($jsFile, 'js');
$asset = $this->parseAsset($jsFile);
if (isset($asset['code'])) {
$sections['inlineJs'] .= '<script>' . PHP_EOL;
$sections['inlineJs'] .= $asset['code'];
@@ -130,25 +130,22 @@ class Linker
}
}
private function parseAsset(string $asset, string $type): array
private function parseAsset(string $asset): array
{
$output = [];
eval('$asset = ' . $asset . ';');
if (preg_match('/^[\w\/\.]+$/', $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 ?>';
$output['file'] = '<?= $_ENV[\'STATIC_ROOT\'] ?>/' . $asset . '?rev=<?= REVISION ?>';
}
} else {
$output['file'] = $asset;
}
}
return $output;
}
+2 -2
View File
@@ -126,7 +126,7 @@ class Parser
private function matchExtends(string $line): ?string
{
if (preg_match('/^\s*@extends\(\'([\w\/]+)\'\)\s*$/', $line, $matches)) {
if (preg_match('/^\s*@extends\(([\w\/]+)\)\s*$/', $line, $matches)) {
return $matches[1];
}
@@ -135,7 +135,7 @@ class Parser
private function matchSection(string $line): ?string
{
if (preg_match('/^\s*@section\(\'(\w+)\'\)\s*$/', $line, $matches)) {
if (preg_match('/^\s*@section\((\w+)\)\s*$/', $line, $matches)) {
return $matches[1];
}