Compare commits
22
Commits
3bcd5c9780
...
v2.x
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
200b58bdf5 | ||
|
|
5e54407f74 | ||
|
|
b856743741 | ||
|
|
10fb17c7c3 | ||
|
|
8b6dd85fd5 | ||
|
|
8c7a4d730b | ||
|
|
ed0eb8225b | ||
|
|
0e655fd387 | ||
|
|
ce43abb954 | ||
|
|
5aa70dfd17 | ||
|
|
043fb90f8d | ||
|
|
d5de5e665b | ||
|
|
3ccbf6bfab | ||
|
|
42d84796ee | ||
|
|
386e0aa448 | ||
|
|
be65d45b41 | ||
|
|
7d2bb8d407 | ||
|
|
215bc26981 | ||
|
|
3dce6a41cb | ||
|
|
f9c955a82a | ||
|
|
2fe97c60ed | ||
|
|
710ecad98d |
No files matched your search
@@ -16,5 +16,14 @@
|
||||
},
|
||||
"require": {
|
||||
"psr/http-message": "^1.0"
|
||||
},
|
||||
"extra": {
|
||||
"lean": {
|
||||
"autoload": {
|
||||
"definitions": [
|
||||
"meta/definitions.php"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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, NumberExtension, UrlExtension };
|
||||
use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension };
|
||||
use Picea\Ui\{ Method, Ui };
|
||||
|
||||
return [
|
||||
@@ -49,10 +49,10 @@ return [
|
||||
|
||||
Method\Pagination::class => autowire(Method\Pagination::class),
|
||||
|
||||
LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandler::class)),
|
||||
LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandlerInterface::class)),
|
||||
|
||||
LanguageHandler::class => function($c) {
|
||||
return new class( $c->get(Tell\I18n::class) ) implements LanguageHandler {
|
||||
LanguageHandlerInterface::class => function($c) {
|
||||
return new class( $c->get(Tell\I18n::class) ) implements LanguageHandlerInterface {
|
||||
public Tell\I18n $tell;
|
||||
|
||||
public function __construct(Tell\I18n $tell) {
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
use function DI\autowire, DI\create, DI\get;
|
||||
|
||||
use Picea\{
|
||||
Caching\Cache,
|
||||
Caching\Opcache,
|
||||
Compiler,
|
||||
Compiler\Context,
|
||||
Compiler\BaseContext,
|
||||
FileFetcher,
|
||||
Method\Request
|
||||
};
|
||||
|
||||
use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension };
|
||||
|
||||
return [
|
||||
Picea\Picea::class => function($c) {
|
||||
return new Picea\Picea($c->get(Context::class), $c->get(Cache::class), $c->get(Compiler::class), null, $c->get(FileFetcher::class), null, getenv("DEBUG"));
|
||||
},
|
||||
|
||||
Context::class => function($c) {
|
||||
return new BaseContext($c->get(Lean\Lean::class)->getPiceaContext());
|
||||
},
|
||||
|
||||
Compiler::class => autowire(Compiler::class),
|
||||
|
||||
Request::class => autowire(Request::class),
|
||||
|
||||
LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandlerInterface::class)),
|
||||
|
||||
# LanguageHandlerInterface::class => autowire(\Lean\LanguageHandler::class),
|
||||
|
||||
# LanguageRegistration::class => create(\Lean\PiceaDefaultRegistration::class)->constructor(get('picea.extensions'), [], [], get(Ui::class), null),
|
||||
|
||||
'picea.extensions' => function(\Psr\Container\ContainerInterface $c) {
|
||||
return array_merge([
|
||||
$c->get(LanguageExtension::class),
|
||||
$c->get(TitleExtension::class),
|
||||
$c->get(NumberExtension::class),
|
||||
$c->get(UrlExtension::class),
|
||||
$c->get(Request::class),
|
||||
], class_exists(\Taxus\Picea\Extension::class) ? [ $c->get(\Taxus\Picea\Extension::class) ] : [],
|
||||
array_map(fn($class) => $c->get($class), $c->get(Lean\Lean::class)->getPiceaExtensions())
|
||||
);
|
||||
},
|
||||
|
||||
TitleExtension::class => autowire(TitleExtension::class),
|
||||
|
||||
NumberExtension::class => autowire(NumberExtension::class),
|
||||
|
||||
UrlExtension::class => create(UrlExtension::class)->constructor(getenv("URL_BASE"), get('git.commit'), explode(',', getenv('APP_URL')), (bool) getenv('FORCE_SSL')),
|
||||
|
||||
Cache::class => create(Opcache::class)->constructor(getenv("CACHE_PATH"), get(Context::class)),
|
||||
|
||||
FileFetcher::class => function($c) {
|
||||
return new FileFetcher($c->get(Lean\Lean::class)->getViewPaths());
|
||||
},
|
||||
];
|
||||
@@ -50,15 +50,21 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
||||
|
||||
$__event->eventExecute(\Picea\Event\Builder\ClassTemplateOutputing::class, $variablesList);
|
||||
|
||||
try {
|
||||
( function($___class__template, $___global_variables, $___variables, $__event, $picea) {
|
||||
extract($___global_variables);
|
||||
extract($___variables, \EXTR_OVERWRITE);
|
||||
?>%CONTENT%<?php
|
||||
} )->call($this->thisProxy ?? new class() {}, $this, $this->variableList, $variablesList, $__event, $this->picea);
|
||||
} catch (\Throwable $error) {
|
||||
throw $this->errorHandler($error);
|
||||
}
|
||||
|
||||
$__event->eventExecute(\Picea\Event\Builder\ClassTemplateOutputDone::class, $variablesList);
|
||||
|
||||
%PARENT_OUTPUT%
|
||||
try { %PARENT_OUTPUT% } catch (\Throwable $error) {
|
||||
throw $this->errorHandler($error);
|
||||
}
|
||||
|
||||
$this->depth--;
|
||||
|
||||
@@ -138,6 +144,32 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
||||
$this->output($variablesList);
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
public static function isTemplateError(\Throwable $error) : bool
|
||||
{
|
||||
$class = substr(static::class, strripos(static::class, '\\') + 1) . ".php";
|
||||
|
||||
return substr($error->getFile(), -strlen($class)) === $class;
|
||||
}
|
||||
|
||||
protected function errorHandler(\Throwable $error) : \Throwable
|
||||
{
|
||||
$class = substr(static::class, strrpos(static::class, '\\') + 1) . ".php";
|
||||
|
||||
#dump( __NAMESPACE__, basename($error->getFile()), $class );
|
||||
$basename = basename($error->getFile());
|
||||
|
||||
#dump(is_subclass_of(sprintf("\\%s\\%s", __NAMESPACE__, substr($basename,0 , strrpos($basename, '.'))), static::class, true));
|
||||
|
||||
if (str_ends_with($error->getFile(), $class)) {
|
||||
$error = new \Picea\Exception\RenderHtmlException($this, $error->getMessage(), $error->getCode(), $error);
|
||||
}
|
||||
#elseif (is_subclass_of(sprintf("\\%s\\%s", __NAMESPACE__, substr($basename,0 , strrpos($basename, '.'))), static::class, true)) {
|
||||
# $error = new \Picea\Exception\RenderHtmlException($this, $error->getMessage(), $error->getCode(), $error);
|
||||
#}
|
||||
|
||||
return $error;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@ class Opcache implements Cache {
|
||||
|
||||
protected array $compiled = [];
|
||||
|
||||
protected ? Context $context;
|
||||
protected null|Context $context;
|
||||
|
||||
public function __construct(?string $cachePath = null, ? Context $context)
|
||||
public function __construct(?string $cachePath = null, null|Context $context = null)
|
||||
{
|
||||
$this->cachePath = $cachePath ?? sys_get_temp_dir();
|
||||
$this->context = $context;
|
||||
@@ -34,7 +34,7 @@ class Opcache implements Cache {
|
||||
|
||||
$compiledContext = $this->compiled[$viewPath];
|
||||
|
||||
$fullName = isset($compiledContext['namespace']) ? "\\{$compiledContext['namespace']}\\{$compiledContext['classname']}" : $compiledContext['classname'];
|
||||
$fullName = isset($compiledContext['namespace']) ? "\\{$compiledContext['namespace']}\\{$compiledContext['classname']}" : ($compiledContext['classname'] ?? $viewPath);
|
||||
|
||||
return new $fullName(...$arguments);
|
||||
}
|
||||
|
||||
+13
-6
@@ -2,12 +2,17 @@
|
||||
|
||||
namespace Picea;
|
||||
|
||||
use Picea\Event\Compiler\CompileTokenExtension;
|
||||
use Picea\Event\Compiler\CompileTokenExtensionEvent;
|
||||
use Picea\Event\Compiler\CompileTokenTagEvent;
|
||||
use Picea\Exception\RegisterExtensionToken;
|
||||
use Picea\Extension\FunctionExtension;
|
||||
use Picea\Language\LanguageRegistration;
|
||||
|
||||
class Compiler
|
||||
{
|
||||
use EventTrait;
|
||||
|
||||
protected string $sourceCode = "";
|
||||
|
||||
protected array $syntaxObjectList = [];
|
||||
@@ -20,11 +25,10 @@ class Compiler
|
||||
|
||||
public array $extensionList = [];
|
||||
|
||||
public ? LanguageRegistration $languageRegistration;
|
||||
|
||||
public function __construct(?LanguageRegistration $languageRegistration = null)
|
||||
public function __construct(
|
||||
public LanguageRegistration $languageRegistration
|
||||
)
|
||||
{
|
||||
$this->languageRegistration = $languageRegistration;
|
||||
$this->languageRegistration->registerAll($this);
|
||||
}
|
||||
|
||||
@@ -53,11 +57,14 @@ class Compiler
|
||||
$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 ) {
|
||||
$this->eventExecute(CompileTokenTagEvent::class, $context, $arguments, $tokenName, $tokenOptions);
|
||||
|
||||
return $this->tagList[$tokenName]->parse($context, $arguments, $tokenName, $tokenOptions);
|
||||
}
|
||||
elseif ( $this->extensionList[$tokenName] ?? false ) {
|
||||
$this->eventExecute(CompileTokenExtensionEvent::class, $context, $arguments, $tokenName, $tokenOptions);
|
||||
|
||||
return $this->extensionList[$tokenName]->parse($context, $arguments, $tokenName, $tokenOptions);
|
||||
}
|
||||
else {
|
||||
@@ -135,7 +142,7 @@ class Compiler
|
||||
|
||||
public function __toString() : string
|
||||
{
|
||||
return "WHATAFAK";
|
||||
return "???";
|
||||
}
|
||||
|
||||
public function getExtensionFromToken(string $name) : Extension\Extension
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Picea\Compiler;
|
||||
|
||||
class BaseContext extends Context {
|
||||
|
||||
public function __construct(?string $defaultNamespace = null) {
|
||||
public function __construct(array|string|null $defaultNamespace = null) {
|
||||
if ( $defaultNamespace !== null ) {
|
||||
$this->namespace = $defaultNamespace;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use Picea\Compiler;
|
||||
|
||||
abstract class Context {
|
||||
|
||||
public string $namespace = "";
|
||||
public string|array $namespace = "";
|
||||
|
||||
public string $extendFrom = "";
|
||||
|
||||
|
||||
@@ -33,6 +33,8 @@ class SectionToken implements ControlStructure {
|
||||
throw new \RuntimeException("A section closing tag {% endsection %} was found without an opening {% section %} tag");
|
||||
}
|
||||
|
||||
$this->action = PrintActionEnum::default;
|
||||
|
||||
return $this->printEndSection($context);
|
||||
}
|
||||
}
|
||||
@@ -62,13 +64,13 @@ class SectionToken implements ControlStructure {
|
||||
|
||||
$order = $options['order'] ?? "count(\$___class__template->sectionList[$name]['$action'])";
|
||||
|
||||
return "<?php \$___class__template->sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];".
|
||||
"\$___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);?>";
|
||||
return "<?php \$___class__template->sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];" .
|
||||
"\$___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);?>";
|
||||
}
|
||||
|
||||
protected function printEndSection($context) : string
|
||||
@@ -76,13 +78,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 .
|
||||
"?>";
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Event\Compiler;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
interface CompileTokenExtensionEvent
|
||||
{
|
||||
public function execute(Context $context, ?string $arguments, string $token, array $options = []);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Event\Compiler;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
interface CompileTokenTagEvent
|
||||
{
|
||||
public function execute(Context &$context, ?string $arguments, string $token, array $options = []);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Event\Extension;
|
||||
|
||||
interface BuildAssetUrl
|
||||
{
|
||||
public function execute(string $uri, array $parameters = [], bool $appendVersion = true) : mixed;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Event\Extension;
|
||||
|
||||
interface UrlBuildAssetEvent
|
||||
{
|
||||
public function execute(string $uri, array $parameters = [], bool $appendVersion) : void;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Event\Extension;
|
||||
|
||||
interface UrlBuildRouteEvent
|
||||
{
|
||||
public function execute(string $uri, array $parameters = [], bool $appendVersion) : void;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Event\Extension;
|
||||
|
||||
interface UrlBuildUrlEvent
|
||||
{
|
||||
public function execute(string $uri, array $parameters = [], bool $appendVersion) : void;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Event\Extension;
|
||||
|
||||
interface UrlRegisterRouteEvent
|
||||
{
|
||||
public function execute(string $name, array $route) : void;
|
||||
}
|
||||
+6
-6
@@ -4,26 +4,26 @@ namespace Picea;
|
||||
|
||||
trait EventTrait
|
||||
{
|
||||
protected $_eventTraitMethod = "execute";
|
||||
protected $eventTraitMethod = "execute";
|
||||
|
||||
public array $_eventList = [];
|
||||
public array $eventTraitList = [];
|
||||
|
||||
protected array $_returnList = [];
|
||||
protected array $eventTraitReturnList = [];
|
||||
|
||||
public function eventRegister(object $event) : void
|
||||
{
|
||||
$this->_eventList[] = $event;
|
||||
$this->eventTraitList[] = $event;
|
||||
}
|
||||
|
||||
public function eventFromType(string $type) : array
|
||||
{
|
||||
return array_filter($this->_eventList, fn($ev) => $ev instanceof $type);
|
||||
return array_filter($this->eventTraitList, fn($ev) => $ev instanceof $type);
|
||||
}
|
||||
|
||||
public function eventExecute(string $type, ...$arguments) : void
|
||||
{
|
||||
foreach($this->eventFromType($type) as $event) {
|
||||
$this->_returnList[$event::class][] = call_user_func_array([ $event, $this->_eventTraitMethod ], $arguments);
|
||||
$this->eventTraitReturnList[$event::class][] = call_user_func_array([ $event, $this->eventTraitMethod ], $arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,33 +10,20 @@ use Picea\Picea;
|
||||
*/
|
||||
class RenderHtmlException extends \Exception
|
||||
{
|
||||
protected Picea $picea;
|
||||
|
||||
public function __construct(object $compiledObject, Picea $picea, string $message, int $code, \Throwable $previous)
|
||||
public function __construct(object $template, string $message, int $code, \Throwable $previous)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
|
||||
$this->picea = $picea;
|
||||
|
||||
$this->defineError($previous, $compiledObject);
|
||||
$this->defineError($previous, $template);
|
||||
}
|
||||
|
||||
protected function defineError(\Throwable $previous, object $compiledObject) : void
|
||||
protected function defineError(\Throwable $previous, object $template) : void
|
||||
{
|
||||
$loadedTemplates = array_flip($this->picea->loadedTemplateFile);
|
||||
/*$loadedTemplates = array_flip($this->picea->loadedTemplateFile);*/
|
||||
|
||||
foreach($previous->getTrace() as $trace) {
|
||||
if ( isset($trace['file'], $loadedTemplates[$trace['file']]) ) {
|
||||
$class = $loadedTemplates[ $trace['file'] ];
|
||||
|
||||
$content = include($trace['file']);
|
||||
|
||||
$this->file = $content['view'];
|
||||
$this->line = $class::getSourceLineFromException($trace['line']);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
$this->file = $template::getParam('view');
|
||||
$this->line = $template::getSourceLineFromException($previous->getLine());
|
||||
}
|
||||
|
||||
protected function getTemplateFile(string $filePath) : ? array
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,9 @@ class LanguageExtension implements Extension, FunctionExtension {
|
||||
|
||||
public string $currentLanguage = "";
|
||||
|
||||
protected LanguageHandler $languageHandler;
|
||||
protected LanguageHandlerInterface $languageHandler;
|
||||
|
||||
public function __construct(LanguageHandler $handler) {
|
||||
public function __construct(LanguageHandlerInterface $handler) {
|
||||
$this->languageHandler = $handler;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Picea\Extension;
|
||||
|
||||
interface LanguageHandler
|
||||
interface LanguageHandlerInterface
|
||||
{
|
||||
public function languageFromKey(string $key, array $variables = []); #: array|string;
|
||||
}
|
||||
@@ -15,6 +15,7 @@ class NumberExtension implements Extension, FunctionExtension {
|
||||
public string $title = "",
|
||||
) {
|
||||
$this->locale = explode('.', \Locale::getDefault())[0];
|
||||
$this->formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY);
|
||||
}
|
||||
|
||||
public function exportFunctions(): array
|
||||
@@ -55,8 +56,6 @@ class NumberExtension implements Extension, FunctionExtension {
|
||||
|
||||
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() ) );
|
||||
|
||||
@@ -8,7 +8,7 @@ class PrintExtension implements Extension {
|
||||
|
||||
public array $token = [ "echo", "print", "printf", "no_html" ];
|
||||
|
||||
public int $flag = \ENT_QUOTES;
|
||||
public int $flag = \ENT_QUOTES | \ENT_DISALLOWED | \ENT_SUBSTITUTE;
|
||||
|
||||
public string $encoding;
|
||||
|
||||
|
||||
@@ -17,13 +17,10 @@ class TitleExtension implements Extension, FunctionExtension {
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
return <<<PHP
|
||||
<?php
|
||||
if ( null !== \$title = title($arguments) ) {
|
||||
echo \$title;
|
||||
}
|
||||
?>
|
||||
PHP;
|
||||
return "<?php ".
|
||||
"if ( null !== \$title = title($arguments) ) {".
|
||||
"echo \$title;".
|
||||
"} ?>";
|
||||
}
|
||||
|
||||
public function handleTitle(? string $set = null, ...$arguments) : ? string
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace Picea\Extension;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
use Picea\EventTrait;
|
||||
use Picea\Event\Extension\{ UrlBuildAssetEvent, UrlBuildUrlEvent, UrlBuildRouteEvent, UrlRegisterRouteEvent };
|
||||
|
||||
class UrlExtension implements Extension, FunctionExtension {
|
||||
use EventTrait;
|
||||
|
||||
public const URLIZE_PATTERN_URL = <<<PATTERN
|
||||
~(?<!href=['"])https?://[\w/._\-&?]*(?!</a>)(?=[^\w/._\-&])~s
|
||||
@@ -16,7 +18,7 @@ PATTERN;
|
||||
|
||||
protected string $urlBase;
|
||||
|
||||
protected string $appUrl;
|
||||
protected array $appUrl;
|
||||
|
||||
protected string $assetToken;
|
||||
|
||||
@@ -31,7 +33,7 @@ PATTERN;
|
||||
#[\Deprecated]
|
||||
protected bool $enforceExistingArguments = true;
|
||||
|
||||
public function __construct(string $urlBase = "", string $assetToken = "", string $appUrl = "", bool $forceSSL = false) {
|
||||
public function __construct(string $urlBase = "", string $assetToken = "", array $appUrl = [], bool $forceSSL = false) {
|
||||
$this->urlBase = trim($urlBase, "/");
|
||||
$this->assetToken = $assetToken;
|
||||
$this->appUrl = $appUrl;
|
||||
@@ -61,7 +63,7 @@ PATTERN;
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->slug($arguments) ?>";
|
||||
}
|
||||
|
||||
throw new \ParseExtensionException("Unknown token given $token in UrlExtension.");
|
||||
throw new \Exception("Unknown token given $token in UrlExtension.");
|
||||
}
|
||||
|
||||
public function exportFunctions(): array
|
||||
@@ -104,18 +106,22 @@ PATTERN;
|
||||
|
||||
public function buildUrl(string $uri = "", array $parameters = [], bool $appendVersion = false) : string
|
||||
{
|
||||
$this->eventExecute(UrlBuildUrlEvent::class, $uri, $parameters, $appendVersion);
|
||||
|
||||
return $this->setUrlParameters($this->url() . "/" . ltrim($uri, "/"), $appendVersion ? array_replace([ 'v' => $this->assetToken ], $parameters) : $parameters);
|
||||
}
|
||||
|
||||
public function buildAssetUrl(string $uri, array $parameters = [], bool $appendVersion = true) : string
|
||||
{
|
||||
$this->eventExecute(UrlBuildAssetEvent::class, $uri, $parameters, $appendVersion);
|
||||
|
||||
return $this->buildUrl($uri, $parameters, $appendVersion);
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -149,6 +155,8 @@ PATTERN;
|
||||
'class' => $class,
|
||||
'classMethod' => $method,
|
||||
];
|
||||
|
||||
$this->eventExecute(UrlRegisterRouteEvent::class, $name, $this->routes[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,9 +202,25 @@ PATTERN;
|
||||
|
||||
protected function domain() : string
|
||||
{
|
||||
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 ? $this->appUrl . $port : $_SERVER['HTTP_HOST']);
|
||||
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
|
||||
@@ -221,8 +245,10 @@ 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 = [];
|
||||
|
||||
@@ -249,13 +275,12 @@ PATTERN;
|
||||
if ($default ?? false) {
|
||||
$value = $default;
|
||||
}
|
||||
elseif ($this->enforceExistingArguments && ! strpos($route, sprintf('[/{%s:', $variable)) ) {
|
||||
throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments ( %s )", $route, $variable, json_encode($arguments)));
|
||||
elseif ( strpos($route, "[{$matches[0][0]}]") !== false && $this->enforceExistingArguments) {
|
||||
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 ?? "";
|
||||
|
||||
}
|
||||
|
||||
$route = str_replace(array_keys($search), array_values($search), $route);
|
||||
|
||||
@@ -6,18 +6,11 @@ use Picea\Compiler;
|
||||
|
||||
class DefaultRegistrations implements LanguageRegistration
|
||||
{
|
||||
protected array $extensions;
|
||||
|
||||
protected array $syntaxes;
|
||||
|
||||
protected array $controlStructures;
|
||||
|
||||
public function __construct(array $extensions = [], array $syntaxes = [], array $controlStructure = [])
|
||||
{
|
||||
$this->extensions = $extensions;
|
||||
$this->syntaxes = $syntaxes;
|
||||
$this->controlStructures = $controlStructure;
|
||||
}
|
||||
public function __construct(
|
||||
protected array $extensions,
|
||||
protected array $syntaxes,
|
||||
protected array $controlStructures,
|
||||
) { }
|
||||
|
||||
public function registerAll(Compiler $compiler) : void
|
||||
{
|
||||
@@ -67,6 +60,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);
|
||||
|
||||
+3
-2
@@ -43,7 +43,7 @@ class Picea implements LanguageRegistration
|
||||
){
|
||||
$this->cache = $cache ?? new Caching\Memory("");
|
||||
$this->context = $context ?? new Compiler\BaseContext();
|
||||
$this->languageRegistration = $languageRegistration ?? new Language\DefaultRegistrations();
|
||||
$this->languageRegistration = $languageRegistration ?? new Language\DefaultRegistrations([], [], []);
|
||||
$this->builderTemplatePath = $builderTemplatePath ?? dirname(__FILE__) . static::DEFAULT_BUILDER_TEMPLATE;
|
||||
$this->compiler = $compiler ?? $this->instanciateCompiler();
|
||||
$this->fileFetcher = $fileFetcher ?? $this->instanciateFileFetcher();
|
||||
@@ -79,7 +79,7 @@ class Picea implements LanguageRegistration
|
||||
#}
|
||||
}
|
||||
|
||||
exit();
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,6 +180,7 @@ class Picea implements LanguageRegistration
|
||||
{
|
||||
if ( $this->debug || ! $this->cache->compiled( $context->cacheFilename() ) ) {
|
||||
$context = $this->compileContext($context);
|
||||
|
||||
$this->cache->save($context);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user