2 Commits
Author SHA1 Message Date
mcndave 046dec2819 - Removed the unused EchoToken (which is instead located within the PrintExtension)
- Added inline functions and make sure function can't be redeclared
2026-11-25 19:21:55 +00:00
mcndave 0eed9b76af - Fixed a bug related to encoding into a template exception 2026-10-27 18:18:47 +00:00
4 changed files with 43 additions and 39 deletions

No files matched your search

+3 -1
View File
@@ -31,6 +31,8 @@ class BlockToken implements ControlStructure {
return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>";
case "arguments":
$cleanArguments = htmlspecialchars($arguments);
return <<<PHP
<?php
try {
@@ -42,7 +44,7 @@ class BlockToken implements ControlStructure {
\$showArgs = fn(\$e) => (\$type = gettype(\$e)) === 'object' ? \$e::class : \$type;
throw new \Exception(
sprintf('A block awaiting arguments (%s) instead received (%s) with values (%s)', '$arguments', implode(', ', array_map(\$showArgs, \$inlineVariables ?? [])), json_encode(\$inlineVariables))
sprintf('A block awaiting arguments (%s) instead received (%s) with values (%s)', '$cleanArguments', implode(', ', array_map(\$showArgs, \$inlineVariables ?? [])), json_encode(\$inlineVariables))
);
}
/*%EXCEPTION_LINE_BASE%*/?>
-28
View File
@@ -1,28 +0,0 @@
<?php
namespace Picea\ControlStructure;
class EchoToken implements ControlStructure {
public array $token = [ "echo", "echo.raw" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
switch($token) {
case "echo":
return "<?php \\Picea\\ControlStructure\\EchoToken::echoSafe($arguments) ?>";
case "echo.raw":
return "<?php \\Picea\\ControlStructure\\EchoToken::echoRaw($arguments) ?>";
}
}
public static function echoRaw($arguments) : string
{
return "<?php echo $arguments ?>";
}
public static function echoSafe($arguments) : string
{
return "<?php echo $arguments ?>";
}
}
+23 -6
View File
@@ -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 "<?php if (! function_exists(__NAMESPACE__ . '\\$name')) { function $arguments { ?>";
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 "<?php if (isset(\$$name)) {throw new \LogicException('A variable named \'$name\' already exists in namespace ' . __NAMESPACE__);} \$$name = function ($parsedArguments { ?>";
}
return "<?php if (function_exists(__NAMESPACE__ . '\\$name')) {throw new \LogicException('A function named \'$name\' already exists in namespace ' . __NAMESPACE__);} function $arguments { ?>";
}
protected function printReturn($context, ?string $arguments) : string
@@ -45,6 +62,6 @@ class FunctionToken implements ControlStructure {
protected function printEndFunction($context) : string
{
return "<?php } } ?>";
return "<?php } ?>";
}
}
+17 -4
View File
@@ -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 "<?php echo $arguments ?>";
}
return "<?php echo htmlspecialchars((string) $arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
return "<?php echo htmlspecialchars($arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
case 'printf':
if (in_array('raw', $options)) {
return "<?php printf((string) $arguments) ?>";
return "<?php printf($arguments) ?>";
}
return "<?php echo htmlspecialchars(sprintf((string) $arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
return "<?php echo htmlspecialchars(sprintf($arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
case 'no_html':
return "<?php echo nl2br(strip_tags((string) $arguments)) ?>";
return "<?php echo nl2br(strip_tags($arguments)) ?>";
}
}