Skip to content

Commit

Permalink
fix: patch up @include error
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Feb 17, 2023
1 parent 3cdb6d1 commit 83ad410
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/UI/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ protected static function buildComponent(Component $component, array $config, bo
Core::createElement('script', [], ['
window._leafUIConfig.methods = ' . json_encode(array_unique(static::$componentMethods)) . ';
window._leafUIConfig.components = ' . json_encode(static::$components) . ';
']) .
Core::init() . '</body>',
']) . Core::init() . '</body>',
static::compileTemplate($component->render(), static::$state[$component->key])
),
'state' => $pageState,
Expand All @@ -109,7 +108,7 @@ protected static function buildComponent(Component $component, array $config, bo
$pageState = [];
static::$state[$component->key] = array_merge(static::$state[$component->key], get_class_vars($component::class));
$parsedComponent = static::compileTemplate($component->render(), static::$state[$component->key]);

foreach (array_values(static::$state) as $key => $value) {
$pageState = array_merge($pageState, $value);
}
Expand Down Expand Up @@ -144,7 +143,7 @@ public static function view(string $filename): string
throw new \JsonException("$filename not found!");
}

return file_get_contents($filename);
return static::compileTemplate(file_get_contents($filename));
}

/**
Expand All @@ -153,6 +152,12 @@ public static function view(string $filename): string
*/
public static function compileTemplate(string $rawText, array $state = []): string
{
if (!$state) {
foreach (array_values(static::$state) as $key => $value) {
$state = array_merge($state, $value);
}
}

$compiled = preg_replace_callback('/{{(.*?)}}/', function ($matches) use ($state) {
return $state[ltrim(trim($matches[1]), '$')] ?? trigger_error($matches[1] . ' is not defined', E_USER_ERROR);
}, $rawText);
Expand Down Expand Up @@ -212,6 +217,10 @@ public static function compileTemplate(string $rawText, array $state = []): stri
return "<?php switch ($matches[1]): ?>";
}, $compiled);

$compiled = preg_replace_callback('/@loop\([\s\S]*?\)\s*[\s\S]*@endloop\s*/', function ($matches) {
return $matches[0];
}, $compiled);

$compiled = preg_replace_callback('/@case\((.*?)\)/', function ($matches) {
return "<?php case $matches[1]: ?>";
}, $compiled);
Expand Down

0 comments on commit 83ad410

Please sign in to comment.