Skip to content

Commit 94570df

Browse files
4.16
1 parent 7f05b92 commit 94570df

File tree

3 files changed

+105
-19
lines changed

3 files changed

+105
-19
lines changed

examples/teststack.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2014 Jorge Patricio Castro Castillo MIT License.
4+
*/
5+
include "../lib/BladeOne.php";
6+
use eftec\bladeone\BladeOne;
7+
8+
$views = __DIR__ . '/views';
9+
$compiledFolder = __DIR__ . '/compiled';
10+
$blade=new BladeOne($views,$compiledFolder,BladeOne::MODE_DEBUG);
11+
12+
13+
$products=[
14+
["name"=>"cocacola","price"=>10],
15+
["name"=>"fanta","price"=>20],
16+
["name"=>"sprite","price"=>30],
17+
];
18+
19+
20+
try {
21+
echo $blade->run("Test2.stack", ['products'=>$products]);
22+
} catch (Exception $e) {
23+
echo "error found ".$e->getMessage()."<br>".$e->getTraceAsString();
24+
}
25+

examples/views/Test2/stack.blade.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---stack style:---<br>
2+
@stack("style")
3+
<br>---stack example1:---<br>
4+
@stack("example1")
5+
<br>---stack example*:---<br>
6+
@stack("example*")
7+
<br>---stack examplenotexist:---<br>
8+
@stack("examplenotexist","notfound")
9+
@push("example1")
10+
alpha
11+
@endpush
12+
@push("example1")
13+
beta
14+
@endpush
15+
@push("example1","gamma")
16+
17+
@foreach($products as $product)
18+
<div class="blue">{{$product['name']}} ${{$product['price']}}</div>
19+
@pushonce("style")
20+
<style>
21+
.blue {
22+
background-color: blue;
23+
color:white;
24+
}
25+
</style>
26+
@endpushonce
27+
@endforeach
28+

lib/BladeOne.php

+52-19
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
* @copyright Copyright (c) 2016-2024 Jorge Patricio Castro Castillo MIT License.
3636
* Don't delete this comment, its part of the license.
3737
* Part of this code is based in the work of Laravel PHP Components.
38-
* @version 4.15.1
38+
* @version 4.16
3939
* @link https://github.com/EFTEC/BladeOne
4040
*/
4141
class BladeOne
4242
{
4343
//<editor-fold desc="fields">
44-
public const VERSION = '4.15.2';
44+
public const VERSION = '4.16';
4545
/** @var int BladeOne reads if the compiled file has changed. If it has changed,then the file is replaced. */
4646
public const MODE_AUTO = 0;
4747
/** @var int Then compiled file is always replaced. It's slow and it's useful for development. */
@@ -52,6 +52,10 @@ class BladeOne
5252
public const MODE_DEBUG = 5;
5353
/** @var array Hold dictionary of translations */
5454
public static array $dictionary = [];
55+
/** @var string It is used to mark the start of the stack (regexp). This value must not be used for other purposes */
56+
public string $escapeStack0 = '-#1Z#-#2B#';
57+
/** @var string It is used to mark the end of the stack (regexp). This value must not be used for other purposes */
58+
public string $escapeStack1 = '#3R#-#4X#-';
5559
/** @var string PHP tag. You could use < ?php or < ? (if shorttag is active in php.ini) */
5660
public string $phpTag = '<?php '; // hello hello hello.
5761
/** @var string this line is used to easily echo a value */
@@ -766,7 +770,7 @@ public function runString($string, $data = []): string
766770
$this->showError('runString', $lastError['message'] . ' ' . $lastError['type'], true);
767771
return '';
768772
}
769-
return \ob_get_clean();
773+
return $this->postRun(\ob_get_clean());
770774
}
771775

