10 Commits
33 changed files with 188 additions and 124 deletions

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@
"authors": [ "authors": [
{ {
"name": "Dave Mc Nicoll", "name": "Dave Mc Nicoll",
"email": "mcndave@gmail.com" "email": "info@mcnd.ca"
} }
], ],
"autoload": { "autoload": {
+3 -3
View File
@@ -10,7 +10,7 @@ use function DI\autowire, DI\create, DI\get;
use Laminas\Diactoros\Response\HtmlResponse; use Laminas\Diactoros\Response\HtmlResponse;
use Picea\{ Picea, Caching\Cache, Caching\Opcache, Compiler, Compiler\Context, Compiler\BaseContext, FileFetcher, Language\DefaultRegistrations, Method\Request }; use Picea\{ Picea, Caching\Cache, Caching\Opcache, Compiler, Compiler\Context, Compiler\BaseContext, FileFetcher, Language\DefaultRegistrations, Method\Request };
use Picea\Extension\{ LanguageHandler, LanguageExtension, TitleExtension, MoneyExtension, UrlExtension }; use Picea\Extension\{ LanguageHandler, LanguageExtension, TitleExtension, NumberExtension, UrlExtension };
use Picea\Ui\{ Method, Ui }; use Picea\Ui\{ Method, Ui };
return [ return [
@@ -26,7 +26,7 @@ return [
return new Compiler(new class(array_merge([ return new Compiler(new class(array_merge([
$c->get(LanguageExtension::class), $c->get(LanguageExtension::class),
$c->get(TitleExtension::class), $c->get(TitleExtension::class),
$c->get(MoneyExtension::class), $c->get(NumberExtension::class),
$c->get(UrlExtension::class), $c->get(UrlExtension::class),
$c->get(Method\Form::class), $c->get(Method\Form::class),
$c->get(Method\Pagination::class), $c->get(Method\Pagination::class),
@@ -68,7 +68,7 @@ return [
TitleExtension::class => autowire(TitleExtension::class), TitleExtension::class => autowire(TitleExtension::class),
MoneyExtension::class => autowire(MoneyExtension::class), NumberExtension::class => autowire(NumberExtension::class),
UrlExtension::class => create(UrlExtension::class)->constructor(get(Context::class), getenv("URL_BASE"), get('git.commit')), UrlExtension::class => create(UrlExtension::class)->constructor(get(Context::class), getenv("URL_BASE"), get('git.commit')),
+4 -3
View File
@@ -50,14 +50,15 @@ class Compiler
list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null); list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null);
$token = strtolower(trim($token)); $token = strtolower(trim($token));
$tokenName = explode('.', $token, 2)[0]; $tokenName = $context->tokenName($token);
$tokenOptions = $context->tokenOptions($token);
# @TODO Refractor this parts to allows registration to the tag's name # @TODO Refractor this parts to allows registration to the tag's name
if ( $this->tagList[$tokenName] ?? false ) { if ( $this->tagList[$tokenName] ?? false ) {
return $this->tagList[$tokenName]->parse($context, $arguments, $token); return $this->tagList[$tokenName]->parse($context, $arguments, $tokenName, $tokenOptions);
} }
elseif ( $this->extensionList[$tokenName] ?? false ) { elseif ( $this->extensionList[$tokenName] ?? false ) {
return $this->extensionList[$tokenName]->parse($context, $arguments, $token); return $this->extensionList[$tokenName]->parse($context, $arguments, $tokenName, $tokenOptions);
} }
else { else {
throw new \LogicException("Impossible to find token `$tokenName` declared in `{$matches[2]}`. Perhapse you forgot to add a custom token to Picea's engine ?"); throw new \LogicException("Impossible to find token `$tokenName` declared in `{$matches[2]}`. Perhapse you forgot to add a custom token to Picea's engine ?");
+11 -3
View File
@@ -20,12 +20,16 @@ abstract class Context {
public string $filePath = ""; public string $filePath = "";
public string $classPath = "";
public array $switchStack = []; public array $switchStack = [];
public array $iterateStack = []; public array $iterationStack = [];
public array $useStack = []; public array $useStack = [];
public array $sections = [];
public int $functions = 0; public int $functions = 0;
public array $functionStack = []; public array $functionStack = [];
@@ -88,10 +92,14 @@ abstract class Context {
} }
public function tokenName(string $token) : string
{
return explode('.', $token)[0];
}
public function tokenOptions(string $token, bool $export = false) : array|string public function tokenOptions(string $token, bool $export = false) : array|string
{ {
$options = explode('.', strtolower($token)); $options = array_slice(explode('.', strtolower($token)), 1);
array_shift($options);
return $export ? var_export($options, true) : $options; return $export ? var_export($options, true) : $options;
} }
+2 -1
View File
@@ -4,7 +4,8 @@ namespace Picea\ControlStructure;
abstract class AbstractLoop implements ControlStructure { abstract class AbstractLoop implements ControlStructure {
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
{
switch($token) { switch($token) {
case "while": case "while":
case "foreach": case "foreach":
+2 -1
View File
@@ -6,7 +6,8 @@ class BlockToken implements ControlStructure {
public array $token = [ "arguments", "block", "endblock", "define", "slot", "endslot", "using" ]; public array $token = [ "arguments", "block", "endblock", "define", "slot", "endslot", "using" ];
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
{
static $slotDefinitions = []; static $slotDefinitions = [];
switch($token) { switch($token) {
+2 -1
View File
@@ -6,7 +6,8 @@ class BreakToken implements ControlStructure {
public string $token = "break"; public string $token = "break";
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 break; ?>"; return "<?php break; ?>";
} }
+2 -1
View File
@@ -6,7 +6,8 @@ class CaseToken implements ControlStructure {
public string $token = "case"; public string $token = "case";
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
{
$output = ""; $output = "";
if ( $context->switchStack ) { if ( $context->switchStack ) {
+1 -1
View File
@@ -6,7 +6,7 @@ class ContinueToken implements ControlStructure {
public string $token = "continue"; public string $token = "continue";
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 continue; ?>"; return "<?php continue; ?>";
} }
+1 -1
View File
@@ -6,7 +6,7 @@ class DefaultToken implements ControlStructure {
public string $token = "default"; public string $token = "default";
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 {
$output = ""; $output = "";
if ( $context->switchStack ) { if ( $context->switchStack ) {
+1 -1
View File
@@ -6,7 +6,7 @@ class EchoToken implements ControlStructure {
public array $token = [ "echo", "echo.raw" ]; public array $token = [ "echo", "echo.raw" ];
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 {
switch($token) { switch($token) {
case "echo": case "echo":
return "<?php \\Picea\\ControlStructure\\EchoToken::echoSafe($arguments) ?>"; return "<?php \\Picea\\ControlStructure\\EchoToken::echoSafe($arguments) ?>";
+1 -1
View File
@@ -6,7 +6,7 @@ class ForToken implements ControlStructure {
public array $token = [ "for", "endfor" ]; public array $token = [ "for", "endfor" ];
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 {
switch($token) { switch($token) {
case "for": case "for":
$uid = "$".uniqid("for_"); $uid = "$".uniqid("for_");
+1 -1
View File
@@ -6,7 +6,7 @@ class FunctionToken implements ControlStructure {
public array $token = [ "function", "endfunction", "return" ]; public array $token = [ "function", "endfunction", "return" ];
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 {
switch($token) { switch($token) {
case "function": case "function":
$context->functions++; $context->functions++;
+1 -1
View File
@@ -6,7 +6,7 @@ class IfToken implements ControlStructure {
public array $token = [ "if", "else", "elseif", "endif" ]; public array $token = [ "if", "else", "elseif", "endif" ];
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 {
switch($token) { switch($token) {
case "if": case "if":
return "<?php if ($arguments): ?>"; return "<?php if ($arguments): ?>";
+1 -1
View File
@@ -6,7 +6,7 @@ class NamespaceToken implements ControlStructure {
public string $token = "namespace"; public string $token = "namespace";
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 {
$context->namespace = $arguments; $context->namespace = $arguments;
return ""; return "";
} }
+1 -1
View File
@@ -6,7 +6,7 @@ class OrToken implements ControlStructure {
public string $token = "or"; public string $token = "or";
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 {
if ( empty($context->iterationStack) ) { if ( empty($context->iterationStack) ) {
throw new \LogicException("Token `or` was used outside of iterator. Make sure your `for`, `foreach`, `while`, `do/until` declaration are properly made."); throw new \LogicException("Token `or` was used outside of iterator. Make sure your `for`, `foreach`, `while`, `do/until` declaration are properly made.");
} }
+16 -11
View File
@@ -2,19 +2,26 @@
namespace Picea\ControlStructure; namespace Picea\ControlStructure;
enum PrintActionEnum : string {
case prepend = "prepend";
case default = "default";
case append = "append";
# case both = "both";
}
class SectionToken implements ControlStructure { class SectionToken implements ControlStructure {
public array $token = [ "section", "endsection" ]; public array $token = [ "section", "endsection" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) protected PrintActionEnum $action = PrintActionEnum::default;
{
$opt = $context->tokenOptions($token);
if (in_array('prepend', $opt)) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
$context->sectionAction = "prepend"; {
if (in_array('prepend', $options)) {
$this->action = PrintActionEnum::prepend;
} }
elseif (in_array('append', $opt)) { elseif (in_array('append', $options)) {
$context->sectionAction = "append"; $this->action = PrintActionEnum::append;
} }
switch($token) { switch($token) {
@@ -30,7 +37,7 @@ class SectionToken implements ControlStructure {
} }
} }
protected function printSection($context, ?string $arguments) : string protected function printSection($context, ? string $arguments) : string
{ {
list($name, $options) = array_pad(explode(',', $arguments, 2), 2, null); list($name, $options) = array_pad(explode(',', $arguments, 2), 2, null);
@@ -47,9 +54,7 @@ class SectionToken implements ControlStructure {
'options' => $options, 'options' => $options,
]; ];
$action = $options['action'] ?? $context->sectionAction ?? "default"; $action = $options['action'] ?? $this->action->value;
unset($context->sectionAction);
if (! in_array($action, ['prepend', 'append', 'default'])) { if (! in_array($action, ['prepend', 'append', 'default'])) {
throw new \RuntimeException("An unsupported action `$action` was given as an option of a {% section %} tag"); throw new \RuntimeException("An unsupported action `$action` was given as an option of a {% section %} tag");
+1 -1
View File
@@ -6,7 +6,7 @@ class SwitchToken implements ControlStructure {
public array $token = [ "switch", "case", "endswitch" ]; public array $token = [ "switch", "case", "endswitch" ];
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 {
switch($token) { switch($token) {
case "switch": case "switch":
$context->switchStack[] = true; $context->switchStack[] = true;
+1 -1
View File
@@ -6,7 +6,7 @@ class UseToken implements ControlStructure {
public string $token = "use"; public string $token = "use";
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 {
$context->useStack[] = $arguments; $context->useStack[] = $arguments;
return ""; return "";
} }
+1 -1
View File
@@ -6,7 +6,7 @@ class ViewToken implements ControlStructure {
public string $token = "view"; public string $token = "view";
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 {
# The way this is ordered, if you provide a second arguments, being an array of variables, get_defined_vars() will not be pushed inside the view # The way this is ordered, if you provide a second arguments, being an array of variables, get_defined_vars() will not be pushed inside the view
return "<?php echo \$___class__template->picea->inlineHtml(\$this, $arguments, get_defined_vars()); ?>"; return "<?php echo \$___class__template->picea->inlineHtml(\$this, $arguments, get_defined_vars()); ?>";
} }
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Picea\ControlStructure;
class WhileToken extends AbstractLoop { class WhileToken extends AbstractLoop {
public array $token = [ "do", "while", "endwhile", ]; public array $token = [ "do", "while", "endwhile", ];
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 {
switch($token) { switch($token) {
case "do": case "do":
$context->iterationStack[] = [ $context->iterationStack[] = [
@@ -0,0 +1,7 @@
<?php
namespace Picea\Exception;
use Picea\Picea;
class ParseExtensionException extends \Exception {}
+1 -1
View File
@@ -3,5 +3,5 @@
namespace Picea\Extension; namespace Picea\Extension;
interface 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;
} }
+3 -5
View File
@@ -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 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; $flag = $this->flags;
if ( in_array('pretty', $opt) ) { if ( in_array('pretty', $options) ) {
$flag |= \JSON_PRETTY_PRINT; $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') ?>"; return "<?php echo htmlentities(json_encode($arguments, $flag), ENT_QUOTES, 'UTF-8') ?>";
} }
+3 -5
View File
@@ -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]) { switch(explode('.', $token)[0]) {
case "language": case "language":
$cls = $this::class; $cls = $this::class;
@@ -49,14 +47,14 @@ class LanguageExtension implements Extension, FunctionExtension {
PHP; PHP;
case "lang": case "lang":
if ( in_array('raw', $opt) ) { if ( in_array('raw', $options) ) {
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>"; 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) ?>"; return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>";
case "_": case "_":
if ( in_array('raw', $opt) ) { if ( in_array('raw', $options) ) {
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>";
} }
-42
View File
@@ -1,42 +0,0 @@
<?php
namespace Picea\Extension;
use Picea\Compiler\Context;
class MoneyExtension implements Extension, FunctionExtension {
public string $token = "money";
public string $title = "";
public string $locale;
public \NumberFormatter $formatter;
public function __construct() {
$this->locale = explode('.', \Locale::getDefault())[0];
$this->formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY);
}
public function exportFunctions(): array
{
return [
"money" => [ $this, 'money' ]
];
}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
return "<?php echo money($arguments) ?>";
}
public function money(float $money, ? string $currency = null) /* : string|false */
{
$this->formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, 'CAD');
$this->formatter->setPattern( str_replace('¤#','¤ #', $this->formatter->getPattern() ) );
return $this->formatter->formatCurrency($money, $currency ?? $this->formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE));
}
}
+67
View File
@@ -0,0 +1,67 @@
<?php
namespace Picea\Extension;
use Picea\Compiler\Context;
class NumberExtension implements Extension, FunctionExtension {
public string $locale;
public \NumberFormatter $formatter;
public function __construct(
public string|array $token = [ "money", "format", "format_zero" ],
public string $title = "",
) {
$this->locale = explode('.', \Locale::getDefault())[0];
}
public function exportFunctions(): array
{
return [
"money" => [ $this, 'money' ],
"format" => [ $this, 'format' ],
"format_zero" => [ $this, 'formatZero' ],
];
}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
switch($token) {
case "format":
return "<?php echo format($arguments) ?>";
case "format_zero":
return "<?php echo format_zero($arguments) ?>";
case "money":
return "<?php echo money($arguments) ?>";
}
return "";
}
public function format(int|float $num, int $decimals = 0, null|string $decimalSeparator = ".", null|string $thousandsSeparator = ",") : string
{
return number_format($num, $decimals, $decimalSeparator, $thousandsSeparator);
}
public function formatZero(int $num, int $length) : string
{
return sprintf("%0{$length}d", $num);
}
public function money(float $money, ? string $currency = null) /* : string|false */
{
$this->formatter ??= new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY);
$this->formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, 'CAD');
$this->formatter->setPattern( str_replace('¤#','¤ #', $this->formatter->getPattern() ) );
return $this->formatter->formatCurrency($money, $currency ?? $this->formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE));
}
}
+2 -1
View File
@@ -6,7 +6,8 @@ class PhpExtension implements Extension {
public array $token = [ "php" ]; 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 ?>"; return "<?php $arguments ?>";
} }
} }
+9 -10
View File
@@ -18,25 +18,24 @@ class PrintExtension implements Extension {
$this->encoding = ini_get("default_charset"); $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($token) { switch($token) {
case 'echo': case 'echo':
case 'print': case 'print':
if (in_array('raw', $options)) {
return "<?php echo $arguments ?>";
}
return "<?php echo htmlspecialchars((string) $arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>"; return "<?php echo htmlspecialchars((string) $arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
case 'echo.raw':
case "print.raw":
return "<?php echo $arguments ?>";
case 'printf': case 'printf':
return "<?php echo htmlspecialchars(sprintf((string) $arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>"; if (in_array('raw', $options)) {
case 'printf.raw':
return "<?php printf((string) $arguments) ?>"; return "<?php printf((string) $arguments) ?>";
} }
return "<?php echo htmlspecialchars(sprintf((string) $arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
}
} }
} }
+2 -1
View File
@@ -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 return <<<PHP
<?php <?php
if ( null !== \$title = title($arguments) ) { if ( null !== \$title = title($arguments) ) {
+33 -16
View File
@@ -16,23 +16,30 @@ PATTERN;
protected string $urlBase; protected string $urlBase;
protected string $appUrl;
protected string $assetToken; protected string $assetToken;
protected array $routes; protected array $routes;
protected array $routesTarget; protected array $routesTarget;
protected bool $forceSSL = false;
public array $tokens = [ "url" , "route", "asset", "slug" ]; public array $tokens = [ "url" , "route", "asset", "slug" ];
public function __construct(string $urlBase = "", string $assetToken = "") { #[\Deprecated]
protected bool $enforceExistingArguments = true;
public function __construct(string $urlBase = "", string $assetToken = "", string $appUrl = "", bool $forceSSL = false) {
$this->urlBase = trim($urlBase, "/"); $this->urlBase = trim($urlBase, "/");
$this->assetToken = $assetToken; $this->assetToken = $assetToken;
$this->appUrl = $appUrl;
$this->forceSSL = $forceSSL;
} }
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) { switch($token) {
case "asset": case "asset":
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>";
@@ -41,10 +48,10 @@ PATTERN;
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>";
case "url": case "url":
if ( in_array('parameters', $opt) ) { if ( in_array('parameters', $options) ) {
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>"; 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) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>";
} }
@@ -54,7 +61,7 @@ PATTERN;
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->slug($arguments) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->slug($arguments) ?>";
} }
return null; throw new \ParseExtensionException("Unknown token given $token in UrlExtension.");
} }
public function exportFunctions(): array public function exportFunctions(): array
@@ -168,7 +175,7 @@ PATTERN;
protected function scheme() : string protected function scheme() : string
{ {
return ( $this->isHttps() ? "https" : "http" ) . "://"; return ( $this->forceSSL || $this->isHttps() ? "https" : "http" ) . "://";
} }
protected function base() : string protected function base() : string
@@ -187,7 +194,9 @@ PATTERN;
protected function domain() : string protected function domain() : string
{ {
return strtolower($_SERVER['HTTP_HOST']); $port = $this->isDefaultPort() ? "" : ":" . $_SERVER['SERVER_PORT'];
return strtolower($this->appUrl ? $this->appUrl . $port : $_SERVER['HTTP_HOST']);
} }
protected function isDefaultPort() : bool protected function isDefaultPort() : bool
@@ -218,26 +227,34 @@ PATTERN;
$search = []; $search = [];
foreach($matches as $item) { foreach($matches as $item) {
if (strpos($item[1], "=") !== false) { $default = null;
$variable = $item[1];
# Handles default
if (strpos($variable, "=") !== false) {
list($variable, $default) = explode('=', $item[1]); list($variable, $default) = explode('=', $item[1]);
} }
elseif (strpos($item[1], ":") !== false) {
# Handles type
if (strpos($variable, ":") !== false) {
list($variable, ) = explode(':', $item[1]); list($variable, ) = explode(':', $item[1]);
} }
else {
$variable = $item[1];
}
if ( array_key_exists($variable, $arguments) ) { if ( array_key_exists($variable, $arguments) ) {
$value = $arguments[ $variable ]; $value = $arguments[ $variable ];
unset($arguments[ $variable ]); unset($arguments[ $variable ]);
} }
else { else {
$value = $default ?? ""; if ($default ?? false) {
$value = $default;
}
elseif ($this->enforceExistingArguments) {
throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments ( %s )", $route, $variable, json_encode($arguments)));
}
} }
$search[ $item[0] ] = $value; $search[ $item[0] ] = $value;
} }
$route = str_replace(array_keys($search), array_values($search), $route); $route = str_replace(array_keys($search), array_values($search), $route);
+1 -1
View File
@@ -24,7 +24,7 @@ class Request implements Extension, FunctionExtension {
$this->request = $request; $this->request = $request;
} }
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 { }
public function exportFunctions(): array public function exportFunctions(): array
{ {
+5 -5
View File
@@ -71,12 +71,12 @@ class Picea implements LanguageRegistration
return call_user_func($object); return call_user_func($object);
} }
catch(\Throwable $ex) { catch(\Throwable $ex) {
if (! $ex instanceof Exception\RenderHtmlException ) { #if (! $ex instanceof Exception\RenderHtmlException ) {
throw new Exception\RenderHtmlException($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex); # throw new Exception\RenderHtmlException($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex);
} #}
else { #else {
throw $ex; throw $ex;
} #}
} }
exit(); exit();