This commit is contained in:
Dave M. 2023-05-31 14:40:23 -04:00
commit be65d45b41
5 changed files with 75 additions and 21 deletions

View File

@ -62,13 +62,13 @@ class SectionToken implements ControlStructure {
$order = $options['order'] ?? "count(\$___class__template->sectionList[$name]['$action'])"; $order = $options['order'] ?? "count(\$___class__template->sectionList[$name]['$action'])";
return "<?php \$___class__template->sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];". return "<?php \$___class__template->sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];" .
"\$___class__template->sectionList[$name]['$action'][] = [ "\$___class__template->sectionList[$name]['$action'][] = [
'order' => $order, 'order' => $order,
'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables, \$__event) {". 'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables, \$__event) {" .
"extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE); "extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE);" .
\$___class__template->sectionStack[] = $name; "\$___class__template->sectionStack[] = $name;" .
\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSection::class, $name);?>"; "\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSection::class, $name);?>";
} }
protected function printEndSection($context) : string protected function printEndSection($context) : string
@ -76,13 +76,10 @@ class SectionToken implements ControlStructure {
$section = array_pop($context->sections); $section = array_pop($context->sections);
$build = $context->extendFrom ? "!empty(\$___class__template->sectionStack) && \$___class__template->renderSection({$section['name']}, false);" : "\$___class__template->renderSection({$section['name']}, false);"; $build = $context->extendFrom ? "!empty(\$___class__template->sectionStack) && \$___class__template->renderSection({$section['name']}, false);" : "\$___class__template->renderSection({$section['name']}, false);";
return <<<PHP return "<?php \$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSectionDone::class, {$section['name']});" .
<?php "array_pop(\$___class__template->sectionStack); }];" .
\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSectionDone::class, {$section['name']}); $build .
array_pop(\$___class__template->sectionStack); }]; "?>";
$build
?>
PHP;
} }
} }

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;
}
}
}

View File

@ -6,7 +6,7 @@ use Picea\Compiler\Context;
class PrintExtension implements Extension { class PrintExtension implements Extension {
public array $token = [ "echo", "print", "printf" ]; public array $token = [ "echo", "print", "printf", "no_html" ];
public int $flag = \ENT_QUOTES; public int $flag = \ENT_QUOTES;
@ -35,6 +35,10 @@ class PrintExtension implements Extension {
} }
return "<?php echo htmlspecialchars(sprintf((string) $arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>"; 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)) ?>";
} }
} }

View File

@ -2,6 +2,7 @@
namespace Picea\Extension; namespace Picea\Extension;
use Notes\Route\Attribute\Object\Route;
use Picea\Compiler\Context; use Picea\Compiler\Context;
class UrlExtension implements Extension, FunctionExtension { class UrlExtension implements Extension, FunctionExtension {
@ -16,7 +17,7 @@ PATTERN;
protected string $urlBase; protected string $urlBase;
protected string $appUrl; protected array $appUrl;
protected string $assetToken; protected string $assetToken;
@ -31,7 +32,7 @@ PATTERN;
#[\Deprecated] #[\Deprecated]
protected bool $enforceExistingArguments = true; 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->urlBase = trim($urlBase, "/");
$this->assetToken = $assetToken; $this->assetToken = $assetToken;
$this->appUrl = $appUrl; $this->appUrl = $appUrl;
@ -115,7 +116,7 @@ PATTERN;
public function buildRouteUrl(string $name, array $parameters = [], bool $appendVersion = false) : string public function buildRouteUrl(string $name, array $parameters = [], bool $appendVersion = false) : string
{ {
if ( false !== ( $route = $this->routes[$name] ?? false ) ) { 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); $routeList = json_encode($this->routes, \JSON_PRETTY_PRINT);
@ -194,9 +195,25 @@ PATTERN;
protected function domain() : string protected function domain() : string
{ {
$port = $this->isDefaultPort() ? "" : ":" . $_SERVER['SERVER_PORT']; 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 protected function isDefaultPort() : bool
@ -221,8 +238,10 @@ PATTERN;
|| ( "off" !== ( strtolower($_SERVER['HTTPS'] ?? $_SERVER['HTTP_X_FORWARDED_SSL'] ?? $_SERVER['X-Forwarded-Ssl'] ?? "off")) ); || ( "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) ) { if ( preg_match_all('~{(.*?)}~si', $route, $matches, PREG_SET_ORDER) ) {
$search = []; $search = [];
@ -249,8 +268,8 @@ PATTERN;
if ($default ?? false) { if ($default ?? false) {
$value = $default; $value = $default;
} }
elseif ($this->enforceExistingArguments && ! strpos($route, sprintf('[/{%s:', $variable)) ) { elseif ($this->enforceExistingArguments) {dump($routeParam);
throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments ( %s )", $route, $variable, json_encode($arguments))); 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']));
} }
} }

View File

@ -67,6 +67,7 @@ class DefaultRegistrations implements LanguageRegistration
$compiler->registerExtension(new \Picea\Extension\PhpExtension()); $compiler->registerExtension(new \Picea\Extension\PhpExtension());
$compiler->registerExtension(new \Picea\Extension\PrintExtension()); $compiler->registerExtension(new \Picea\Extension\PrintExtension());
$compiler->registerExtension(new \Picea\Extension\JsonExtension()); $compiler->registerExtension(new \Picea\Extension\JsonExtension());
$compiler->registerExtension(new \Picea\Extension\BatchExtension());
foreach($this->extensions ?? [] as $extension) { foreach($this->extensions ?? [] as $extension) {
$compiler->registerExtension($extension); $compiler->registerExtension($extension);