From 08201b1958a7d4920f9a7c81de0240f00be54a51 Mon Sep 17 00:00:00 2001 From: Dave M Date: Wed, 12 Oct 2022 18:25:39 +0000 Subject: [PATCH] - Remove the old {? / ?} token as a PHP opening - Added url versions if needed --- src/Extension/UrlExtension.php | 32 +++++++++------------------ src/Language/DefaultRegistrations.php | 2 -- src/Syntax/PhpTagToken.php | 18 --------------- 3 files changed, 11 insertions(+), 41 deletions(-) delete mode 100644 src/Syntax/PhpTagToken.php diff --git a/src/Extension/UrlExtension.php b/src/Extension/UrlExtension.php index 99a66df..fddab6e 100644 --- a/src/Extension/UrlExtension.php +++ b/src/Extension/UrlExtension.php @@ -14,7 +14,7 @@ class UrlExtension implements Extension { protected array $routesTarget; - public array $tokens = [ "url" , "route", "asset", "url.current", "url.parameters" ]; + public array $tokens = [ "url" , "route", "route.cacheless", "asset", "url.current", "url.parameters" ]; public function __construct(Context $context, string $urlBase = "", string $assetToken = "") { $this->urlBase = trim($urlBase, "/"); @@ -67,20 +67,20 @@ class UrlExtension implements Extension { return $this->buildUrl($this->uri(), $parameters); } - public function buildUrl(string $uri = "", array $parameters = []) : string + public function buildUrl(string $uri = "", array $parameters = [], bool $appendVersion = false) : string { - return $this->setUrlParameters($this->url() . "/" . ltrim($uri, "/"), $parameters); + return $this->setUrlParameters($this->url() . "/" . ltrim($uri, "/"), $appendVersion ? array_replace([ 'v' => $this->assetToken ], $parameters) : $parameters); } - public function buildAssetUrl(string $uri, array $parameters = []) : string + public function buildAssetUrl(string $uri, array $parameters = [], bool $appendVersion = true) : string { - return $this->buildUrl($uri, array_replace([ 'v' => $this->assetToken ], $parameters)); + return $this->buildUrl($uri, $parameters, $appendVersion); } - public function buildRouteUrl(string $name, array $parameters = []) : string + 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); + return $this->buildUrl($this->prepareRoute($route['route'], $parameters), $parameters, $appendVersion); } $routeList = json_encode($this->routes, \JSON_PRETTY_PRINT); @@ -165,7 +165,7 @@ class UrlExtension implements Extension { $_SERVER['X-Forwarded-Ssl'] ?? "", $_SERVER['X-Forwarded-Proto'] ?? "", $_SERVER['X-Forwarded-Protocol'] ?? "", - ])) || isset($_SERVER['HTTP_X_ARR_SSL']); + ])); return $header || ( "443" === ( $_SERVER['SERVER_PORT'] ?? "" ) ) @@ -183,7 +183,7 @@ class UrlExtension implements Extension { list($variable, $default) = explode('=', $item[1]); } elseif (strpos($item[1], ":") !== false) { - list($variable, $type) = explode(':', $item[1]); + list($variable, ) = explode(':', $item[1]); } else { $variable = $item[1]; @@ -191,7 +191,6 @@ class UrlExtension implements Extension { if ( array_key_exists($variable, $arguments) ) { $value = $arguments[ $variable ]; - unset($arguments[ $variable ]); } else { @@ -199,22 +198,13 @@ class UrlExtension implements Extension { } $search[ $item[0] ] = $value; + } $route = str_replace(array_keys($search), array_values($search), $route); } - /* - * @TODO - must also take into account that recursivity is possible here [/test[/another[/still]]], - * so the regex must be adjusted (or simply using another method could also be a possiblity) - * while(strpos($route, '[') !== false && strpos($route, ']') !== false ) { - if ( preg_match_all('~[(.*?)]~si', $route, $matches, PREG_SET_ORDER) ) { - - } - }*/ - $route = trim(str_replace([ '[', ']' ], '', $route), '/'); - return $route; - } + } } diff --git a/src/Language/DefaultRegistrations.php b/src/Language/DefaultRegistrations.php index 051d071..aea8a95 100644 --- a/src/Language/DefaultRegistrations.php +++ b/src/Language/DefaultRegistrations.php @@ -28,7 +28,6 @@ class DefaultRegistrations implements LanguageRegistration public function registerSyntax(Compiler $compiler) : void { - $compiler->registerSyntax(new \Picea\Syntax\PhpTagToken()); $compiler->registerSyntax(new \Picea\Syntax\CommentToken()); $compiler->registerSyntax(new \Picea\Syntax\EchoRawToken()); $compiler->registerSyntax(new \Picea\Syntax\EchoSafeToken()); @@ -49,7 +48,6 @@ class DefaultRegistrations implements LanguageRegistration $compiler->registerControlStructure(new \Picea\ControlStructure\SwitchToken()); $compiler->registerControlStructure(new \Picea\ControlStructure\DefaultToken()); $compiler->registerControlStructure(new \Picea\ControlStructure\BreakToken()); - $compiler->registerControlStructure(new \Picea\ControlStructure\ContinueToken()); $compiler->registerControlStructure(new \Picea\ControlStructure\ExtendsToken()); $compiler->registerControlStructure(new \Picea\ControlStructure\SectionToken()); $compiler->registerControlStructure(new \Picea\ControlStructure\BlockToken()); diff --git a/src/Syntax/PhpTagToken.php b/src/Syntax/PhpTagToken.php deleted file mode 100644 index 3dc0c6a..0000000 --- a/src/Syntax/PhpTagToken.php +++ /dev/null @@ -1,18 +0,0 @@ -tokenOpen})(.*?)({$this->tokenClose})#s", function ($matches) { - return ""; - }, $sourceCode); - } - -}