- Removed the unused EchoToken (which is instead located within the PrintExtension)
- Added inline functions and make sure function can't be redeclared
This commit is contained in:
1 parent
0eed9b76af
commit
046dec2819
3 files changed
+40
-38
No files matched your search
@@ -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 ?>";
|
||||
}
|
||||
}
|
||||
@@ -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 } ?>";
|
||||
}
|
||||
}
|
||||
@@ -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)) ?>";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user