Skip to content

Commit

Permalink
4.16
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecc-business-account committed Oct 11, 2024
1 parent 7f05b92 commit 94570df
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 19 deletions.
25 changes: 25 additions & 0 deletions examples/teststack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright (c) 2014 Jorge Patricio Castro Castillo MIT License.
*/
include "../lib/BladeOne.php";
use eftec\bladeone\BladeOne;

$views = __DIR__ . '/views';
$compiledFolder = __DIR__ . '/compiled';
$blade=new BladeOne($views,$compiledFolder,BladeOne::MODE_DEBUG);


$products=[
["name"=>"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()."<br>".$e->getTraceAsString();
}

28 changes: 28 additions & 0 deletions examples/views/Test2/stack.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---stack style:---<br>
@stack("style")
<br>---stack example1:---<br>
@stack("example1")
<br>---stack example*:---<br>
@stack("example*")
<br>---stack examplenotexist:---<br>
@stack("examplenotexist","notfound")
@push("example1")
alpha
@endpush
@push("example1")
beta
@endpush
@push("example1","gamma")

@foreach($products as $product)
<div class="blue">{{$product['name']}} ${{$product['price']}}</div>
@pushonce("style")
<style>
.blue {
background-color: blue;
color:white;
}
</style>
@endpushonce
@endforeach

71 changes: 52 additions & 19 deletions lib/BladeOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
//<editor-fold desc="fields">
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. */
Expand All @@ -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 = '<?php '; // hello hello hello.
/** @var string this line is used to easily echo a value */
Expand Down Expand Up @@ -766,7 +770,7 @@ public function runString($string, $data = []): string
$this->showError('runString', $lastError['message'] . ' ' . $lastError['type'], true);
return '';
}
return \ob_get_clean();
return $this->postRun(\ob_get_clean());
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<br>
* This value is cleared when it is used (method run).<br>
Expand Down Expand Up @@ -2947,6 +2978,7 @@ protected function getEchoMethods(): array
});
return $methods;
}

/**
* Compile Blade components that start with "x-".
*
Expand All @@ -2966,36 +2998,32 @@ 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-([a-z0-9.-]+)(\s[^>]*)?(>((?:(?!<\/x-\1>).)*)<\/x-\1>|\/>)/ms', $callback, $value);

}

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) . ']';
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 94570df

Please sign in to comment.