Skip to content

Commit

Permalink
feat: switch to use ui-state
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Apr 12, 2023
1 parent 99c0c04 commit 83f6146
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions src/UI/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class Core
*/
public static function init(): string
{
return implode(static::$scripts) . static::createElement('script', ['src' => '/vendor/leafs/ui/client/dist/ui.cjs.production.min.js'], ['']);
return implode(static::$scripts) . static::createElement('script', [
'src' => '/vendor/leafs/ui/client/dist/ui.cjs.production.min.js',
], ['']);
}

/**
Expand All @@ -45,10 +47,13 @@ public static function render($component)
$config = json_decode((new \Leaf\Http\Request())->get('_leaf_ui_config', false) ?? '', true) ?? [];

if (!$component->key) {
$component->key = static::randomId($component::class);
$component->key = Utils::randomId($component::class);
}

static::$state[$component->key] = get_class_vars($component::class);
static::$state[$component->key] = array_merge(get_class_vars($component::class), [
'key' => $component->key,
]);

static::$componentMethods = array_merge(static::$componentMethods, get_class_methods($component));
static::$mappedComponentMethods = array_merge(static::$mappedComponentMethods, [$component->key => get_class_methods($component)]);
static::$components = array_merge(static::$components, [$component->key => $component::class]);
Expand Down Expand Up @@ -121,29 +126,32 @@ protected static function buildComponent(Component $component, array $config, bo
}

$pageState = [];
$componentState = static::$state[$componentCalled->key];
foreach (array_values(static::$state) as $key => $value) {
$pageState = array_merge($pageState, $value);
}

return [
'responseType' => 'json',
'html' => $withoutScripts ?
static::compileTemplate($component->render(), $pageState) :
preg_replace('/<(\w+)([^>]*)>/i', "<$1 ui-state='" . json_encode($componentState) . "' $2>", static::compileTemplate($component->render(), $pageState), 1) :
str_replace(
'</body>',
Core::createElement('script', [], ['
window._leafUIConfig.methods = ' . json_encode(array_unique(static::$componentMethods)) . ';
window._leafUIConfig.components = ' . json_encode(static::$components) . ';
']) . Core::init() . '</body>',
static::compileTemplate($component->render(), $pageState)
preg_replace('/<(\w+)([^>]*)>/i', "<$1 ui-state='" . json_encode($componentState) . "' $2>", static::compileTemplate($component->render(), $pageState), 1)
),
'state' => $pageState,
];
}

$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]);
static::$state[$component->key] = array_merge(static::$state[$component->key], get_class_vars($component::class), [
'key' => $component->key,
]);
$componentState = static::$state[$component->key];
$parsedComponent = preg_replace('/<(\w+)([^>]*)>/i', "<$1 ui-state='" . json_encode($componentState) . "' $2>", static::compileTemplate($component->render(), static::$state[$component->key]), 1);

foreach (array_values(static::$state) as $key => $value) {
$pageState = array_merge($pageState, $value);
Expand All @@ -158,13 +166,11 @@ protected static function buildComponent(Component $component, array $config, bo
el: document.querySelector("body"),
component: "' . $component::class . '",
components: ' . json_encode(static::$components) . ',
data: ' . json_encode(array_merge($pageState, ['key' => null])) . ',
methods: ' . json_encode(array_unique(static::$componentMethods)) . ',
path: "' . $_SERVER['REQUEST_URI'] . '",
requestMethod: "' . $_SERVER['REQUEST_METHOD'] . '",
};
']) . Core::init() . '</body>', $parsedComponent),
'state' => json_encode($pageState),
];
}

Expand Down Expand Up @@ -198,6 +204,12 @@ public static function compileTemplate(string $rawText, array $state = []): stri
return $state[ltrim(trim($matches[1]), '$')] ?? trigger_error($matches[1] . ' is not defined', E_USER_ERROR);
}, $rawText);

$compiled = preg_replace_callback('/<style.*?>(.*?)<\/style>/is', function ($matches) {
$newCSS = (new CSS())->add($matches[1])->minify();
return str_replace($matches[1], $newCSS, $matches[0]);
}, $compiled);


$compiled = preg_replace_callback('/\$eval\((.*?)\)/', function ($matches) use ($state) {
$compiledWithVars = preg_replace_callback('/\$([a-zA-Z0-9_]+)/', function ($matches) use ($state) {
return $state[$matches[1]] ?? trigger_error($matches[1] . ' is not defined', E_USER_ERROR);
Expand Down Expand Up @@ -344,10 +356,10 @@ public static function component(string $component, array $props = []): string
$component = new $component;

if (!$component->key) {
$component->key = static::randomId($component::class);
$component->key = Utils::randomId($component::class);
}

static::$state[$component->key] = array_merge(get_class_vars($component::class), $props, static::$state[$component->key] ?? []);
static::$state[$component->key] = array_merge(get_class_vars($component::class), static::$state[$component->key] ?? [], ['key' => $component->key], $props);
static::$componentMethods = array_merge(static::$componentMethods, get_class_methods($component));
static::$mappedComponentMethods = array_merge(static::$mappedComponentMethods, [$component->key => get_class_methods($component)]);
static::$components = array_merge(static::$components, [$component->key => $component::class]);
Expand All @@ -363,7 +375,7 @@ public static function component(string $component, array $props = []): string
],
];

$componentData = static::buildComponent(new $component, $config, true);
$componentData = static::buildComponent($component, $config, true);

return $componentData['html'];
}
Expand Down Expand Up @@ -442,22 +454,6 @@ public static function loop($array, callable $handler)
return $element;
}

/**
* Generate a random id
*
* @param string $element An html element name to append to id
* @return string The random id
*/
public static function randomId($element = '')
{
$rand = '';
$seed = str_split('abcdefghijklmnopqrstuvwxyz' . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . '0123456789_-');
shuffle($seed);
foreach (array_rand($seed, 5) as $k) $rand .= $seed[$k];

return "$rand-$element";
}

protected static function parseStyles(array $styles): string
{
$parsedStyles = '';
Expand Down

0 comments on commit 83f6146

Please sign in to comment.