Skip to content

Commit

Permalink
basic x-component support
Browse files Browse the repository at this point in the history
  • Loading branch information
elaps committed Jul 8, 2024
1 parent 896764d commit bb8743a
Showing 1 changed file with 55 additions and 5 deletions.
60 changes: 55 additions & 5 deletions lib/BladeOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class BladeOne
/** @var array All the available compiler functions. */
protected array $compilers = [
'Extensions',
'Components',
'Statements',
'Comments',
'Echos',
Expand Down Expand Up @@ -2894,6 +2895,55 @@ protected function getEchoMethods(): array
});
return $methods;
}
/**
* Compile Blade components that start with "x-".
*
* @param string $value
*
* @return array|string|string[]|null
*/
protected function compileComponents($value)
{
/**
* @param array $match
* [0]=full expression with @ and parenthesis
* [1]=Component name
* [2]=parameters
* [3]=...
* [4]=content
*
* @return mixed|string
*/

$callback = function($match) {

if(static::contains($match[0], 'x-')) {
$match[4] = $this->compileComponents( $match[4]);
}
$paramsCompiled = $this->parseParams($match[2]);
$str = "('components.".$match[1]."',".$paramsCompiled.")";

return self::compileComponent($str).$match[4].self::compileEndComponent();
};
return preg_replace_callback('/<x-([a-z0-9.-]+)(\s[^>]*)?(>((?:(?!<\/x-\1>).)*)<\/x-\1>|\/>)/ms', $callback, $value);

}

protected function parseParams($params) {
preg_match_all('/([a-z-0-9:]*?)\s*?=\s*?(.+?)(\s|$)/ms', $params, $matches);
$paramsCompiled = [];
foreach ($matches[1] as $i => $key) {
$value = str_replace('"','',$matches[2][$i]);
//its php code
if(self::startsWith($key, ':')) {
$key = substr($key, 1);
$paramsCompiled[] = '"'.$key. '"' . '=>' . $value;
continue;
}
$paramsCompiled[] = '"'.$key. '"' . '=>' .'"'. $value. '"';
}
return '['.implode(',',$paramsCompiled).']';
}

/**
* Compile Blade statements that start with "@".
Expand Down Expand Up @@ -4191,12 +4241,12 @@ protected function runtimeStyle($expression=null,$separator=' '): string
* @param string $expression
* @return string
*/
protected function compileSelected($expression): string
{
return $this->phpTag . "if$expression echo 'selected'; ?>";
}
protected function compileSelected($expression): string
{
return $this->phpTag . "if$expression echo 'selected'; ?>";
}

/**
/**
* Compile the disabled statements into valid PHP.
*
* @param string $expression
Expand Down

0 comments on commit bb8743a

Please sign in to comment.