- A lot of bugfixes made for this release.
This commit is contained in:
1 parent
4e909b2840
commit
8df38b568c
13 files changed
+159
-71
No files matched your search
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Extension;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
interface FunctionExtension
|
||||
{
|
||||
public function exportFunctions() : array;
|
||||
}
|
||||
@@ -4,38 +4,35 @@ namespace Picea\Extension;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
class JsonExtension implements Extension {
|
||||
class JsonExtension implements Extension, FunctionExtension {
|
||||
|
||||
public array $token = [ "json", "json.pretty" ];
|
||||
|
||||
public int $flags = JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_QUOT | \JSON_THROW_ON_ERROR;
|
||||
|
||||
public function __construct(? Context $context = null) {
|
||||
if ($context) {
|
||||
$this->register($context);
|
||||
}
|
||||
}
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token)
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token)
|
||||
{
|
||||
$flag = $this->flags;
|
||||
|
||||
|
||||
switch ($token) {
|
||||
case "json.pretty":
|
||||
$flag |= \JSON_PRETTY_PRINT;
|
||||
break;
|
||||
|
||||
break;
|
||||
|
||||
case "json.html":
|
||||
return "<?php echo htmlentities(json_encode($arguments, {$this->flags}), ENT_QUOTES, 'UTF-8') ?>";
|
||||
}
|
||||
|
||||
return "<?php echo json_encode($arguments, $flag) ?>";
|
||||
}
|
||||
|
||||
public function register(Context $context) : void
|
||||
|
||||
public function exportFunctions(): array
|
||||
{
|
||||
$context->pushFunction("json", function($arguments, ? int $flags = null) {
|
||||
return json_encode($arguments, \JSON_FORCE_OBJECT);
|
||||
});
|
||||
return [
|
||||
'json' => function($arguments, ? int $flags = null) {
|
||||
return json_encode($arguments, \JSON_FORCE_OBJECT);
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace Picea\Extension;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
class LanguageExtension implements Extension {
|
||||
class LanguageExtension implements Extension, FunctionExtension {
|
||||
|
||||
public array $tokens = [ "lang", "_", "language.set" ];
|
||||
|
||||
@@ -12,11 +12,18 @@ class LanguageExtension implements Extension {
|
||||
|
||||
protected LanguageHandler $languageHandler;
|
||||
|
||||
public function __construct(Context $context, LanguageHandler $handler) {
|
||||
$this->register($context);
|
||||
public function __construct(LanguageHandler $handler) {
|
||||
$this->languageHandler = $handler;
|
||||
}
|
||||
|
||||
public function exportFunctions(): array
|
||||
{
|
||||
return [
|
||||
'_' => 'relativeLang',
|
||||
'lang' => 'absoluteLang',
|
||||
];
|
||||
}
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
|
||||
{
|
||||
switch($token) {
|
||||
@@ -35,8 +42,6 @@ class LanguageExtension implements Extension {
|
||||
|
||||
public function register(Context $context) : void
|
||||
{
|
||||
$context->pushFunction("_", [ $this, 'relativeLang' ]);
|
||||
$context->pushFunction("lang", [ $this, 'absoluteLang' ]);
|
||||
}
|
||||
|
||||
public function relativeLang(string $key, array $variables = []) #: array|string
|
||||
|
||||
@@ -14,7 +14,7 @@ class UrlExtension implements Extension {
|
||||
|
||||
protected array $routesTarget;
|
||||
|
||||
public array $tokens = [ "url" , "route", "asset", "url.parameters" ];
|
||||
public array $tokens = [ "url" , "route", "asset", "url.current", "url.parameters" ];
|
||||
|
||||
public function __construct(Context $context, string $urlBase = "", string $assetToken = "") {
|
||||
$this->urlBase = trim($urlBase, "/");
|
||||
@@ -33,7 +33,10 @@ class UrlExtension implements Extension {
|
||||
|
||||
case "url":
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildUrl($arguments) ?>";
|
||||
|
||||
|
||||
case "url.current":
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>";
|
||||
|
||||
case "url.parameters":
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>";
|
||||
}
|
||||
@@ -44,6 +47,7 @@ class UrlExtension implements Extension {
|
||||
public function register(Context $context) : void
|
||||
{
|
||||
$context->pushFunction("url", [ $this, 'buildUrl' ]);
|
||||
$context->pushFunction("current_url", [ $this, 'currentUrl' ]);
|
||||
$context->pushFunction("asset", [ $this, 'buildAssetUrl' ]);
|
||||
$context->pushFunction("route", [ $this, 'buildRouteUrl' ]);
|
||||
}
|
||||
@@ -58,6 +62,11 @@ class UrlExtension implements Extension {
|
||||
return $url . ( $parameters ? "?" . http_build_query($parameters) : "" );
|
||||
}
|
||||
|
||||
public function currentUrl(array $parameters = []) : string
|
||||
{
|
||||
return $this->buildUrl($this->uri(), $parameters);
|
||||
}
|
||||
|
||||
public function buildUrl(string $uri = "", array $parameters = []) : string
|
||||
{
|
||||
return $this->setUrlParameters($this->url() . "/" . ltrim($uri, "/"), $parameters);
|
||||
@@ -108,11 +117,7 @@ class UrlExtension implements Extension {
|
||||
$uri = explode('?', $uri, 2)[0];
|
||||
}
|
||||
|
||||
if ( ($base = $this->config['base'] ?? false) && ( stripos($uri, $base) === 0 ) ) {
|
||||
$uri = substr($uri, strlen($base));
|
||||
}
|
||||
|
||||
return '/' . ltrim($uri, '/');
|
||||
return '/' . ltrim(substr($uri, strlen($this->base())), '/');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -177,23 +182,23 @@ class UrlExtension implements Extension {
|
||||
if (strpos($item[1], "=") !== false) {
|
||||
list($variable, $default) = explode('=', $item[1]);
|
||||
}
|
||||
elseif (strpos($item[1], ":") !== false) {
|
||||
list($variable, ) = explode(':', $item[1]);
|
||||
}
|
||||
else {
|
||||
$variable = $item[1];
|
||||
}
|
||||
|
||||
|
||||
if ( array_key_exists($variable, $arguments) ) {
|
||||
$value = $arguments[ $item[1] ];
|
||||
}
|
||||
else if ( isset($default) ) {
|
||||
$value = ""; # $default;
|
||||
$value = $arguments[ $variable ];
|
||||
unset($arguments[ $variable ]);
|
||||
}
|
||||
else {
|
||||
$variable = $item[1];
|
||||
$value = $default ?? "";
|
||||
}
|
||||
|
||||
$search[ $item[0] ] = $value;
|
||||
|
||||
unset($arguments[ $item[1] ]);
|
||||
}
|
||||
|
||||
$route = str_replace(array_keys($search), array_values($search), $route);
|
||||
|
||||
Reference in new issue
Block a user