Skip to content

Commit 52d4ec3

Browse files
committed
Better base path detection
1 parent e8daebe commit 52d4ec3

File tree

3 files changed

+24
-33
lines changed

3 files changed

+24
-33
lines changed

api.include.php

+8-11
Original file line numberDiff line numberDiff line change
@@ -7648,7 +7648,7 @@ class SimpleRouter implements Router
76487648

76497649
public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl)
76507650
{
7651-
$this->basePath = rtrim($basePath, '/');
7651+
$this->basePath = rtrim($basePath, '/') ?: rtrim($this->detectBasePath(), '/');;
76527652
$this->responder = $responder;
76537653
$this->cache = $cache;
76547654
$this->ttl = $ttl;
@@ -7658,18 +7658,18 @@ public function __construct(string $basePath, Responder $responder, Cache $cache
76587658
$this->middlewares = array();
76597659
}
76607660

7661-
private function detectBasePath(ServerRequestInterface $request): string
7661+
private function detectBasePath(): string
76627662
{
7663-
$serverParams = $request->getServerParams();
7664-
if (isset($serverParams['REQUEST_URI'])) {
7665-
$fullPath = urldecode(explode('?', $serverParams['REQUEST_URI'])[0]);
7666-
if (isset($serverParams['PATH_INFO'])) {
7667-
$path = $serverParams['PATH_INFO'];
7663+
if (isset($_SERVER['REQUEST_URI'])) {
7664+
$fullPath = urldecode(explode('?', $_SERVER['REQUEST_URI'])[0]);
7665+
if (isset($_SERVER['PATH_INFO'])) {
7666+
$path = $_SERVER['PATH_INFO'];
76687667
if (substr($fullPath, -1 * strlen($path)) == $path) {
76697668
return substr($fullPath, 0, -1 * strlen($path));
76707669
}
76717670
}
7672-
if ('/' . basename(__FILE__) == $fullPath) {
7671+
$path = '/' . basename(__FILE__);
7672+
if (substr($fullPath, -1 * strlen($path)) == $path) {
76737673
return $fullPath;
76747674
}
76757675
}
@@ -7710,9 +7710,6 @@ public function load(Middleware $middleware) /*: void*/
77107710

77117711
public function route(ServerRequestInterface $request): ResponseInterface
77127712
{
7713-
if (!$this->basePath) {
7714-
$this->basePath = rtrim($this->detectBasePath($request), '/');
7715-
}
77167713
if ($this->registration) {
77177714
$data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE));
77187715
$this->cache->set('PathTree', $data, $this->ttl);

api.php

+8-11
Original file line numberDiff line numberDiff line change
@@ -7648,7 +7648,7 @@ class SimpleRouter implements Router
76487648

76497649
public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl)
76507650
{
7651-
$this->basePath = rtrim($basePath, '/');
7651+
$this->basePath = rtrim($basePath, '/') ?: rtrim($this->detectBasePath(), '/');;
76527652
$this->responder = $responder;
76537653
$this->cache = $cache;
76547654
$this->ttl = $ttl;
@@ -7658,18 +7658,18 @@ public function __construct(string $basePath, Responder $responder, Cache $cache
76587658
$this->middlewares = array();
76597659
}
76607660

7661-
private function detectBasePath(ServerRequestInterface $request): string
7661+
private function detectBasePath(): string
76627662
{
7663-
$serverParams = $request->getServerParams();
7664-
if (isset($serverParams['REQUEST_URI'])) {
7665-
$fullPath = urldecode(explode('?', $serverParams['REQUEST_URI'])[0]);
7666-
if (isset($serverParams['PATH_INFO'])) {
7667-
$path = $serverParams['PATH_INFO'];
7663+
if (isset($_SERVER['REQUEST_URI'])) {
7664+
$fullPath = urldecode(explode('?', $_SERVER['REQUEST_URI'])[0]);
7665+
if (isset($_SERVER['PATH_INFO'])) {
7666+
$path = $_SERVER['PATH_INFO'];
76687667
if (substr($fullPath, -1 * strlen($path)) == $path) {
76697668
return substr($fullPath, 0, -1 * strlen($path));
76707669
}
76717670
}
7672-
if ('/' . basename(__FILE__) == $fullPath) {
7671+
$path = '/' . basename(__FILE__);
7672+
if (substr($fullPath, -1 * strlen($path)) == $path) {
76737673
return $fullPath;
76747674
}
76757675
}
@@ -7710,9 +7710,6 @@ public function load(Middleware $middleware) /*: void*/
77107710

77117711
public function route(ServerRequestInterface $request): ResponseInterface
77127712
{
7713-
if (!$this->basePath) {
7714-
$this->basePath = rtrim($this->detectBasePath($request), '/');
7715-
}
77167713
if ($this->registration) {
77177714
$data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE));
77187715
$this->cache->set('PathTree', $data, $this->ttl);

src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php

+8-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SimpleRouter implements Router
2424

2525
public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl)
2626
{
27-
$this->basePath = rtrim($basePath, '/');
27+
$this->basePath = rtrim($basePath, '/') ?: rtrim($this->detectBasePath(), '/');;
2828
$this->responder = $responder;
2929
$this->cache = $cache;
3030
$this->ttl = $ttl;
@@ -34,18 +34,18 @@ public function __construct(string $basePath, Responder $responder, Cache $cache
3434
$this->middlewares = array();
3535
}
3636

37-
private function detectBasePath(ServerRequestInterface $request): string
37+
private function detectBasePath(): string
3838
{
39-
$serverParams = $request->getServerParams();
40-
if (isset($serverParams['REQUEST_URI'])) {
41-
$fullPath = urldecode(explode('?', $serverParams['REQUEST_URI'])[0]);
42-
if (isset($serverParams['PATH_INFO'])) {
43-
$path = $serverParams['PATH_INFO'];
39+
if (isset($_SERVER['REQUEST_URI'])) {
40+
$fullPath = urldecode(explode('?', $_SERVER['REQUEST_URI'])[0]);
41+
if (isset($_SERVER['PATH_INFO'])) {
42+
$path = $_SERVER['PATH_INFO'];
4443
if (substr($fullPath, -1 * strlen($path)) == $path) {
4544
return substr($fullPath, 0, -1 * strlen($path));
4645
}
4746
}
48-
if ('/' . basename(__FILE__) == $fullPath) {
47+
$path = '/' . basename(__FILE__);
48+
if (substr($fullPath, -1 * strlen($path)) == $path) {
4949
return $fullPath;
5050
}
5151
}
@@ -86,9 +86,6 @@ public function load(Middleware $middleware) /*: void*/
8686

8787
public function route(ServerRequestInterface $request): ResponseInterface
8888
{
89-
if (!$this->basePath) {
90-
$this->basePath = rtrim($this->detectBasePath($request), '/');
91-
}
9289
if ($this->registration) {
9390
$data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE));
9491
$this->cache->set('PathTree', $data, $this->ttl);

0 commit comments

Comments
 (0)