Download tasks/170aaaacfe4a243e/source/patch.diff from FuCY/php: direct link, hf CLI and curl.
- Browser
- Download file 3.76 kB
-
https://huggingface.co/datasets/FuCY/php/resolve/main/tasks/170aaaacfe4a243e/source/patch.diff
- Command line
-
hf download hf://datasets/FuCY/php/tasks/170aaaacfe4a243e/source/patch.diff
-
curl -L -o patch.diff https://huggingface.co/datasets/FuCY/php/resolve/main/tasks/170aaaacfe4a243e/source/patch.diff
3.76 kB
| diff --git a/Template.php b/Template.php | |
| index f9a8613..746cb1d 100644 | |
| --- a/Template.php | |
| +++ b/Template.php | |
| 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 | |
| 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]); | |
| } | |
| 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); | |
| 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 | |
| { | |
| "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 | |
| 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); | |