- Token name and options are now passed through parse() method
This commit is contained in:
1 parent
e9b2f0ece3
commit
b3a8e3aca2
29 files changed
+70
-59
No files matched your search
@@ -3,5 +3,5 @@
|
||||
namespace Picea\Extension;
|
||||
|
||||
interface Extension {
|
||||
public function parse(\Picea\Compiler\Context &$context, string $sourceCode, string $token);
|
||||
public function parse(\Picea\Compiler\Context &$context, string $sourceCode, string $token, array $options = []) : string;
|
||||
}
|
||||
@@ -10,17 +10,15 @@ class JsonExtension implements Extension, FunctionExtension {
|
||||
|
||||
public int $flags = JSON_HEX_TAG | \JSON_HEX_QUOT | \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_UNICODE;
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token)
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
$opt = $context->tokenOptions($token);
|
||||
|
||||
$flag = $this->flags;
|
||||
|
||||
if ( in_array('pretty', $opt) ) {
|
||||
if ( in_array('pretty', $options) ) {
|
||||
$flag |= \JSON_PRETTY_PRINT;
|
||||
}
|
||||
|
||||
if ( in_array('html', $opt) ) {
|
||||
if ( in_array('html', $options) ) {
|
||||
return "<?php echo htmlentities(json_encode($arguments, $flag), ENT_QUOTES, 'UTF-8') ?>";
|
||||
}
|
||||
|
||||
|
||||
@@ -25,10 +25,8 @@ class LanguageExtension implements Extension, FunctionExtension {
|
||||
];
|
||||
}
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
$opt = $context->tokenOptions($token);
|
||||
|
||||
switch(explode('.', $token)[0]) {
|
||||
case "language":
|
||||
$cls = $this::class;
|
||||
@@ -49,14 +47,14 @@ class LanguageExtension implements Extension, FunctionExtension {
|
||||
PHP;
|
||||
|
||||
case "lang":
|
||||
if ( in_array('raw', $opt) ) {
|
||||
if ( in_array('raw', $options) ) {
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>";
|
||||
}
|
||||
|
||||
return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>";
|
||||
|
||||
case "_":
|
||||
if ( in_array('raw', $opt) ) {
|
||||
if ( in_array('raw', $options) ) {
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>";
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,8 @@ class MoneyExtension implements Extension, FunctionExtension {
|
||||
];
|
||||
}
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
return "<?php echo money($arguments) ?>";
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ class PhpExtension implements Extension {
|
||||
|
||||
public array $token = [ "php" ];
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
return "<?php $arguments ?>";
|
||||
}
|
||||
}
|
||||
@@ -18,21 +18,19 @@ class PrintExtension implements Extension {
|
||||
$this->encoding = ini_get("default_charset");
|
||||
}
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
$opt = $context->tokenOptions($token);
|
||||
|
||||
switch($context->tokenName($token)) {
|
||||
switch($token) {
|
||||
case 'echo':
|
||||
case 'print':
|
||||
if (in_array('raw', $opt)) {
|
||||
if (in_array('raw', $options)) {
|
||||
return "<?php echo $arguments ?>";
|
||||
}
|
||||
|
||||
return "<?php echo htmlspecialchars((string) $arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
|
||||
|
||||
case 'printf':
|
||||
if (in_array('raw', $opt)) {
|
||||
if (in_array('raw', $options)) {
|
||||
return "<?php printf((string) $arguments) ?>";
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,8 @@ class TitleExtension implements Extension, FunctionExtension {
|
||||
];
|
||||
}
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
return <<<PHP
|
||||
<?php
|
||||
if ( null !== \$title = title($arguments) ) {
|
||||
|
||||
@@ -29,10 +29,8 @@ PATTERN;
|
||||
$this->assetToken = $assetToken;
|
||||
}
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : ?string
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
$opt = $context->tokenOptions($token);
|
||||
|
||||
switch($token) {
|
||||
case "asset":
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>";
|
||||
@@ -41,10 +39,10 @@ PATTERN;
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>";
|
||||
|
||||
case "url":
|
||||
if ( in_array('parameters', $opt) ) {
|
||||
if ( in_array('parameters', $options) ) {
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>";
|
||||
}
|
||||
elseif ( in_array('current', $opt) ) {
|
||||
elseif ( in_array('current', $options) ) {
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>";
|
||||
}
|
||||
|
||||
@@ -54,7 +52,7 @@ PATTERN;
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->slug($arguments) ?>";
|
||||
}
|
||||
|
||||
return null;
|
||||
throw new \ParseExtensionException("Unknown token given $token in UrlExtension.");
|
||||
}
|
||||
|
||||
public function exportFunctions(): array
|
||||
|
||||
Reference in new issue
Block a user