Skip to content

Commit c7a648e

Browse files
committed
use PSR-7 for SimpleRouter
1 parent 25dadb2 commit c7a648e

File tree

4 files changed

+39
-36
lines changed

4 files changed

+39
-36
lines changed

api.include.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7121,7 +7121,6 @@ public function route(ServerRequestInterface $request): ResponseInterface;
71217121
use Tqdev\PhpCrudApi\Record\ErrorCode;
71227122
use Tqdev\PhpCrudApi\Record\PathTree;
71237123
use Tqdev\PhpCrudApi\RequestUtils;
7124-
use Tqdev\PhpCrudApi\ResponseUtils;
71257124

71267125
class SimpleRouter implements Router
71277126
{
@@ -7136,7 +7135,7 @@ class SimpleRouter implements Router
71367135

71377136
public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl)
71387137
{
7139-
$this->basePath = rtrim($this->detectBasePath($basePath), '/');
7138+
$this->basePath = rtrim($basePath, '/');
71407139
$this->responder = $responder;
71417140
$this->cache = $cache;
71427141
$this->ttl = $ttl;
@@ -7146,15 +7145,13 @@ public function __construct(string $basePath, Responder $responder, Cache $cache
71467145
$this->middlewares = array();
71477146
}
71487147

7149-
private function detectBasePath(string $basePath): string
7148+
private function detectBasePath(ServerRequestInterface $request): string
71507149
{
7151-
if ($basePath) {
7152-
return $basePath;
7153-
}
7154-
if (isset($_SERVER['REQUEST_URI'])) {
7155-
$fullPath = urldecode(explode('?', $_SERVER['REQUEST_URI'])[0]);
7156-
if (isset($_SERVER['PATH_INFO'])) {
7157-
$path = $_SERVER['PATH_INFO'];
7150+
$serverParams = $request->getServerParams();
7151+
if (isset($serverParams['REQUEST_URI'])) {
7152+
$fullPath = urldecode(explode('?', $serverParams['REQUEST_URI'])[0]);
7153+
if (isset($serverParams['PATH_INFO'])) {
7154+
$path = $serverParams['PATH_INFO'];
71587155
if (substr($fullPath, -1 * strlen($path)) == $path) {
71597156
return substr($fullPath, 0, -1 * strlen($path));
71607157
}
@@ -7200,6 +7197,9 @@ public function load(Middleware $middleware) /*: void*/
72007197

72017198
public function route(ServerRequestInterface $request): ResponseInterface
72027199
{
7200+
if (!$this->basePath) {
7201+
$this->basePath = rtrim($this->detectBasePath($request), '/');
7202+
}
72037203
if ($this->registration) {
72047204
$data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE));
72057205
$this->cache->set('PathTree', $data, $this->ttl);
@@ -7549,8 +7549,9 @@ private function writePasswords(string $passwordFile, array $passwords): bool
75497549

75507550
private function getAuthorizationCredentials(ServerRequestInterface $request): string
75517551
{
7552-
if (isset($_SERVER['PHP_AUTH_USER'])) {
7553-
return $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'];
7552+
$serverParams = $request->getServerParams();
7553+
if (isset($serverParams['PHP_AUTH_USER'])) {
7554+
return $serverParams['PHP_AUTH_USER'] . ':' . $serverParams['PHP_AUTH_PW'];
75547555
}
75557556
$header = RequestUtils::getHeader($request, 'Authorization');
75567557
$parts = explode(' ', trim($header), 2);

api.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7121,7 +7121,6 @@ public function route(ServerRequestInterface $request): ResponseInterface;
71217121
use Tqdev\PhpCrudApi\Record\ErrorCode;
71227122
use Tqdev\PhpCrudApi\Record\PathTree;
71237123
use Tqdev\PhpCrudApi\RequestUtils;
7124-
use Tqdev\PhpCrudApi\ResponseUtils;
71257124

71267125
class SimpleRouter implements Router
71277126
{
@@ -7136,7 +7135,7 @@ class SimpleRouter implements Router
71367135

71377136
public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl)
71387137
{
7139-
$this->basePath = rtrim($this->detectBasePath($basePath), '/');
7138+
$this->basePath = rtrim($basePath, '/');
71407139
$this->responder = $responder;
71417140
$this->cache = $cache;
71427141
$this->ttl = $ttl;
@@ -7146,15 +7145,13 @@ public function __construct(string $basePath, Responder $responder, Cache $cache
71467145
$this->middlewares = array();
71477146
}
71487147

7149-
private function detectBasePath(string $basePath): string
7148+
private function detectBasePath(ServerRequestInterface $request): string
71507149
{
7151-
if ($basePath) {
7152-
return $basePath;
7153-
}
7154-
if (isset($_SERVER['REQUEST_URI'])) {
7155-
$fullPath = urldecode(explode('?', $_SERVER['REQUEST_URI'])[0]);
7156-
if (isset($_SERVER['PATH_INFO'])) {
7157-
$path = $_SERVER['PATH_INFO'];
7150+
$serverParams = $request->getServerParams();
7151+
if (isset($serverParams['REQUEST_URI'])) {
7152+
$fullPath = urldecode(explode('?', $serverParams['REQUEST_URI'])[0]);
7153+
if (isset($serverParams['PATH_INFO'])) {
7154+
$path = $serverParams['PATH_INFO'];
71587155
if (substr($fullPath, -1 * strlen($path)) == $path) {
71597156
return substr($fullPath, 0, -1 * strlen($path));
71607157
}
@@ -7200,6 +7197,9 @@ public function load(Middleware $middleware) /*: void*/
72007197

72017198
public function route(ServerRequestInterface $request): ResponseInterface
72027199
{
7200+
if (!$this->basePath) {
7201+
$this->basePath = rtrim($this->detectBasePath($request), '/');
7202+
}
72037203
if ($this->registration) {
72047204
$data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE));
72057205
$this->cache->set('PathTree', $data, $this->ttl);
@@ -7549,8 +7549,9 @@ private function writePasswords(string $passwordFile, array $passwords): bool
75497549

75507550
private function getAuthorizationCredentials(ServerRequestInterface $request): string
75517551
{
7552-
if (isset($_SERVER['PHP_AUTH_USER'])) {
7553-
return $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'];
7552+
$serverParams = $request->getServerParams();
7553+
if (isset($serverParams['PHP_AUTH_USER'])) {
7554+
return $serverParams['PHP_AUTH_USER'] . ':' . $serverParams['PHP_AUTH_PW'];
75547555
}
75557556
$header = RequestUtils::getHeader($request, 'Authorization');
75567557
$parts = explode(' ', trim($header), 2);

src/Tqdev/PhpCrudApi/Middleware/BasicAuthMiddleware.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ private function writePasswords(string $passwordFile, array $passwords): bool
6363

6464
private function getAuthorizationCredentials(ServerRequestInterface $request): string
6565
{
66-
if (isset($_SERVER['PHP_AUTH_USER'])) {
67-
return $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'];
66+
$serverParams = $request->getServerParams();
67+
if (isset($serverParams['PHP_AUTH_USER'])) {
68+
return $serverParams['PHP_AUTH_USER'] . ':' . $serverParams['PHP_AUTH_PW'];
6869
}
6970
$header = RequestUtils::getHeader($request, 'Authorization');
7071
$parts = explode(' ', trim($header), 2);

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Tqdev\PhpCrudApi\Record\ErrorCode;
1111
use Tqdev\PhpCrudApi\Record\PathTree;
1212
use Tqdev\PhpCrudApi\RequestUtils;
13-
use Tqdev\PhpCrudApi\ResponseUtils;
1413

1514
class SimpleRouter implements Router
1615
{
@@ -25,7 +24,7 @@ class SimpleRouter implements Router
2524

2625
public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl)
2726
{
28-
$this->basePath = rtrim($this->detectBasePath($basePath), '/');
27+
$this->basePath = rtrim($basePath, '/');
2928
$this->responder = $responder;
3029
$this->cache = $cache;
3130
$this->ttl = $ttl;
@@ -35,15 +34,13 @@ public function __construct(string $basePath, Responder $responder, Cache $cache
3534
$this->middlewares = array();
3635
}
3736

38-
private function detectBasePath(string $basePath): string
37+
private function detectBasePath(ServerRequestInterface $request): string
3938
{
40-
if ($basePath) {
41-
return $basePath;
42-
}
43-
if (isset($_SERVER['REQUEST_URI'])) {
44-
$fullPath = urldecode(explode('?', $_SERVER['REQUEST_URI'])[0]);
45-
if (isset($_SERVER['PATH_INFO'])) {
46-
$path = $_SERVER['PATH_INFO'];
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'];
4744
if (substr($fullPath, -1 * strlen($path)) == $path) {
4845
return substr($fullPath, 0, -1 * strlen($path));
4946
}
@@ -89,6 +86,9 @@ public function load(Middleware $middleware) /*: void*/
8986

9087
public function route(ServerRequestInterface $request): ResponseInterface
9188
{
89+
if (!$this->basePath) {
90+
$this->basePath = rtrim($this->detectBasePath($request), '/');
91+
}
9292
if ($this->registration) {
9393
$data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE));
9494
$this->cache->set('PathTree', $data, $this->ttl);

0 commit comments

Comments
 (0)