@@ -1690,30 +1690,20 @@ public function clear(): bool;
1690
1690
1691
1691
class CacheFactory
1692
1692
{
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
1704
1694
{
1705
- switch ($ config -> getCacheType () ) {
1695
+ switch ($ type ) {
1706
1696
case 'TempFile ' :
1707
- $ cache = new TempFileCache (self :: getPrefix ( $ config ) , $ config-> getCachePath () );
1697
+ $ cache = new TempFileCache ($ prefix , $ config );
1708
1698
break ;
1709
1699
case 'Redis ' :
1710
- $ cache = new RedisCache (self :: getPrefix ( $ config ) , $ config-> getCachePath () );
1700
+ $ cache = new RedisCache ($ prefix , $ config );
1711
1701
break ;
1712
1702
case 'Memcache ' :
1713
- $ cache = new MemcacheCache (self :: getPrefix ( $ config ) , $ config-> getCachePath () );
1703
+ $ cache = new MemcacheCache ($ prefix , $ config );
1714
1704
break ;
1715
1705
case 'Memcached ' :
1716
- $ cache = new MemcachedCache (self :: getPrefix ( $ config ) , $ config-> getCachePath () );
1706
+ $ cache = new MemcachedCache ($ prefix , $ config );
1717
1707
break ;
1718
1708
default :
1719
1709
$ cache = new NoCache ();
@@ -2834,6 +2824,25 @@ public function read(ServerRequestInterface $request): ResponseInterface
2834
2824
2835
2825
}
2836
2826
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
+
2837
2846
// file: src/Tqdev/PhpCrudApi/Controller/OpenApiController.php
2838
2847
2839
2848
class OpenApiController
@@ -3029,20 +3038,11 @@ public function increment(ServerRequestInterface $request): ResponseInterface
3029
3038
3030
3039
// file: src/Tqdev/PhpCrudApi/Controller/Responder.php
3031
3040
3032
- class Responder
3041
+ interface Responder
3033
3042
{
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 ;
3041
3044
3042
- public function success ($ result ): ResponseInterface
3043
- {
3044
- return ResponseFactory::fromObject (ResponseFactory::OK , $ result );
3045
- }
3045
+ public function success ($ result ): ResponseInterface ;
3046
3046
3047
3047
}
3048
3048
@@ -4793,7 +4793,7 @@ class SimpleRouter implements Router
4793
4793
4794
4794
public function __construct (string $ basePath , Responder $ responder , Cache $ cache , int $ ttl , bool $ debug )
4795
4795
{
4796
- $ this ->basePath = $ basePath ;
4796
+ $ this ->basePath = $ this -> detectBasePath ( $ basePath) ;
4797
4797
$ this ->responder = $ responder ;
4798
4798
$ this ->cache = $ cache ;
4799
4799
$ this ->ttl = $ ttl ;
@@ -4804,6 +4804,21 @@ public function __construct(string $basePath, Responder $responder, Cache $cache
4804
4804
$ this ->middlewares = array ();
4805
4805
}
4806
4806
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
+
4807
4822
private function loadPathTree (): PathTree
4808
4823
{
4809
4824
$ data = $ this ->cache ->get ('PathTree ' );
@@ -4869,6 +4884,11 @@ private function removeBasePath(ServerRequestInterface $request): ServerRequestI
4869
4884
return $ request ;
4870
4885
}
4871
4886
4887
+ public function getBasePath (): string
4888
+ {
4889
+ return $ this ->basePath ;
4890
+ }
4891
+
4872
4892
public function handle (ServerRequestInterface $ request ): ResponseInterface
4873
4893
{
4874
4894
$ request = $ this ->removeBasePath ($ request );
@@ -7354,9 +7374,10 @@ public function __construct(Config $config)
7354
7374
$ config ->getUsername (),
7355
7375
$ config ->getPassword ()
7356
7376
);
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 ());
7358
7379
$ reflection = new ReflectionService ($ db , $ cache , $ config ->getCacheTime ());
7359
- $ responder = new Responder ();
7380
+ $ responder = new JsonResponder ();
7360
7381
$ router = new SimpleRouter ($ config ->getBasePath (), $ responder , $ cache , $ config ->getCacheTime (), $ config ->getDebug ());
7361
7382
foreach ($ config ->getMiddlewares () as $ middleware => $ properties ) {
7362
7383
switch ($ middleware ) {
@@ -7784,15 +7805,25 @@ class ResponseFactory
7784
7805
const UNPROCESSABLE_ENTITY = 422 ;
7785
7806
const INTERNAL_SERVER_ERROR = 500 ;
7786
7807
7808
+ public static function fromHtml (int $ status , string $ html ): ResponseInterface
7809
+ {
7810
+ return self ::from ($ status , 'text/html ' , $ html );
7811
+ }
7812
+
7787
7813
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
7788
7820
{
7789
7821
$ psr17Factory = new Psr17Factory ();
7790
7822
$ response = $ psr17Factory ->createResponse ($ status );
7791
- $ content = json_encode ($ body , JSON_UNESCAPED_UNICODE );
7792
7823
$ stream = $ psr17Factory ->createStream ($ content );
7793
7824
$ stream ->rewind ();
7794
7825
$ response = $ response ->withBody ($ stream );
7795
- $ response = $ response ->withHeader ('Content-Type ' , ' application/json ' );
7826
+ $ response = $ response ->withHeader ('Content-Type ' , $ contentType );
7796
7827
$ response = $ response ->withHeader ('Content-Length ' , strlen ($ content ));
7797
7828
return $ response ;
7798
7829
}
@@ -7858,7 +7889,6 @@ public static function toString(ResponseInterface $response): string
7858
7889
'username ' => 'php-crud-api ' ,
7859
7890
'password ' => 'php-crud-api ' ,
7860
7891
'database ' => 'php-crud-api ' ,
7861
- 'debug ' => false ,
7862
7892
]);
7863
7893
$ request = RequestFactory::fromGlobals ();
7864
7894
$ api = new Api ($ config );
0 commit comments