Skip to content

Commit 02b6847

Browse files
committed
refactor for re-use
1 parent 7ebd31b commit 02b6847

File tree

5 files changed

+49
-38
lines changed

5 files changed

+49
-38
lines changed

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use Tqdev\PhpCrudApi\Controller\CacheController;
1111
use Tqdev\PhpCrudApi\Controller\ColumnController;
1212
use Tqdev\PhpCrudApi\Controller\GeoJsonController;
13+
use Tqdev\PhpCrudApi\Controller\JsonResponder;
1314
use Tqdev\PhpCrudApi\Controller\OpenApiController;
1415
use Tqdev\PhpCrudApi\Controller\RecordController;
15-
use Tqdev\PhpCrudApi\Controller\Responder;
1616
use Tqdev\PhpCrudApi\Database\GenericDB;
1717
use Tqdev\PhpCrudApi\GeoJson\GeoJsonService;
1818
use Tqdev\PhpCrudApi\Middleware\AuthorizationMiddleware;
@@ -51,9 +51,10 @@ public function __construct(Config $config)
5151
$config->getUsername(),
5252
$config->getPassword()
5353
);
54-
$cache = CacheFactory::create($config);
54+
$prefix = sprintf('phpcrudapi-%s-%s-%s-', $config->getDriver(), $config->getDatabase(), substr(md5(__FILE__), 0, 8));
55+
$cache = CacheFactory::create($config->getCacheType(), $prefix, $config->getCachePath());
5556
$reflection = new ReflectionService($db, $cache, $config->getCacheTime());
56-
$responder = new Responder();
57+
$responder = new JsonResponder();
5758
$router = new SimpleRouter($config->getBasePath(), $responder, $cache, $config->getCacheTime(), $config->getDebug());
5859
foreach ($config->getMiddlewares() as $middleware => $properties) {
5960
switch ($middleware) {

src/Tqdev/PhpCrudApi/Cache/CacheFactory.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
11
<?php
22
namespace Tqdev\PhpCrudApi\Cache;
33

4-
use Tqdev\PhpCrudApi\Config;
5-
64
class CacheFactory
75
{
8-
const PREFIX = 'phpcrudapi-%s-%s-%s-';
9-
10-
private static function getPrefix(Config $config): string
11-
{
12-
$driver = $config->getDriver();
13-
$database = $config->getDatabase();
14-
$filehash = substr(md5(__FILE__), 0, 8);
15-
return sprintf(self::PREFIX, $driver, $database, $filehash);
16-
}
17-
18-
public static function create(Config $config): Cache
6+
public static function create(string $type, string $prefix, string $config): Cache
197
{
20-
switch ($config->getCacheType()) {
8+
switch ($type) {
219
case 'TempFile':
22-
$cache = new TempFileCache(self::getPrefix($config), $config->getCachePath());
10+
$cache = new TempFileCache($prefix, $config);
2311
break;
2412
case 'Redis':
25-
$cache = new RedisCache(self::getPrefix($config), $config->getCachePath());
13+
$cache = new RedisCache($prefix, $config);
2614
break;
2715
case 'Memcache':
28-
$cache = new MemcacheCache(self::getPrefix($config), $config->getCachePath());
16+
$cache = new MemcacheCache($prefix, $config);
2917
break;
3018
case 'Memcached':
31-
$cache = new MemcachedCache(self::getPrefix($config), $config->getCachePath());
19+
$cache = new MemcachedCache($prefix, $config);
3220
break;
3321
default:
3422
$cache = new NoCache();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace Tqdev\PhpCrudApi\Controller;
3+
4+
use Psr\Http\Message\ResponseInterface;
5+
use Tqdev\PhpCrudApi\Record\Document\ErrorDocument;
6+
use Tqdev\PhpCrudApi\Record\ErrorCode;
7+
use Tqdev\PhpCrudApi\ResponseFactory;
8+
9+
class JsonResponder implements Responder
10+
{
11+
public function error(int $error, string $argument, $details = null): ResponseInterface
12+
{
13+
$errorCode = new ErrorCode($error);
14+
$status = $errorCode->getStatus();
15+
$document = new ErrorDocument($errorCode, $argument, $details);
16+
return ResponseFactory::fromObject($status, $document);
17+
}
18+
19+
public function success($result): ResponseInterface
20+
{
21+
return ResponseFactory::fromObject(ResponseFactory::OK, $result);
22+
}
23+
24+
}

src/Tqdev/PhpCrudApi/Controller/Responder.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,11 @@
22
namespace Tqdev\PhpCrudApi\Controller;
33

44
use Psr\Http\Message\ResponseInterface;
5-
use Tqdev\PhpCrudApi\Record\Document\ErrorDocument;
6-
use Tqdev\PhpCrudApi\Record\ErrorCode;
7-
use Tqdev\PhpCrudApi\ResponseFactory;
85

9-
class Responder
6+
interface Responder
107
{
11-
public function error(int $error, string $argument, $details = null): ResponseInterface
12-
{
13-
$errorCode = new ErrorCode($error);
14-
$status = $errorCode->getStatus();
15-
$document = new ErrorDocument($errorCode, $argument, $details);
16-
return ResponseFactory::fromObject($status, $document);
17-
}
8+
public function error(int $error, string $argument, $details = null): ResponseInterface;
189

19-
public function success($result): ResponseInterface
20-
{
21-
return ResponseFactory::fromObject(ResponseFactory::OK, $result);
22-
}
10+
public function success($result): ResponseInterface;
2311

2412
}

src/Tqdev/PhpCrudApi/ResponseFactory.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,25 @@ class ResponseFactory
1515
const UNPROCESSABLE_ENTITY = 422;
1616
const INTERNAL_SERVER_ERROR = 500;
1717

18+
public static function fromHtml(int $status, string $html): ResponseInterface
19+
{
20+
return self::from($status, 'text/html', $html);
21+
}
22+
1823
public static function fromObject(int $status, $body): ResponseInterface
24+
{
25+
$content = json_encode($body, JSON_UNESCAPED_UNICODE);
26+
return self::from($status, 'application/json', $content);
27+
}
28+
29+
private static function from(int $status, string $contentType, string $content): ResponseInterface
1930
{
2031
$psr17Factory = new Psr17Factory();
2132
$response = $psr17Factory->createResponse($status);
22-
$content = json_encode($body, JSON_UNESCAPED_UNICODE);
2333
$stream = $psr17Factory->createStream($content);
2434
$stream->rewind();
2535
$response = $response->withBody($stream);
26-
$response = $response->withHeader('Content-Type', 'application/json');
36+
$response = $response->withHeader('Content-Type', $contentType);
2737
$response = $response->withHeader('Content-Length', strlen($content));
2838
return $response;
2939
}

0 commit comments

Comments
 (0)