php / tasks /170aaaacfe4a243e /source /patch.diff
FuCY's picture
Add files using upload-large-folder tool
4d2a9bf verified
Raw History Blame Contribute Delete
3.76 kB
diff --git a/Template.php b/Template.php
index f9a8613..746cb1d 100644
--- a/Template.php
+++ b/Template.php
@@ -24,12 +24,10 @@ static function embrace($content, $array)
$saveClosing = preg_quote(TemplateFunctions::getDelimiters()[1]);
foreach ($flatArray as $flatKey => $value){
$flatKey = preg_replace('/[\/\.\\\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:\-]/', "\\\\$0",$flatKey);
- if(is_callable($value)){
- TemplateFunctions::registerClosure($flatKey,$value);
- } else {
- $content = preg_replace("/$saveOpening\s*$flatKey\s*$saveClosing/", $value, $content);
- $content = TemplateFunctions::tryClosures($flatArray, $content, false);
- }
+ // PATCHED: direct function injection is not allowed anymore
+ $content = preg_replace("/$saveOpening\s*$flatKey\s*$saveClosing/", $value, $content);
+ $content = TemplateFunctions::tryClosures($flatArray, $content, false);
+
}
return $content;
diff --git a/TemplateFunctions.php b/TemplateFunctions.php
index 783dc49..48941eb 100644
--- a/TemplateFunctions.php
+++ b/TemplateFunctions.php
@@ -103,7 +103,7 @@ private static function retrieveClosurePattern($pure, $closureName)
if (!$pure) {
$pattern .= preg_quote(self::$registeredDelimiters[0]) . "\s*";
}
- $pattern .= "$closureName\(([a-z0-9,\.\s]+)\)";
+ $pattern .= "$closureName\(([a-z0-9,\.\s_]+)\)";
if (!$pure) {
$pattern .= "\s*" . preg_quote(self::$registeredDelimiters[1]);
}
@@ -201,7 +201,6 @@ private static function evaluateTypedCondition(array $flatArray, $expression): b
foreach ($flatArray as $key => $value) {
$pattern = '/' . $key . '([^.]|$)/';
if (preg_match($pattern, $expression, $matches)) {
-
switch (gettype($flatArray[$key])) {
case 'boolean':
$expression = str_replace($key, $flatArray[$key] ? 'true' : 'false', $expression);
@@ -241,11 +240,12 @@ static function nIf($content, $array)
return $content;
}
+ $array = Template::flattenArray($array);
+ // important: first try closures
+ $array = array_merge(self::$registeredClosures, $array);
foreach ($hits as $hit) {
$expression = $hit->getAttribute('n-if');
- $array = Template::flattenArray($array);
$bool = self::evaluateTypedCondition($array, $expression);
-
if (!$bool) {
$hit->parentNode->removeChild($hit);
} else {
diff --git a/composer.json b/composer.json
index 97ea57b..dffe8ac 100644
--- a/composer.json
+++ b/composer.json
@@ -1,13 +1,12 @@
{
"name": "neoan3-apps/template",
"description": "neoan3 minimal template engine",
- "version": "1.1.0",
+ "version": "1.1.1",
"license": "MIT",
"autoload": {
"psr-4": {
"Neoan3\\Apps\\": "./"
}
-
},
"require": {
"ext-openssl": "*",
diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php
index 20e4e2e..dda1d75 100644
--- a/tests/TemplateTest.php
+++ b/tests/TemplateTest.php
@@ -113,11 +113,11 @@ public function testEmbraceTypes()
public function testCallback()
{
$array = [
- 'myFunc' => function ($x) {
- return strtoupper($x);
- },
'some' => 'value'
];
+ TemplateFunctions::registerClosure('myFunc',function($x){
+ return strtoupper($x);
+ });
$t = Template::embraceFromFile('callback.html', $array);
$this->assertStringContainsString('<p>VALUE</p>', $t);