18 Commits
Author SHA1 Message Date
mcndave 215bc26981 - UrlExtension now supports multiple application URL 2023-05-31 18:29:36 +00:00
Dave Mc Nicoll 3dce6a41cb - Added a batch() function to ease array iterations in templates 2023-05-01 14:36:47 -04:00
mcndave f9c955a82a - Fixed port redirection for URL behind proxy 2023-04-28 19:57:52 +00:00
Dave Mc Nicoll 2fe97c60ed - Fixed a bad merge 2023-04-28 19:56:05 +00:00
Dave Mc Nicoll 710ecad98d - Adjusted route name on exception 2023-04-28 19:52:19 +00:00
mcndave 3bcd5c9780 Merge branch 'master' of https://git.mcnd.ca/mcndave/picea 2023-04-20 18:16:29 +00:00
mcndave 7a2fa872fa - Added a no_html() method 2023-04-20 18:16:20 +00:00
mcndave 5182ba19bc - Fixed optional arguments in buildUrl() 2023-04-19 13:23:29 -04:00
mcndave 57367968ca Merge branch 'master' of https://git.mcnd.ca/mcndave/picea 2023-04-13 15:50:23 -04:00
mcndave b1e1324bfa - Fixed some PHP 8.x bugs (missing vars within compiler's context) and removed Picea's error handler - needs some work before putting online 2023-04-13 15:49:45 -04:00
mcndave 44b0283938 - Merged with new code-base 2023-03-30 14:46:52 -04:00
mcndave 1611ebe047 Merge branch 'master' of https://git.mcnd.ca/mcndave/picea 2023-03-30 18:29:35 +00:00
mcndave 6ee3e1bbae - Forced parameters in URL to be filled in UrlExtension buildUrl() and disabled the custom exception for now... 2023-03-30 18:29:17 +00:00
mcndave 169400cd61 Merge branch 'master' of https://git.mcnd.ca/mcndave/picea 2023-03-29 15:13:54 +00:00
mcndave b4964402fd - Renamed MoneyExtension to NumberExtension 2023-03-29 15:13:41 +00:00
mcndave a65aa72733 - Fixed bug Creation of dynamic property Picea\Compiler\BaseContext:: 2023-03-29 14:02:11 +00:00
mcndave b3a8e3aca2 - Token name and options are now passed through parse() method 2023-02-02 19:38:40 +00:00
mcndave e9b2f0ece3 - Fixed a bug added in previous commit 2023-02-02 18:12:31 +00:00
35 changed files with 255 additions and 136 deletions

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@
"authors": [
{
"name": "Dave Mc Nicoll",
"email": "mcndave@gmail.com"
"email": "info@mcnd.ca"
}
],
"autoload": {
+3 -3
View File
@@ -10,7 +10,7 @@ use function DI\autowire, DI\create, DI\get;
use Laminas\Diactoros\Response\HtmlResponse;
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 };
return [
@@ -26,7 +26,7 @@ return [
return new Compiler(new class(array_merge([
$c->get(LanguageExtension::class),
$c->get(TitleExtension::class),
$c->get(MoneyExtension::class),
$c->get(NumberExtension::class),
$c->get(UrlExtension::class),
$c->get(Method\Form::class),
$c->get(Method\Pagination::class),
@@ -68,7 +68,7 @@ return [
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')),
+4 -3
View File
@@ -50,14 +50,15 @@ class Compiler
list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null);
$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
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 ) {
return $this->extensionList[$tokenName]->parse($context, $arguments, $token);
return $this->extensionList[$tokenName]->parse($context, $arguments, $tokenName, $tokenOptions);
}
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 ?");
+11 -3
View File
@@ -20,12 +20,16 @@ abstract class Context {
public string $filePath = "";
public string $classPath = "";
public array $switchStack = [];
public array $iterateStack = [];
public array $iterationStack = [];
public array $useStack = [];
public array $sections = [];
public int $functions = 0;
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
{
$options = explode('.', strtolower($token));
array_shift($options);
$options = array_slice(explode('.', strtolower($token)), 1);
return $export ? var_export($options, true) : $options;
}
+2 -1
View File
@@ -4,7 +4,8 @@ namespace Picea\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) {
case "while":
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 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 = [];
switch($token) {
+2 -1
View File
@@ -6,7 +6,8 @@ class BreakToken implements ControlStructure {
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; ?>";
}
+2 -1
View File
@@ -6,7 +6,8 @@ class CaseToken implements ControlStructure {
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 = "";
if ( $context->switchStack ) {
+1 -1
View File
@@ -6,7 +6,7 @@ class ContinueToken implements ControlStructure {
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; ?>";
}
+1 -1
View File
@@ -6,7 +6,7 @@ class DefaultToken implements ControlStructure {
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 = "";
if ( $context->switchStack ) {
+1 -1
View File
@@ -6,7 +6,7 @@ class EchoToken implements ControlStructure {
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) {
case "echo":
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 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) {
case "for":
$uid = "$".uniqid("for_");
+1 -1
View File
@@ -6,7 +6,7 @@ class FunctionToken implements ControlStructure {
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) {
case "function":
$context->functions++;
+1 -1
View File
@@ -6,7 +6,7 @@ class IfToken implements ControlStructure {
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) {
case "if":
return "<?php if ($arguments): ?>";
+1 -1
View File
@@ -6,7 +6,7 @@ class NamespaceToken implements ControlStructure {
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;
return "";
}
+1 -1
View File
@@ -6,7 +6,7 @@ class OrToken implements ControlStructure {
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) ) {
throw new \LogicException("Token `or` was used outside of iterator. Make sure your `for`, `foreach`, `while`, `do/until` declaration are properly made.");
}
+22 -20
View File
@@ -2,19 +2,26 @@
namespace Picea\ControlStructure;
enum PrintActionEnum : string {
case prepend = "prepend";
case default = "default";
case append = "append";
# case both = "both";
}
class SectionToken implements ControlStructure {
public array $token = [ "section", "endsection" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token)
{
$opt = $context->tokenOptions($token);
protected PrintActionEnum $action = PrintActionEnum::default;
if (in_array('prepend', $opt)) {
$context->sectionAction = "prepend";
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
if (in_array('prepend', $options)) {
$this->action = PrintActionEnum::prepend;
}
elseif (in_array('append', $opt)) {
$context->sectionAction = "append";
elseif (in_array('append', $options)) {
$this->action = PrintActionEnum::append;
}
switch($token) {
@@ -47,9 +54,7 @@ class SectionToken implements ControlStructure {
'options' => $options,
];
$action = $options['action'] ?? $context->sectionAction ?? "default";
unset($context->sectionAction);
$action = $options['action'] ?? $this->action->value;
if (! in_array($action, ['prepend', 'append', 'default'])) {
throw new \RuntimeException("An unsupported action `$action` was given as an option of a {% section %} tag");
@@ -61,9 +66,9 @@ class SectionToken implements ControlStructure {
"\$___class__template->sectionList[$name]['$action'][] = [
'order' => $order,
'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables, \$__event) {" .
"extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE);
\$___class__template->sectionStack[] = $name;
\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSection::class, $name);?>";
"extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE);" .
"\$___class__template->sectionStack[] = $name;" .
"\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSection::class, $name);?>";
}
protected function printEndSection($context) : string
@@ -71,13 +76,10 @@ class SectionToken implements ControlStructure {
$section = array_pop($context->sections);
$build = $context->extendFrom ? "!empty(\$___class__template->sectionStack) && \$___class__template->renderSection({$section['name']}, false);" : "\$___class__template->renderSection({$section['name']}, false);";
return <<<PHP
<?php
\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSectionDone::class, {$section['name']});
array_pop(\$___class__template->sectionStack); }];
$build
?>
PHP;
return "<?php \$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSectionDone::class, {$section['name']});" .
"array_pop(\$___class__template->sectionStack); }];" .
$build .
"?>";
}
}
+1 -1
View File
@@ -6,7 +6,7 @@ class SwitchToken implements ControlStructure {
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) {
case "switch":
$context->switchStack[] = true;
+1 -1
View File
@@ -6,7 +6,7 @@ class UseToken implements ControlStructure {
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;
return "";
}
+1 -1
View File
@@ -6,7 +6,7 @@ class ViewToken implements ControlStructure {
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
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 {
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) {
case "do":
$context->iterationStack[] = [
@@ -0,0 +1,7 @@
<?php
namespace Picea\Exception;
use Picea\Picea;
class ParseExtensionException extends \Exception {}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace Picea\Extension;
use Picea\Compiler\Context;
class BatchExtension implements Extension, FunctionExtension {
public string $token = "batch";
public function exportFunctions(): array
{
return [
"batch" => [ $this, 'batch' ]
];
}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
return "<?php echo batch($arguments) ?>";
}
public function batch(iterable $array, int $size, mixed $default = null) : \Generator
{
$split = ceil( count($array) / $size );
for($i = 0; $i < $split; $i++) {
$slice = array_slice(is_array($array) ? $array : iterator_to_array($array, true), $i * $size, $size, true);
$pad = ( count($slice) !== $size ) && ( $default !== null );
yield $pad ? array_pad($slice, $size, $default) : $slice;
}
}
}
+1 -1
View File
@@ -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;
}
+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 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') ?>";
}
+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]) {
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) ?>";
}
-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 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 ?>";
}
}
+14 -11
View File
@@ -6,7 +6,7 @@ use Picea\Compiler\Context;
class PrintExtension implements Extension {
public array $token = [ "echo", "print", "printf" ];
public array $token = [ "echo", "print", "printf", "no_html" ];
public int $flag = \ENT_QUOTES;
@@ -18,25 +18,28 @@ 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($token) {
case 'echo':
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") . ") ?>";
case 'echo.raw':
case "print.raw":
return "<?php echo $arguments ?>";
case 'printf':
return "<?php echo htmlspecialchars(sprintf((string) $arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
case 'printf.raw':
if (in_array('raw', $options)) {
return "<?php printf((string) $arguments) ?>";
}
return "<?php echo htmlspecialchars(sprintf((string) $arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
case 'no_html':
return "<?php echo nl2br(strip_tags((string) $arguments)) ?>";
}
}
}
+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
<?php
if ( null !== \$title = title($arguments) ) {
+55 -18
View File
@@ -2,6 +2,7 @@
namespace Picea\Extension;
use Notes\Route\Attribute\Object\Route;
use Picea\Compiler\Context;
class UrlExtension implements Extension, FunctionExtension {
@@ -16,23 +17,30 @@ PATTERN;
protected string $urlBase;
protected array $appUrl;
protected string $assetToken;
protected array $routes;
protected array $routesTarget;
protected bool $forceSSL = false;
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 = "", array $appUrl = [], bool $forceSSL = false) {
$this->urlBase = trim($urlBase, "/");
$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) {
case "asset":
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>";
@@ -41,10 +49,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 +62,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
@@ -108,7 +116,7 @@ PATTERN;
public function buildRouteUrl(string $name, array $parameters = [], bool $appendVersion = false) : string
{
if ( false !== ( $route = $this->routes[$name] ?? false ) ) {
return $this->buildUrl($this->prepareRoute($route['route'], $parameters), $parameters, $appendVersion);
return $this->buildUrl($this->prepareRoute($route, $parameters), $parameters, $appendVersion);
}
$routeList = json_encode($this->routes, \JSON_PRETTY_PRINT);
@@ -168,7 +176,7 @@ PATTERN;
protected function scheme() : string
{
return ( $this->isHttps() ? "https" : "http" ) . "://";
return ( $this->forceSSL || $this->isHttps() ? "https" : "http" ) . "://";
}
protected function base() : string
@@ -187,7 +195,25 @@ PATTERN;
protected function domain() : string
{
return strtolower($_SERVER['HTTP_HOST']);
if ( ! empty($_SERVER['HTTP_X_FORWARDED_PROTO']) || ! empty($_SERVER['HTTP_X_FORWARDED_SSL']) ) {
$port = "";
}
else {
$port = $this->isDefaultPort() ? "" : ":" . $_SERVER['SERVER_PORT'];
}
return strtolower($this->appUrl($port) ?: $_SERVER['HTTP_HOST']);
}
protected function appUrl(string $port) : string
{
$domain = strtolower($_SERVER['HTTP_HOST']);
if (in_array($domain, $this->appUrl)) {
return $domain . $port;
}
return $this->appUrl[0] . $port;
}
protected function isDefaultPort() : bool
@@ -212,31 +238,42 @@ PATTERN;
|| ( "off" !== ( strtolower($_SERVER['HTTPS'] ?? $_SERVER['HTTP_X_FORWARDED_SSL'] ?? $_SERVER['X-Forwarded-Ssl'] ?? "off")) );
}
protected function prepareRoute(string $route, array &$arguments)
protected function prepareRoute(array $routeParam, array &$arguments)
{
$route = $routeParam['route'];
if ( preg_match_all('~{(.*?)}~si', $route, $matches, PREG_SET_ORDER) ) {
$search = [];
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]);
}
elseif (strpos($item[1], ":") !== false) {
# Handles type
if (strpos($variable, ":") !== false) {
list($variable, ) = explode(':', $item[1]);
}
else {
$variable = $item[1];
}
if ( array_key_exists($variable, $arguments) ) {
$value = $arguments[ $variable ];
unset($arguments[ $variable ]);
}
else {
$value = $default ?? "";
if ($default ?? false) {
$value = $default;
}
elseif ($this->enforceExistingArguments) {dump($routeParam);
throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments ( %s ) from %s::%s", $route, $variable, json_encode($arguments), $routeParam['class'], $routeParam['classMethod']));
}
}
$search[ $item[0] ] = $value;
$search[$item[0]] = $value ?? "";
}
+1
View File
@@ -67,6 +67,7 @@ class DefaultRegistrations implements LanguageRegistration
$compiler->registerExtension(new \Picea\Extension\PhpExtension());
$compiler->registerExtension(new \Picea\Extension\PrintExtension());
$compiler->registerExtension(new \Picea\Extension\JsonExtension());
$compiler->registerExtension(new \Picea\Extension\BatchExtension());
foreach($this->extensions ?? [] as $extension) {
$compiler->registerExtension($extension);
+1 -1
View File
@@ -24,7 +24,7 @@ class Request implements Extension, FunctionExtension {
$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
{
+5 -5
View File
@@ -71,12 +71,12 @@ class Picea implements LanguageRegistration
return call_user_func($object);
}
catch(\Throwable $ex) {
if (! $ex instanceof Exception\RenderHtmlException ) {
throw new Exception\RenderHtmlException($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex);
}
else {
#if (! $ex instanceof Exception\RenderHtmlException ) {
# throw new Exception\RenderHtmlException($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex);
#}
#else {
throw $ex;
}
#}
}
exit();