8 Commits
33 changed files with 184 additions and 122 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 ?");
+8 -2
View File
@@ -20,6 +20,8 @@ abstract class Context {
public string $filePath = "";
public string $classPath = "";
public array $switchStack = [];
public array $iterateStack = [];
@@ -88,10 +90,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.");
}
+15 -10
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");
+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 {}
+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 ?>";
}
}
+9 -10
View File
@@ -18,25 +18,24 @@ 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") . ") ?>";
}
}
}
+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) ) {
+33 -16
View File
@@ -16,23 +16,30 @@ PATTERN;
protected string $urlBase;
protected string $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 = "", string $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 +48,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 +61,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
@@ -168,7 +175,7 @@ PATTERN;
protected function scheme() : string
{
return ( $this->isHttps() ? "https" : "http" ) . "://";
return ( $this->forceSSL || $this->isHttps() ? "https" : "http" ) . "://";
}
protected function base() : string
@@ -187,7 +194,9 @@ PATTERN;
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
@@ -218,26 +227,34 @@ PATTERN;
$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) {
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;
}
$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;
}
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();