Skip to content

Commit 7b634d9

Browse files
committed
Better support for SlimPHP
1 parent 16525b9 commit 7b634d9

File tree

5 files changed

+58
-14
lines changed

5 files changed

+58
-14
lines changed

api.php

+29-7
Original file line numberDiff line numberDiff line change
@@ -4478,6 +4478,7 @@ public function route(ServerRequestInterface $request): ResponseInterface;
44784478

44794479
class SimpleRouter implements Router
44804480
{
4481+
private $basePath;
44814482
private $responder;
44824483
private $cache;
44834484
private $ttl;
@@ -4487,8 +4488,9 @@ class SimpleRouter implements Router
44874488
private $routeHandlers;
44884489
private $middlewares;
44894490

4490-
public function __construct(Responder $responder, Cache $cache, int $ttl, bool $debug)
4491+
public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl, bool $debug)
44914492
{
4493+
$this->basePath = $basePath;
44924494
$this->responder = $responder;
44934495
$this->cache = $cache;
44944496
$this->ttl = $ttl;
@@ -4548,8 +4550,26 @@ private function getRouteNumbers(ServerRequestInterface $request): array
45484550
return $this->routes->match($path);
45494551
}
45504552

4553+
private function removeBasePath(ServerRequestInterface $request): ServerRequestInterface
4554+
{
4555+
if ($this->basePath) {
4556+
$path = $request->getUri()->getPath();
4557+
$basePath = rtrim($this->basePath, '/');
4558+
if (substr($path, 0, strlen($basePath)) == $basePath) {
4559+
$path = substr($path, strlen($basePath));
4560+
$request = $request->withUri($request->getUri()->withPath($path));
4561+
}
4562+
} elseif (isset($_SERVER['PATH_INFO'])) {
4563+
$path = $_SERVER['PATH_INFO'];
4564+
$request = $request->withUri($request->getUri()->withPath($path));
4565+
}
4566+
return $request;
4567+
}
4568+
45514569
public function handle(ServerRequestInterface $request): ResponseInterface
45524570
{
4571+
$request = $this->removeBasePath($request);
4572+
45534573
if (count($this->middlewares)) {
45544574
$handler = array_pop($this->middlewares);
45554575
return $handler->process($request, $this);
@@ -6948,7 +6968,7 @@ public function __construct(Config $config)
69486968
$cache = CacheFactory::create($config);
69496969
$reflection = new ReflectionService($db, $cache, $config->getCacheTime());
69506970
$responder = new Responder();
6951-
$router = new SimpleRouter($responder, $cache, $config->getCacheTime(), $config->getDebug());
6971+
$router = new SimpleRouter($config->getBasePath(), $responder, $cache, $config->getCacheTime(), $config->getDebug());
69526972
foreach ($config->getMiddlewares() as $middleware => $properties) {
69536973
switch ($middleware) {
69546974
case 'cors':
@@ -7048,6 +7068,7 @@ class Config
70487068
'cachePath' => '',
70497069
'cacheTime' => 10,
70507070
'debug' => false,
7071+
'basePath' => '',
70517072
'openApiBase' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}',
70527073
];
70537074

@@ -7184,6 +7205,11 @@ public function getDebug(): bool
71847205
return $this->values['debug'];
71857206
}
71867207

7208+
public function getBasePath(): string
7209+
{
7210+
return $this->values['basePath'];
7211+
}
7212+
71877213
public function getOpenApiBase(): array
71887214
{
71897215
return json_decode($this->values['openApiBase'], true);
@@ -7280,11 +7306,7 @@ public static function getParams(ServerRequestInterface $request): array
72807306

72817307
public static function getPathSegment(ServerRequestInterface $request, int $part): string
72827308
{
7283-
if (isset($_SERVER['PATH_INFO'])) {
7284-
$path = $_SERVER['PATH_INFO'];
7285-
} else {
7286-
$path = $request->getUri()->getPath();
7287-
}
7309+
$path = $request->getUri()->getPath();
72887310
$pathSegments = explode('/', rtrim($path, '/'));
72897311
if ($part < 0 || $part >= count($pathSegments)) {
72907312
return '';

src/Tqdev/PhpCrudApi/Api.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct(Config $config)
5151
$cache = CacheFactory::create($config);
5252
$reflection = new ReflectionService($db, $cache, $config->getCacheTime());
5353
$responder = new Responder();
54-
$router = new SimpleRouter($responder, $cache, $config->getCacheTime(), $config->getDebug());
54+
$router = new SimpleRouter($config->getBasePath(), $responder, $cache, $config->getCacheTime(), $config->getDebug());
5555
foreach ($config->getMiddlewares() as $middleware => $properties) {
5656
switch ($middleware) {
5757
case 'cors':

src/Tqdev/PhpCrudApi/Config.php

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Config
1616
'cachePath' => '',
1717
'cacheTime' => 10,
1818
'debug' => false,
19+
'basePath' => '',
1920
'openApiBase' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}',
2021
];
2122

@@ -152,6 +153,11 @@ public function getDebug(): bool
152153
return $this->values['debug'];
153154
}
154155

156+
public function getBasePath(): string
157+
{
158+
return $this->values['basePath'];
159+
}
160+
155161
public function getOpenApiBase(): array
156162
{
157163
return json_decode($this->values['openApiBase'], true);

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
class SimpleRouter implements Router
1515
{
16+
private $basePath;
1617
private $responder;
1718
private $cache;
1819
private $ttl;
@@ -22,8 +23,9 @@ class SimpleRouter implements Router
2223
private $routeHandlers;
2324
private $middlewares;
2425

25-
public function __construct(Responder $responder, Cache $cache, int $ttl, bool $debug)
26+
public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl, bool $debug)
2627
{
28+
$this->basePath = $basePath;
2729
$this->responder = $responder;
2830
$this->cache = $cache;
2931
$this->ttl = $ttl;
@@ -83,8 +85,26 @@ private function getRouteNumbers(ServerRequestInterface $request): array
8385
return $this->routes->match($path);
8486
}
8587

88+
private function removeBasePath(ServerRequestInterface $request): ServerRequestInterface
89+
{
90+
if ($this->basePath) {
91+
$path = $request->getUri()->getPath();
92+
$basePath = rtrim($this->basePath, '/');
93+
if (substr($path, 0, strlen($basePath)) == $basePath) {
94+
$path = substr($path, strlen($basePath));
95+
$request = $request->withUri($request->getUri()->withPath($path));
96+
}
97+
} elseif (isset($_SERVER['PATH_INFO'])) {
98+
$path = $_SERVER['PATH_INFO'];
99+
$request = $request->withUri($request->getUri()->withPath($path));
100+
}
101+
return $request;
102+
}
103+
86104
public function handle(ServerRequestInterface $request): ResponseInterface
87105
{
106+
$request = $this->removeBasePath($request);
107+
88108
if (count($this->middlewares)) {
89109
$handler = array_pop($this->middlewares);
90110
return $handler->process($request, $this);

src/Tqdev/PhpCrudApi/RequestUtils.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ public static function getParams(ServerRequestInterface $request): array
2929

3030
public static function getPathSegment(ServerRequestInterface $request, int $part): string
3131
{
32-
if (isset($_SERVER['PATH_INFO'])) {
33-
$path = $_SERVER['PATH_INFO'];
34-
} else {
35-
$path = $request->getUri()->getPath();
36-
}
32+
$path = $request->getUri()->getPath();
3733
$pathSegments = explode('/', rtrim($path, '/'));
3834
if ($part < 0 || $part >= count($pathSegments)) {
3935
return '';

0 commit comments

Comments
 (0)