From cc61ef5fdd7c76c379822c87e139b48aef01b583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:13:46 +0200 Subject: [PATCH 1/7] TwigExtension: Extract getBasePath method --- src/Twig/TwigExtension.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index 58f3e36a..9ffaaef1 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -136,7 +136,7 @@ private function pathPrefix(): string return $this->pathPrefix; } - $rootUri = $this->request->getUri()->getBasePath(); + $rootUri = $this->getBasePath(); // Get URL part prepending index.php $indexPos = strpos($rootUri, 'index.php'); @@ -146,4 +146,9 @@ private function pathPrefix(): string return $rootUri; } + + private function getBasePath(): string + { + return $this->request->getUri()->getBasePath(); + } } From d344354f21768b8b47b383cb109a093173134940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:15:33 +0200 Subject: [PATCH 2/7] TwigExtension: Rename pathPrefix to getPathPrefix --- src/Twig/TwigExtension.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index 9ffaaef1..76560bd4 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -78,7 +78,7 @@ public function url(string $name, $queryargs = []): string // this is copy of \Slim\Slim::urlFor() // to mix path prefix in \Slim\Slim::urlFor - return rtrim($this->pathPrefix(), '/') . $this->router->urlFor($name) . $query; + return rtrim($this->getPathPrefix(), '/') . $this->router->urlFor($name) . $query; } /** @@ -89,7 +89,7 @@ public function url(string $name, $queryargs = []): string */ public function staticUrl(string $path): string { - $rootUri = $this->pathPrefix(); + $rootUri = $this->getPathPrefix(); return rtrim($rootUri, '/') . '/' . $path; } @@ -130,7 +130,7 @@ public function formatPercent($value): string return number_format((float)$value * 100, 0) . ' %'; } - private function pathPrefix(): string + private function getPathPrefix(): string { if ($this->pathPrefix !== null) { return $this->pathPrefix; From 7e31007e32feb3446724500bc067ac6ed2b1ea2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:18:49 +0200 Subject: [PATCH 3/7] TwigExtension: Make pathPrefix a function --- src/Twig/TwigExtension.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index 76560bd4..5d63d47e 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -75,10 +75,7 @@ public function url(string $name, $queryargs = []): string $query = '?' . http_build_query($queryargs); } - // this is copy of \Slim\Slim::urlFor() - // to mix path prefix in \Slim\Slim::urlFor - - return rtrim($this->getPathPrefix(), '/') . $this->router->urlFor($name) . $query; + return $this->pathPrefix($this->router->urlFor($name) . $query); } /** @@ -89,9 +86,7 @@ public function url(string $name, $queryargs = []): string */ public function staticUrl(string $path): string { - $rootUri = $this->getPathPrefix(); - - return rtrim($rootUri, '/') . '/' . $path; + return $this->pathPrefix($path); } public function formatBytes($value): string @@ -130,6 +125,11 @@ public function formatPercent($value): string return number_format((float)$value * 100, 0) . ' %'; } + private function pathPrefix($path): string + { + return rtrim($this->getPathPrefix(), '/') . '/'. $path; + } + private function getPathPrefix(): string { if ($this->pathPrefix !== null) { From bddab1717126cec43a9362ed85f85260277ed75b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:19:33 +0200 Subject: [PATCH 4/7] TwigExtension: Strip basePath from urlFor result --- src/Twig/TwigExtension.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index 5d63d47e..d0041915 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -75,7 +75,15 @@ public function url(string $name, $queryargs = []): string $query = '?' . http_build_query($queryargs); } - return $this->pathPrefix($this->router->urlFor($name) . $query); + $basePath = $this->getBasePath(); + $url = $this->router->urlFor($name); + + // Remove basePath from url + if (strpos($url, $basePath) === 0) { + $url = ltrim(substr($url, strlen($basePath)), '/'); + } + + return $this->pathPrefix($url . $query); } /** From 63b5646be77f4f47fd324c04c55ee613fc9abd41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:25:51 +0200 Subject: [PATCH 5/7] TwigExtension: Pre-compute basePath and pathPrefix --- src/Twig/TwigExtension.php | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index d0041915..4cbcb325 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -12,16 +12,16 @@ class TwigExtension extends AbstractExtension { /** @var Router */ private $router; - /** @var string|null */ + /** @var string */ + private $basePath; + /** @var string */ private $pathPrefix; - /** @var Request */ - private $request; public function __construct(Router $router, Request $request, ?string $pathPrefix) { $this->router = $router; - $this->request = $request; - $this->pathPrefix = $pathPrefix; + $this->basePath = $request->getUri()->getBasePath(); + $this->pathPrefix = $this->buildPathPrefix($this->basePath, $pathPrefix); } public function getFunctions(): array @@ -75,12 +75,11 @@ public function url(string $name, $queryargs = []): string $query = '?' . http_build_query($queryargs); } - $basePath = $this->getBasePath(); $url = $this->router->urlFor($name); // Remove basePath from url - if (strpos($url, $basePath) === 0) { - $url = ltrim(substr($url, strlen($basePath)), '/'); + if (strpos($url, $this->basePath) === 0) { + $url = ltrim(substr($url, strlen($this->basePath)), '/'); } return $this->pathPrefix($url . $query); @@ -135,17 +134,15 @@ public function formatPercent($value): string private function pathPrefix($path): string { - return rtrim($this->getPathPrefix(), '/') . '/'. $path; + return rtrim($this->pathPrefix, '/') . '/'. $path; } - private function getPathPrefix(): string + private function buildPathPrefix(string $rootUri, ?string $pathPrefix): string { - if ($this->pathPrefix !== null) { - return $this->pathPrefix; + if ($pathPrefix !== null) { + return $pathPrefix; } - $rootUri = $this->getBasePath(); - // Get URL part prepending index.php $indexPos = strpos($rootUri, 'index.php'); if ($indexPos > 0) { @@ -154,9 +151,4 @@ private function getPathPrefix(): string return $rootUri; } - - private function getBasePath(): string - { - return $this->request->getUri()->getBasePath(); - } } From c56b7c044e1d477337e8023d9e26dde7a57b08db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:30:12 +0200 Subject: [PATCH 6/7] TwigExtension: Move rtrim to buildPathPrefix rather pathPrefix --- src/Twig/TwigExtension.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index 4cbcb325..d6621ef6 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -21,7 +21,7 @@ public function __construct(Router $router, Request $request, ?string $pathPrefi { $this->router = $router; $this->basePath = $request->getUri()->getBasePath(); - $this->pathPrefix = $this->buildPathPrefix($this->basePath, $pathPrefix); + $this->pathPrefix = rtrim($this->buildPathPrefix($this->basePath, $pathPrefix), '/'); } public function getFunctions(): array @@ -134,13 +134,13 @@ public function formatPercent($value): string private function pathPrefix($path): string { - return rtrim($this->pathPrefix, '/') . '/'. $path; + return $this->pathPrefix . '/' . $path; } private function buildPathPrefix(string $rootUri, ?string $pathPrefix): string { if ($pathPrefix !== null) { - return $pathPrefix; + return rtrim($pathPrefix, '/'); } // Get URL part prepending index.php @@ -149,6 +149,6 @@ private function buildPathPrefix(string $rootUri, ?string $pathPrefix): string return substr($rootUri, 0, $indexPos); } - return $rootUri; + return rtrim($rootUri, '/'); } } From 9faee081243b6d9d50b03c46217b29c4c5e74ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 17 Jan 2022 18:36:33 +0200 Subject: [PATCH 7/7] TwigExtension: Avoid strpos(): Empty needle error --- src/Twig/TwigExtension.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Twig/TwigExtension.php b/src/Twig/TwigExtension.php index d6621ef6..d104d1c1 100755 --- a/src/Twig/TwigExtension.php +++ b/src/Twig/TwigExtension.php @@ -78,11 +78,11 @@ public function url(string $name, $queryargs = []): string $url = $this->router->urlFor($name); // Remove basePath from url - if (strpos($url, $this->basePath) === 0) { - $url = ltrim(substr($url, strlen($this->basePath)), '/'); + if ($this->basePath && strpos($url, $this->basePath) === 0) { + $url = substr($url, strlen($this->basePath)); } - return $this->pathPrefix($url . $query); + return $this->pathPrefix(ltrim($url, '/') . $query); } /**