From 52d4ec34b2f52f5b9e0098ab675f2fbda571d424 Mon Sep 17 00:00:00 2001 From: Maurits van der Schee Date: Thu, 6 Oct 2022 16:51:33 +0200 Subject: [PATCH] Better base path detection --- api.include.php | 19 ++++++++----------- api.php | 19 ++++++++----------- .../Middleware/Router/SimpleRouter.php | 19 ++++++++----------- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/api.include.php b/api.include.php index 408d36fc..9996331e 100644 --- a/api.include.php +++ b/api.include.php @@ -7648,7 +7648,7 @@ class SimpleRouter implements Router public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl) { - $this->basePath = rtrim($basePath, '/'); + $this->basePath = rtrim($basePath, '/') ?: rtrim($this->detectBasePath(), '/');; $this->responder = $responder; $this->cache = $cache; $this->ttl = $ttl; @@ -7658,18 +7658,18 @@ public function __construct(string $basePath, Responder $responder, Cache $cache $this->middlewares = array(); } - private function detectBasePath(ServerRequestInterface $request): string + private function detectBasePath(): string { - $serverParams = $request->getServerParams(); - if (isset($serverParams['REQUEST_URI'])) { - $fullPath = urldecode(explode('?', $serverParams['REQUEST_URI'])[0]); - if (isset($serverParams['PATH_INFO'])) { - $path = $serverParams['PATH_INFO']; + if (isset($_SERVER['REQUEST_URI'])) { + $fullPath = urldecode(explode('?', $_SERVER['REQUEST_URI'])[0]); + if (isset($_SERVER['PATH_INFO'])) { + $path = $_SERVER['PATH_INFO']; if (substr($fullPath, -1 * strlen($path)) == $path) { return substr($fullPath, 0, -1 * strlen($path)); } } - if ('/' . basename(__FILE__) == $fullPath) { + $path = '/' . basename(__FILE__); + if (substr($fullPath, -1 * strlen($path)) == $path) { return $fullPath; } } @@ -7710,9 +7710,6 @@ public function load(Middleware $middleware) /*: void*/ public function route(ServerRequestInterface $request): ResponseInterface { - if (!$this->basePath) { - $this->basePath = rtrim($this->detectBasePath($request), '/'); - } if ($this->registration) { $data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE)); $this->cache->set('PathTree', $data, $this->ttl); diff --git a/api.php b/api.php index e1424c8f..54e6f599 100644 --- a/api.php +++ b/api.php @@ -7648,7 +7648,7 @@ class SimpleRouter implements Router public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl) { - $this->basePath = rtrim($basePath, '/'); + $this->basePath = rtrim($basePath, '/') ?: rtrim($this->detectBasePath(), '/');; $this->responder = $responder; $this->cache = $cache; $this->ttl = $ttl; @@ -7658,18 +7658,18 @@ public function __construct(string $basePath, Responder $responder, Cache $cache $this->middlewares = array(); } - private function detectBasePath(ServerRequestInterface $request): string + private function detectBasePath(): string { - $serverParams = $request->getServerParams(); - if (isset($serverParams['REQUEST_URI'])) { - $fullPath = urldecode(explode('?', $serverParams['REQUEST_URI'])[0]); - if (isset($serverParams['PATH_INFO'])) { - $path = $serverParams['PATH_INFO']; + if (isset($_SERVER['REQUEST_URI'])) { + $fullPath = urldecode(explode('?', $_SERVER['REQUEST_URI'])[0]); + if (isset($_SERVER['PATH_INFO'])) { + $path = $_SERVER['PATH_INFO']; if (substr($fullPath, -1 * strlen($path)) == $path) { return substr($fullPath, 0, -1 * strlen($path)); } } - if ('/' . basename(__FILE__) == $fullPath) { + $path = '/' . basename(__FILE__); + if (substr($fullPath, -1 * strlen($path)) == $path) { return $fullPath; } } @@ -7710,9 +7710,6 @@ public function load(Middleware $middleware) /*: void*/ public function route(ServerRequestInterface $request): ResponseInterface { - if (!$this->basePath) { - $this->basePath = rtrim($this->detectBasePath($request), '/'); - } if ($this->registration) { $data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE)); $this->cache->set('PathTree', $data, $this->ttl); diff --git a/src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php b/src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php index 28ab4e7c..5c0d10b9 100644 --- a/src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php +++ b/src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php @@ -24,7 +24,7 @@ class SimpleRouter implements Router public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl) { - $this->basePath = rtrim($basePath, '/'); + $this->basePath = rtrim($basePath, '/') ?: rtrim($this->detectBasePath(), '/');; $this->responder = $responder; $this->cache = $cache; $this->ttl = $ttl; @@ -34,18 +34,18 @@ public function __construct(string $basePath, Responder $responder, Cache $cache $this->middlewares = array(); } - private function detectBasePath(ServerRequestInterface $request): string + private function detectBasePath(): string { - $serverParams = $request->getServerParams(); - if (isset($serverParams['REQUEST_URI'])) { - $fullPath = urldecode(explode('?', $serverParams['REQUEST_URI'])[0]); - if (isset($serverParams['PATH_INFO'])) { - $path = $serverParams['PATH_INFO']; + if (isset($_SERVER['REQUEST_URI'])) { + $fullPath = urldecode(explode('?', $_SERVER['REQUEST_URI'])[0]); + if (isset($_SERVER['PATH_INFO'])) { + $path = $_SERVER['PATH_INFO']; if (substr($fullPath, -1 * strlen($path)) == $path) { return substr($fullPath, 0, -1 * strlen($path)); } } - if ('/' . basename(__FILE__) == $fullPath) { + $path = '/' . basename(__FILE__); + if (substr($fullPath, -1 * strlen($path)) == $path) { return $fullPath; } } @@ -86,9 +86,6 @@ public function load(Middleware $middleware) /*: void*/ public function route(ServerRequestInterface $request): ResponseInterface { - if (!$this->basePath) { - $this->basePath = rtrim($this->detectBasePath($request), '/'); - } if ($this->registration) { $data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE)); $this->cache->set('PathTree', $data, $this->ttl);