From 046dec2819ea465a1a6bfb142710f776319fdc11 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 25 Nov 2026 19:21:55 +0000 Subject: [PATCH] - Removed the unused EchoToken (which is instead located within the PrintExtension) - Added inline functions and make sure function can't be redeclared --- src/ControlStructure/EchoToken.php | 28 ------------------------- src/ControlStructure/FunctionToken.php | 29 ++++++++++++++++++++------ src/Extension/PrintExtension.php | 21 +++++++++++++++---- 3 files changed, 40 insertions(+), 38 deletions(-) delete mode 100644 src/ControlStructure/EchoToken.php diff --git a/src/ControlStructure/EchoToken.php b/src/ControlStructure/EchoToken.php deleted file mode 100644 index 3afa2e3..0000000 --- a/src/ControlStructure/EchoToken.php +++ /dev/null @@ -1,28 +0,0 @@ -"; - - case "echo.raw": - return ""; - } - } - - public static function echoRaw($arguments) : string - { - return ""; - } - - public static function echoSafe($arguments) : string - { - return ""; - } -} diff --git a/src/ControlStructure/FunctionToken.php b/src/ControlStructure/FunctionToken.php index 61ea545..085df22 100644 --- a/src/ControlStructure/FunctionToken.php +++ b/src/ControlStructure/FunctionToken.php @@ -2,16 +2,18 @@ namespace Picea\ControlStructure; +use Picea\Compiler\Context; + class FunctionToken implements ControlStructure { public array $token = [ "function", "endfunction", "return" ]; - public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { + public function parse(Context &$context, ?string $arguments, string $token, array $options = []) : string { switch($token) { case "function": $context->functions++; - return $this->printFunction($context, $arguments); + return $this->printFunction($context, $options, $arguments); case "return": if ( empty($context->functions) ) { @@ -31,11 +33,26 @@ class FunctionToken implements ControlStructure { } } - protected function printFunction($context, ?string $arguments) : string + protected function printFunction(Context $context, array $options, ?string $arguments) : string { - $name = trim(explode('(', $arguments, 2)[0]); + list($name, $parsedArguments) = array_map('trim', explode('(', $arguments, 2)); - return ""; + if ( in_array('inline', array_map('strtolower', $options)) ) { + $endPos = strrpos($parsedArguments, ')') + 1; + $return = substr($parsedArguments, $endPos); + $use = " use (\$___class__template, \$___global_variables, \$___variables, \$__event, \$picea)"; + + if ( str_contains($return, ':') ) { + $parsedArguments = substr_replace($parsedArguments, $use, $endPos, 0); + } + else { + $parsedArguments .= $use; + } + + return ""; + } + + return ""; } protected function printReturn($context, ?string $arguments) : string @@ -45,6 +62,6 @@ class FunctionToken implements ControlStructure { protected function printEndFunction($context) : string { - return ""; + return ""; } } \ No newline at end of file diff --git a/src/Extension/PrintExtension.php b/src/Extension/PrintExtension.php index 29320c3..a9cc7b3 100644 --- a/src/Extension/PrintExtension.php +++ b/src/Extension/PrintExtension.php @@ -20,6 +20,19 @@ class PrintExtension implements Extension { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { + $arguments = "(string) $arguments"; + + if (in_array('lc', $options) || in_array('lowercase', $options)) { + $arguments = "strtolower($arguments)"; + } + elseif (in_array('uc', $options) || in_array('uppercase', $options)) { + $arguments = "strtoupper($arguments)"; + } + + if (in_array('ucfirst', $options)) { + $arguments = "ucfirst($arguments)"; + } + switch($token) { case 'echo': case 'print': @@ -27,17 +40,17 @@ class PrintExtension implements Extension { return ""; } - return "flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>"; + return "flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>"; case 'printf': if (in_array('raw', $options)) { - return ""; + return ""; } - return "flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>"; + return "flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>"; case 'no_html': - return ""; + return ""; } }