Skip to content

Commit 46126ae

Browse files
committed
better basePath support
1 parent 31845e1 commit 46126ae

File tree

1 file changed

+64
-34
lines changed

1 file changed

+64
-34
lines changed

api.php

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,30 +1690,20 @@ public function clear(): bool;
16901690

16911691
class CacheFactory
16921692
{
1693-
const PREFIX = 'phpcrudapi-%s-%s-%s-';
1694-
1695-
private static function getPrefix(Config $config): string
1696-
{
1697-
$driver = $config->getDriver();
1698-
$database = $config->getDatabase();
1699-
$filehash = substr(md5(__FILE__), 0, 8);
1700-
return sprintf(self::PREFIX, $driver, $database, $filehash);
1701-
}
1702-
1703-
public static function create(Config $config): Cache
1693+
public static function create(string $type, string $prefix, string $config): Cache
17041694
{
1705-
switch ($config->getCacheType()) {
1695+
switch ($type) {
17061696
case 'TempFile':
1707-
$cache = new TempFileCache(self::getPrefix($config), $config->getCachePath());
1697+
$cache = new TempFileCache($prefix, $config);
17081698
break;
17091699
case 'Redis':
1710-
$cache = new RedisCache(self::getPrefix($config), $config->getCachePath());
1700+
$cache = new RedisCache($prefix, $config);
17111701
break;
17121702
case 'Memcache':
1713-
$cache = new MemcacheCache(self::getPrefix($config), $config->getCachePath());
1703+
$cache = new MemcacheCache($prefix, $config);
17141704
break;
17151705
case 'Memcached':
1716-
$cache = new MemcachedCache(self::getPrefix($config), $config->getCachePath());
1706+
$cache = new MemcachedCache($prefix, $config);
17171707
break;
17181708
default:
17191709
$cache = new NoCache();
@@ -2834,6 +2824,25 @@ public function read(ServerRequestInterface $request): ResponseInterface
28342824

28352825
}
28362826

2827+
// file: src/Tqdev/PhpCrudApi/Controller/JsonResponder.php
2828+
2829+
class JsonResponder implements Responder
2830+
{
2831+
public function error(int $error, string $argument, $details = null): ResponseInterface
2832+
{
2833+
$errorCode = new ErrorCode($error);
2834+
$status = $errorCode->getStatus();
2835+
$document = new ErrorDocument($errorCode, $argument, $details);
2836+
return ResponseFactory::fromObject($status, $document);
2837+
}
2838+
2839+
public function success($result): ResponseInterface
2840+
{
2841+
return ResponseFactory::fromObject(ResponseFactory::OK, $result);
2842+
}
2843+
2844+
}
2845+
28372846
// file: src/Tqdev/PhpCrudApi/Controller/OpenApiController.php
28382847

28392848
class OpenApiController
@@ -3029,20 +3038,11 @@ public function increment(ServerRequestInterface $request): ResponseInterface
30293038

30303039
// file: src/Tqdev/PhpCrudApi/Controller/Responder.php
30313040

3032-
class Responder
3041+
interface Responder
30333042
{
3034-
public function error(int $error, string $argument, $details = null): ResponseInterface
3035-
{
3036-
$errorCode = new ErrorCode($error);
3037-
$status = $errorCode->getStatus();
3038-
$document = new ErrorDocument($errorCode, $argument, $details);
3039-
return ResponseFactory::fromObject($status, $document);
3040-
}
3043+
public function error(int $error, string $argument, $details = null): ResponseInterface;
30413044

3042-
public function success($result): ResponseInterface
3043-
{
3044-
return ResponseFactory::fromObject(ResponseFactory::OK, $result);
3045-
}
3045+
public function success($result): ResponseInterface;
30463046

30473047
}
30483048

@@ -4793,7 +4793,7 @@ class SimpleRouter implements Router
47934793

47944794
public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl, bool $debug)
47954795
{
4796-
$this->basePath = $basePath;
4796+
$this->basePath = $this->detectBasePath($basePath);
47974797
$this->responder = $responder;
47984798
$this->cache = $cache;
47994799
$this->ttl = $ttl;
@@ -4804,6 +4804,21 @@ public function __construct(string $basePath, Responder $responder, Cache $cache
48044804
$this->middlewares = array();
48054805
}
48064806

4807+
private function detectBasePath(string $basePath): string
4808+
{
4809+
if ($basePath) {
4810+
return $basePath;
4811+
}
4812+
if (isset($_SERVER['PATH_INFO'])) {
4813+
$fullPath = array_shift(explode('?',$_SERVER['REQUEST_URI']));
4814+
$path = $_SERVER['PATH_INFO'];
4815+
if (substr($fullPath, -1*strlen($path)) == $path) {
4816+
return substr($fullPath, 0, -1*strlen($path));
4817+
}
4818+
}
4819+
return '';
4820+
}
4821+
48074822
private function loadPathTree(): PathTree
48084823
{
48094824
$data = $this->cache->get('PathTree');
@@ -4869,6 +4884,11 @@ private function removeBasePath(ServerRequestInterface $request): ServerRequestI
48694884
return $request;
48704885
}
48714886

4887+
public function getBasePath(): string
4888+
{
4889+
return $this->basePath;
4890+
}
4891+
48724892
public function handle(ServerRequestInterface $request): ResponseInterface
48734893
{
48744894
$request = $this->removeBasePath($request);
@@ -7354,9 +7374,10 @@ public function __construct(Config $config)
73547374
$config->getUsername(),
73557375
$config->getPassword()
73567376
);
7357-
$cache = CacheFactory::create($config);
7377+
$prefix = sprintf('phpcrudapi-%s-%s-%s-', $config->getDriver(), $config->getDatabase(), substr(md5(__FILE__), 0, 8));
7378+
$cache = CacheFactory::create($config->getCacheType(), $prefix, $config->getCachePath());
73587379
$reflection = new ReflectionService($db, $cache, $config->getCacheTime());
7359-
$responder = new Responder();
7380+
$responder = new JsonResponder();
73607381
$router = new SimpleRouter($config->getBasePath(), $responder, $cache, $config->getCacheTime(), $config->getDebug());
73617382
foreach ($config->getMiddlewares() as $middleware => $properties) {
73627383
switch ($middleware) {
@@ -7784,15 +7805,25 @@ class ResponseFactory
77847805
const UNPROCESSABLE_ENTITY = 422;
77857806
const INTERNAL_SERVER_ERROR = 500;
77867807

7808+
public static function fromHtml(int $status, string $html): ResponseInterface
7809+
{
7810+
return self::from($status, 'text/html', $html);
7811+
}
7812+
77877813
public static function fromObject(int $status, $body): ResponseInterface
7814+
{
7815+
$content = json_encode($body, JSON_UNESCAPED_UNICODE);
7816+
return self::from($status, 'application/json', $content);
7817+
}
7818+
7819+
private static function from(int $status, string $contentType, string $content): ResponseInterface
77887820
{
77897821
$psr17Factory = new Psr17Factory();
77907822
$response = $psr17Factory->createResponse($status);
7791-
$content = json_encode($body, JSON_UNESCAPED_UNICODE);
77927823
$stream = $psr17Factory->createStream($content);
77937824
$stream->rewind();
77947825
$response = $response->withBody($stream);
7795-
$response = $response->withHeader('Content-Type', 'application/json');
7826+
$response = $response->withHeader('Content-Type', $contentType);
77967827
$response = $response->withHeader('Content-Length', strlen($content));
77977828
return $response;
77987829
}
@@ -7858,7 +7889,6 @@ public static function toString(ResponseInterface $response): string
78587889
'username' => 'php-crud-api',
78597890
'password' => 'php-crud-api',
78607891
'database' => 'php-crud-api',
7861-
'debug' => false,
78627892
]);
78637893
$request = RequestFactory::fromGlobals();
78647894
$api = new Api($config);

0 commit comments

Comments
 (0)