From 6ee3e1bbaec319e89320ddd6c5a0ef2548abd17e Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 30 Mar 2023 18:29:17 +0000 Subject: [PATCH 1/2] - Forced parameters in URL to be filled in UrlExtension buildUrl() and disabled the custom exception for now... --- src/Extension/UrlExtension.php | 25 ++++++++++++++++++------- src/Picea.php | 10 +++++----- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Extension/UrlExtension.php b/src/Extension/UrlExtension.php index 8d63f85..9ddc9eb 100644 --- a/src/Extension/UrlExtension.php +++ b/src/Extension/UrlExtension.php @@ -24,6 +24,9 @@ PATTERN; public array $tokens = [ "url" , "route", "asset", "slug" ]; + #[\Deprecated] + protected bool $enforceExistingArguments = true; + public function __construct(string $urlBase = "", string $assetToken = "") { $this->urlBase = trim($urlBase, "/"); $this->assetToken = $assetToken; @@ -216,26 +219,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); diff --git a/src/Picea.php b/src/Picea.php index 5879360..e7caa98 100644 --- a/src/Picea.php +++ b/src/Picea.php @@ -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(); From 44b028393852ccd2660e1d756ce0bf3964e812e2 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 30 Mar 2023 14:46:52 -0400 Subject: [PATCH 2/2] - Merged with new code-base --- src/Extension/UrlExtension.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Extension/UrlExtension.php b/src/Extension/UrlExtension.php index 9ddc9eb..691d5dc 100644 --- a/src/Extension/UrlExtension.php +++ b/src/Extension/UrlExtension.php @@ -16,20 +16,26 @@ 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" ]; #[\Deprecated] protected bool $enforceExistingArguments = true; - public function __construct(string $urlBase = "", string $assetToken = "") { + 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, array $options = []) : string @@ -169,7 +175,7 @@ PATTERN; protected function scheme() : string { - return ( $this->isHttps() ? "https" : "http" ) . "://"; + return ( $this->forceSSL || $this->isHttps() ? "https" : "http" ) . "://"; } protected function base() : string @@ -188,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