diff --git a/src/ControlStructure/SectionToken.php b/src/ControlStructure/SectionToken.php index 2b0eac5..85c6d54 100644 --- a/src/ControlStructure/SectionToken.php +++ b/src/ControlStructure/SectionToken.php @@ -62,13 +62,13 @@ class SectionToken implements ControlStructure { $order = $options['order'] ?? "count(\$___class__template->sectionList[$name]['$action'])"; - return "sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];". + return "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);?>"; + '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 +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 <<eventExecute(\Picea\Event\Builder\ClassTemplateRenderSectionDone::class, {$section['name']}); - array_pop(\$___class__template->sectionStack); }]; - $build - ?> - PHP; + return "eventExecute(\Picea\Event\Builder\ClassTemplateRenderSectionDone::class, {$section['name']});" . + "array_pop(\$___class__template->sectionStack); }];" . + $build . + "?>"; } } \ No newline at end of file diff --git a/src/Extension/BatchExtension.php b/src/Extension/BatchExtension.php new file mode 100644 index 0000000..c661d86 --- /dev/null +++ b/src/Extension/BatchExtension.php @@ -0,0 +1,33 @@ + [ $this, 'batch' ] + ]; + } + + public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string + { + return ""; + } + + 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; + } + } +} diff --git a/src/Extension/PrintExtension.php b/src/Extension/PrintExtension.php index fd4c9dd..0bc5a1a 100644 --- a/src/Extension/PrintExtension.php +++ b/src/Extension/PrintExtension.php @@ -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; @@ -35,6 +35,10 @@ class PrintExtension implements Extension { } return "flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>"; + + case 'no_html': + return ""; + } } diff --git a/src/Extension/UrlExtension.php b/src/Extension/UrlExtension.php index 598c782..82082b6 100644 --- a/src/Extension/UrlExtension.php +++ b/src/Extension/UrlExtension.php @@ -2,6 +2,7 @@ namespace Picea\Extension; +use Notes\Route\Attribute\Object\Route; use Picea\Compiler\Context; class UrlExtension implements Extension, FunctionExtension { @@ -16,7 +17,7 @@ PATTERN; protected string $urlBase; - protected string $appUrl; + protected array $appUrl; protected string $assetToken; @@ -31,7 +32,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; @@ -115,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); @@ -194,9 +195,25 @@ PATTERN; 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 @@ -221,8 +238,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,8 +268,8 @@ 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 ($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'])); } } diff --git a/src/Language/DefaultRegistrations.php b/src/Language/DefaultRegistrations.php index 3719ad7..72d40f0 100644 --- a/src/Language/DefaultRegistrations.php +++ b/src/Language/DefaultRegistrations.php @@ -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);