Skip to content

Commit

Permalink
Better base path detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mevdschee committed Oct 6, 2022
1 parent e8daebe commit 52d4ec3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
19 changes: 8 additions & 11 deletions api.include.php
Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 8 additions & 11 deletions api.php
Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 8 additions & 11 deletions src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php
Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 52d4ec3

Please sign in to comment.