Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
046dec2819 | ||
|
|
0eed9b76af | ||
|
|
2931a462c0 |
No files matched your search
@@ -158,6 +158,19 @@ class Compiler
|
||||
return $this->extensionList[$tokenName];
|
||||
}
|
||||
|
||||
public function getExtensionInstanceOf(string $class) : Extension\Extension
|
||||
{
|
||||
foreach(array_reverse($this->extensionList) as $ext) {
|
||||
if ( is_a($ext, $class, false) ) {
|
||||
return $ext;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException(<<<MSG
|
||||
Requested extension instance of `$class` could not be found from loaded Picea compiler's extension.
|
||||
MSG);
|
||||
}
|
||||
|
||||
public function exportFunctions() : void
|
||||
{
|
||||
static $caching = [];
|
||||
|
||||
@@ -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%*/?>
|
||||
|
||||
@@ -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