From 94570df4cd5e81a1efd0bb25d451bd715d529279 Mon Sep 17 00:00:00 2001 From: jorgecc Date: Fri, 11 Oct 2024 14:41:08 -0300 Subject: [PATCH] 4.16 --- examples/teststack.php | 25 ++++++++++ examples/views/Test2/stack.blade.php | 28 +++++++++++ lib/BladeOne.php | 71 ++++++++++++++++++++-------- 3 files changed, 105 insertions(+), 19 deletions(-) create mode 100644 examples/teststack.php create mode 100644 examples/views/Test2/stack.blade.php diff --git a/examples/teststack.php b/examples/teststack.php new file mode 100644 index 0000000..b1728ac --- /dev/null +++ b/examples/teststack.php @@ -0,0 +1,25 @@ +"cocacola","price"=>10], + ["name"=>"fanta","price"=>20], + ["name"=>"sprite","price"=>30], +]; + + +try { + echo $blade->run("Test2.stack", ['products'=>$products]); +} catch (Exception $e) { + echo "error found ".$e->getMessage()."
".$e->getTraceAsString(); +} + diff --git a/examples/views/Test2/stack.blade.php b/examples/views/Test2/stack.blade.php new file mode 100644 index 0000000..97e6c28 --- /dev/null +++ b/examples/views/Test2/stack.blade.php @@ -0,0 +1,28 @@ +---stack style:---
+@stack("style") +
---stack example1:---
+@stack("example1") +
---stack example*:---
+@stack("example*") +
---stack examplenotexist:---
+@stack("examplenotexist","notfound") +@push("example1") +alpha +@endpush +@push("example1") +beta +@endpush +@push("example1","gamma") + +@foreach($products as $product) +
{{$product['name']}} ${{$product['price']}}
+ @pushonce("style") + + @endpushonce +@endforeach + diff --git a/lib/BladeOne.php b/lib/BladeOne.php index f02ce45..8de2143 100644 --- a/lib/BladeOne.php +++ b/lib/BladeOne.php @@ -35,13 +35,13 @@ * @copyright Copyright (c) 2016-2024 Jorge Patricio Castro Castillo MIT License. * Don't delete this comment, its part of the license. * Part of this code is based in the work of Laravel PHP Components. - * @version 4.15.1 + * @version 4.16 * @link https://github.com/EFTEC/BladeOne */ class BladeOne { // - public const VERSION = '4.15.2'; + public const VERSION = '4.16'; /** @var int BladeOne reads if the compiled file has changed. If it has changed,then the file is replaced. */ public const MODE_AUTO = 0; /** @var int Then compiled file is always replaced. It's slow and it's useful for development. */ @@ -52,6 +52,10 @@ class BladeOne public const MODE_DEBUG = 5; /** @var array Hold dictionary of translations */ public static array $dictionary = []; + /** @var string It is used to mark the start of the stack (regexp). This value must not be used for other purposes */ + public string $escapeStack0 = '-#1Z#-#2B#'; + /** @var string It is used to mark the end of the stack (regexp). This value must not be used for other purposes */ + public string $escapeStack1 = '#3R#-#4X#-'; /** @var string PHP tag. You could use < ?php or < ? (if shorttag is active in php.ini) */ public string $phpTag = 'showError('runString', $lastError['message'] . ' ' . $lastError['type'], true); return ''; } - return \ob_get_clean(); + return $this->postRun(\ob_get_clean()); } /** @@ -1272,13 +1276,13 @@ protected function runInternal(string $view, $variables = [], $forced = false, $ } $result = $this->compile($view, $forced); if (!$this->isCompiled) { - return $this->evaluateText($result, $this->variables); + return $this->postRun($this->evaluateText($result, $this->variables)); } } elseif ($view) { $this->fileName = $view; } $this->isRunFast = $runFast; - return $this->evaluatePath($this->getCompiledFile(), $this->variables); + return $this->postRun($this->evaluatePath($this->getCompiledFile(), $this->variables)); } protected function evalComposer($view): void @@ -2180,6 +2184,33 @@ public function run($view = null, $variables = []): string return $this->runInternal($view, $variables, $forced, $runFast); } + /** + * It executes a post run execution. It is used to display the stacks. + * @noinspection PhpVariableIsUsedOnlyInClosureInspection + */ + protected function postRun(?string $string) + { + if (!$string) { + return $string; + } + if (strpos($string, $this->escapeStack0) === false) { + // nothing to post run + return $string; + } + $me = $this; + $result = preg_replace_callback('/' . $this->escapeStack0 . '\s?([A-Za-z0-9_:() ,*.@$]+)\s?' . $this->escapeStack1 . '/u', + static function($matches) use ($me) { + $l0 = strlen($me->escapeStack0); + $l1 = strlen($me->escapeStack1); + $item = trim(is_array($matches) ? substr($matches[0], $l0, -$l1) : substr($matches, $l0, -$l1)); + $items = explode(',', $item); + return $me->yieldPushContent($items[0], $items[1] ?? null); + //return is_array($r) ? $flagtxt . json_encode($r) : $flagtxt . $r; + }, $string); + // we returned the escape character. + return $result; + } + /** * It sets the current view
* This value is cleared when it is used (method run).
@@ -2947,6 +2978,7 @@ protected function getEchoMethods(): array }); return $methods; } + /** * Compile Blade components that start with "x-". * @@ -2966,19 +2998,15 @@ protected function compileComponents($value) * * @return string */ - $callback = function($match) { - - if(static::contains($match[0], 'x-')) { - $match[4] = $this->compileComponents( $match[4]); + 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(); + $str = "('components." . $match[1] . "'," . $paramsCompiled . ")"; + return self::compileComponent($str) . $match[4] . self::compileEndComponent(); }; return preg_replace_callback('/]*)?(>((?:(?!<\/x-\1>).)*)<\/x-\1>|\/>)/ms', $callback, $value); - } protected function parseParams($params): string @@ -2986,16 +3014,16 @@ protected function parseParams($params): string 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]); + $value = str_replace('"', '', $matches[2][$i]); //its php code - if(self::startsWith($key, ':')) { + if (self::startsWith($key, ':')) { $key = substr($key, 1); - $paramsCompiled[] = '"'.$key. '"' . '=>' . $value; + $paramsCompiled[] = '"' . $key . '"' . '=>' . $value; continue; } - $paramsCompiled[] = '"'.$key. '"' . '=>' .'"'. $value. '"'; + $paramsCompiled[] = '"' . $key . '"' . '=>' . '"' . $value . '"'; } - return '['.implode(',',$paramsCompiled).']'; + return '[' . implode(',', $paramsCompiled) . ']'; } /** @@ -4161,7 +4189,12 @@ protected function compileViewName($expression): string */ protected function compileStack($expression): string { - return $this->phpTagEcho . "\$this->yieldPushContent$expression; ?>"; + return $this->phpTagEcho . " \$this->CompileStackFinal$expression; ?>"; + } + + public function CompileStackFinal($a = null, $b = null): string + { + return $this->escapeStack0 . $a . ',' . $b . $this->escapeStack1; } /**