aboutsummaryrefslogtreecommitdiffstats
path: root/core/namespace/Parsers/FunctionParser.php
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2024-11-15 16:41:11 +0100
committerThomas Lange <code@nerdmind.de>2024-11-15 17:10:11 +0100
commit5e076b70b79ddf7bc6e07122b4646961db280be5 (patch)
tree33d2a85bac6252db35913e42bfd9879925aaef6c /core/namespace/Parsers/FunctionParser.php
parenteb773b95f08dc79df1acad96296a92a9c71d48f1 (diff)
downloadblog-5e076b70b79ddf7bc6e07122b4646961db280be5.tar.gz
blog-5e076b70b79ddf7bc6e07122b4646961db280be5.tar.xz
blog-5e076b70b79ddf7bc6e07122b4646961db280be5.zip
FunctionParser: No static property/methods anymore
Problem: When creating a new "FunctionParser" object, it should not know the previously registered content functions which were saved in a static class property and registered via static class method. If one creates a new "FunctionParser" object, one would expect that the array of registered content functions in that object is empty. Changes: - Make static property "functions" non-static - Make static method "register" non-static - Add new method "registerFromArray"
Diffstat (limited to 'core/namespace/Parsers/FunctionParser.php')
-rw-r--r--core/namespace/Parsers/FunctionParser.php19
1 files changed, 14 insertions, 5 deletions
diff --git a/core/namespace/Parsers/FunctionParser.php b/core/namespace/Parsers/FunctionParser.php
index 02570a0..94a4c92 100644
--- a/core/namespace/Parsers/FunctionParser.php
+++ b/core/namespace/Parsers/FunctionParser.php
@@ -3,7 +3,7 @@ namespace Parsers;
use ReflectionFunction;
class FunctionParser implements ParserInterface {
- private static $functions = [];
+ private $functions = [];
#===============================================================================
# Main regex for matching the whole function call
@@ -39,19 +39,28 @@ class FunctionParser implements ParserInterface {
#===============================================================================
# Register function
#===============================================================================
- public static function register(string $name, callable $callback): void {
+ public function register(string $name, callable $callback): void {
$Function = new ReflectionFunction($callback);
- self::$functions[$name] = [
+ $this->functions[$name] = [
'callback' => $callback,
'required' => $Function->getNumberOfRequiredParameters()
];
}
#===============================================================================
+ # Register multiple functions from array
+ #===============================================================================
+ public function registerFromArray(array $functions): void {
+ foreach($functions as $name => $callback) {
+ $this->register($name, $callback);
+ }
+ }
+
+ #===============================================================================
# Parse functions
#===============================================================================
public function parse(string $text): array {
- $functionNames = array_keys(self::$functions);
+ $functionNames = array_keys($this->functions);
$functionNames = implode('|', $functionNames);
$pattern = self::FUNCTION_PATTERN;
@@ -72,7 +81,7 @@ class FunctionParser implements ParserInterface {
# Transform functions
#===============================================================================
public function transform(string $text): string {
- $functionData = self::$functions;
+ $functionData = $this->functions;
$functionNames = array_keys($functionData);
$functionNames = implode('|', $functionNames);