772776
/**
@@ -1272,13 +1276,13 @@ protected function runInternal(string $view, $variables = [], $forced = false, $
12721276
}
12731277
$result = $this->compile($view, $forced);
12741278
if (!$this->isCompiled) {
1275-
return $this->evaluateText($result, $this->variables);
1279+
return $this->postRun($this->evaluateText($result, $this->variables));
12761280
}
12771281
} elseif ($view) {
12781282
$this->fileName = $view;
12791283
}
12801284
$this->isRunFast = $runFast;
1281-
return $this->evaluatePath($this->getCompiledFile(), $this->variables);
1285+
return $this->postRun($this->evaluatePath($this->getCompiledFile(), $this->variables));
12821286
}
12831287

12841288
protected function evalComposer($view): void
@@ -2180,6 +2184,33 @@ public function run($view = null, $variables = []): string
21802184
return $this->runInternal($view, $variables, $forced, $runFast);
21812185
}
21822186

2187+
/**
2188+
* It executes a post run execution. It is used to display the stacks.
2189+
* @noinspection PhpVariableIsUsedOnlyInClosureInspection
2190+
*/
2191+
protected function postRun(?string $string)
2192+
{
2193+
if (!$string) {
2194+
return $string;
2195+
}
2196+
if (strpos($string, $this->escapeStack0) === false) {
2197+
// nothing to post run
2198+
return $string;
2199+
}
2200+
$me = $this;
2201+
$result = preg_replace_callback('/' . $this->escapeStack0 . '\s?([A-Za-z0-9_:() ,*.@$]+)\s?' . $this->escapeStack1 . '/u',
2202+
static function($matches) use ($me) {
2203+
$l0 = strlen($me->escapeStack0);
2204+
$l1 = strlen($me->escapeStack1);
2205+
$item = trim(is_array($matches) ? substr($matches[0], $l0, -$l1) : substr($matches, $l0, -$l1));
2206+
$items = explode(',', $item);
2207+
return $me->yieldPushContent($items[0], $items[1] ?? null);
2208+
//return is_array($r) ? $flagtxt . json_encode($r) : $flagtxt . $r;
2209+
}, $string);
2210+
// we returned the escape character.
2211+
return $result;
2212+
}
2213+
21832214
/**
21842215
* It sets the current view<br>
21852216
* This value is cleared when it is used (method run).<br>
@@ -2947,6 +2978,7 @@ protected function getEchoMethods(): array
29472978
});
29482979
return $methods;
29492980
}
2981+
29502982
/**
29512983
* Compile Blade components that start with "x-".
29522984
*
@@ -2966,36 +2998,32 @@ protected function compileComponents($value)
29662998
*
29672999
* @return string
29683000
*/
2969-
29703001
$callback = function($match) {
2971-
2972-
if(static::contains($match[0], 'x-')) {
2973-
$match[4] = $this->compileComponents( $match[4]);
3002+
if (static::contains($match[0], 'x-')) {
3003+
$match[4] = $this->compileComponents($match[4]);
29743004
}
29753005
$paramsCompiled = $this->parseParams($match[2]);
2976-
$str = "('components.".$match[1]."',".$paramsCompiled.")";
2977-
2978-
return self::compileComponent($str).$match[4].self::compileEndComponent();
3006+
$str = "('components." . $match[1] . "'," . $paramsCompiled . ")";
3007+
return self::compileComponent($str) . $match[4] . self::compileEndComponent();
29793008
};
29803009
return preg_replace_callback('/<x-([a-z0-9.-]+)(\s[^>]*)?(>((?:(?!<\/x-\1>).)*)<\/x-\1>|\/>)/ms', $callback, $value);
2981-
29823010
}
29833011

29843012
protected function parseParams($params): string
29853013
{
29863014
preg_match_all('/([a-z-0-9:]*?)\s*?=\s*?(.+?)(\s|$)/ms', $params, $matches);
29873015
$paramsCompiled = [];
29883016
foreach ($matches[1] as $i => $key) {
2989-
$value = str_replace('"','',$matches[2][$i]);
3017+
$value = str_replace('"', '', $matches[2][$i]);
29903018
//its php code
2991-
if(self::startsWith($key, ':')) {
3019+
if (self::startsWith($key, ':')) {
29923020
$key = substr($key, 1);
2993-
$paramsCompiled[] = '"'.$key. '"' . '=>' . $value;
3021+
$paramsCompiled[] = '"' . $key . '"' . '=>' . $value;
29943022
continue;
29953023
}
2996-
$paramsCompiled[] = '"'.$key. '"' . '=>' .'"'. $value. '"';
3024+
$paramsCompiled[] = '"' . $key . '"' . '=>' . '"' . $value . '"';
29973025
}
2998-
return '['.implode(',',$paramsCompiled).']';
3026+
return '[' . implode(',', $paramsCompiled) . ']';
29993027
}
30003028

30013029
/**
@@ -4161,7 +4189,12 @@ protected function compileViewName($expression): string
41614189
*/
41624190
protected function compileStack($expression): string
41634191
{
4164-
return $this->phpTagEcho . "\$this->yieldPushContent$expression; ?>";
4192+
return $this->phpTagEcho . " \$this->CompileStackFinal$expression; ?>";
4193+
}
4194+
4195+
public function CompileStackFinal($a = null, $b = null): string
4196+
{
4197+
return $this->escapeStack0 . $a . ',' . $b . $this->escapeStack1;
41654198
}
41664199

41674200
/**

0 commit comments

Comments
 (0)