@@ -3226,6 +3226,41 @@ public static function getHeadersFromServer(array $server): array;
3226
3226
}
3227
3227
}
3228
3228
3229
+ // file: src/Tqdev/PhpCrudApi/Cache/Base/BaseCache.php
3230
+ namespace Tqdev \PhpCrudApi \Cache \Base {
3231
+
3232
+ use Tqdev \PhpCrudApi \Cache \Cache ;
3233
+
3234
+ class BaseCache implements Cache
3235
+ {
3236
+ public function __construct ()
3237
+ {
3238
+ }
3239
+
3240
+ public function set (string $ key , string $ value , int $ ttl = 0 ): bool
3241
+ {
3242
+ return true ;
3243
+ }
3244
+
3245
+ public function get (string $ key ): string
3246
+ {
3247
+ return '' ;
3248
+ }
3249
+
3250
+ public function clear (): bool
3251
+ {
3252
+ return true ;
3253
+ }
3254
+
3255
+ public function ping (): int
3256
+ {
3257
+ $ start = microtime (true );
3258
+ $ this ->get ('__ping__ ' );
3259
+ return intval ((microtime (true )-$ start )*1000000 );
3260
+ }
3261
+ }
3262
+ }
3263
+
3229
3264
// file: src/Tqdev/PhpCrudApi/Cache/Cache.php
3230
3265
namespace Tqdev \PhpCrudApi \Cache {
3231
3266
@@ -3234,6 +3269,7 @@ interface Cache
3234
3269
public function set (string $ key , string $ value , int $ ttl = 0 ): bool ;
3235
3270
public function get (string $ key ): string ;
3236
3271
public function clear (): bool ;
3272
+ public function ping (): int ;
3237
3273
}
3238
3274
}
3239
3275
@@ -3268,7 +3304,9 @@ public static function create(string $type, string $prefix, string $config): Cac
3268
3304
// file: src/Tqdev/PhpCrudApi/Cache/MemcacheCache.php
3269
3305
namespace Tqdev \PhpCrudApi \Cache {
3270
3306
3271
- class MemcacheCache implements Cache
3307
+ use Tqdev \PhpCrudApi \Cache \Base \BaseCache ;
3308
+
3309
+ class MemcacheCache extends BaseCache implements Cache
3272
3310
{
3273
3311
protected $ prefix ;
3274
3312
protected $ memcache ;
@@ -3331,33 +3369,19 @@ public function set(string $key, string $value, int $ttl = 0): bool
3331
3369
// file: src/Tqdev/PhpCrudApi/Cache/NoCache.php
3332
3370
namespace Tqdev \PhpCrudApi \Cache {
3333
3371
3334
- class NoCache implements Cache
3335
- {
3336
- public function __construct ()
3337
- {
3338
- }
3339
-
3340
- public function set (string $ key , string $ value , int $ ttl = 0 ): bool
3341
- {
3342
- return true ;
3343
- }
3372
+ use Tqdev \PhpCrudApi \Cache \Base \BaseCache ;
3344
3373
3345
- public function get (string $ key ): string
3346
- {
3347
- return '' ;
3348
- }
3349
-
3350
- public function clear (): bool
3351
- {
3352
- return true ;
3353
- }
3374
+ class NoCache extends BaseCache implements Cache
3375
+ {
3354
3376
}
3355
3377
}
3356
3378
3357
3379
// file: src/Tqdev/PhpCrudApi/Cache/RedisCache.php
3358
3380
namespace Tqdev \PhpCrudApi \Cache {
3359
3381
3360
- class RedisCache implements Cache
3382
+ use Tqdev \PhpCrudApi \Cache \Base \BaseCache ;
3383
+
3384
+ class RedisCache extends BaseCache implements Cache
3361
3385
{
3362
3386
protected $ prefix ;
3363
3387
protected $ redis ;
@@ -3396,7 +3420,9 @@ public function clear(): bool
3396
3420
// file: src/Tqdev/PhpCrudApi/Cache/TempFileCache.php
3397
3421
namespace Tqdev \PhpCrudApi \Cache {
3398
3422
3399
- class TempFileCache implements Cache
3423
+ use Tqdev \PhpCrudApi \Cache \Base \BaseCache ;
3424
+
3425
+ class TempFileCache extends BaseCache implements Cache
3400
3426
{
3401
3427
const SUFFIX = 'cache ' ;
3402
3428
@@ -4820,6 +4846,56 @@ interface Responder
4820
4846
public function error (int $ error , string $ argument , $ details = null ): ResponseInterface ;
4821
4847
4822
4848
public function success ($ result ): ResponseInterface ;
4849
+
4850
+ public function multi ($ results ): ResponseInterface ;
4851
+
4852
+ public function exception ($ exception ): ResponseInterface ;
4853
+
4854
+ }
4855
+ }
4856
+
4857
+ // file: src/Tqdev/PhpCrudApi/Controller/StatusController.php
4858
+ namespace Tqdev \PhpCrudApi \Controller {
4859
+
4860
+ use Psr \Http \Message \ResponseInterface ;
4861
+ use Psr \Http \Message \ServerRequestInterface ;
4862
+ use Tqdev \PhpCrudApi \Cache \Cache ;
4863
+ use Tqdev \PhpCrudApi \Database \GenericDB ;
4864
+ use Tqdev \PhpCrudApi \Middleware \Router \Router ;
4865
+
4866
+ class StatusController
4867
+ {
4868
+ private $ db ;
4869
+ private $ cache ;
4870
+ private $ responder ;
4871
+
4872
+ public function __construct (Router $ router , Responder $ responder , Cache $ cache , GenericDB $ db )
4873
+ {
4874
+ $ router ->register ('GET ' , '/status/up ' , array ($ this , 'up ' ));
4875
+ $ router ->register ('GET ' , '/status/ping ' , array ($ this , 'ping ' ));
4876
+ $ this ->db = $ db ;
4877
+ $ this ->cache = $ cache ;
4878
+ $ this ->responder = $ responder ;
4879
+ }
4880
+
4881
+ public function up (ServerRequestInterface $ request ): ResponseInterface
4882
+ {
4883
+ $ result = [
4884
+ 'db ' => $ this ->db ->ping ()<1000000 ,
4885
+ 'cache ' => $ this ->cache ->ping ()<1000000 ,
4886
+ ];
4887
+ return $ this ->responder ->success ($ result );
4888
+ }
4889
+
4890
+ public function ping (ServerRequestInterface $ request ): ResponseInterface
4891
+ {
4892
+ $ result = [
4893
+ 'db ' => $ this ->db ->ping (),
4894
+ 'cache ' => $ this ->cache ->ping (),
4895
+ ];
4896
+ return $ this ->responder ->success ($ result );
4897
+ }
4898
+
4823
4899
}
4824
4900
}
4825
4901
@@ -5682,6 +5758,14 @@ private function query(string $sql, array $parameters): \PDOStatement
5682
5758
return $ stmt ;
5683
5759
}
5684
5760
5761
+ public function ping (): int
5762
+ {
5763
+ $ start = microtime (true );
5764
+ $ stmt = $ this ->pdo ->prepare ('SELECT 1 ' );
5765
+ $ stmt ->execute ();
5766
+ return intval ((microtime (true )-$ start )*1000000 );
5767
+ }
5768
+
5685
5769
public function getCacheKey (): string
5686
5770
{
5687
5771
return md5 (json_encode ([
@@ -10519,6 +10603,11 @@ public function _list(string $tableName, array $params): ListDocument
10519
10603
$ this ->joiner ->addJoins ($ table , $ records , $ params , $ this ->db );
10520
10604
return new ListDocument ($ records , $ count );
10521
10605
}
10606
+
10607
+ public function ping (): int
10608
+ {
10609
+ return $ this ->db ->ping ();
10610
+ }
10522
10611
}
10523
10612
}
10524
10613
@@ -10834,6 +10923,7 @@ private function setHabtmValues(ReflectedTable $t1, ReflectedTable $t2, array &$
10834
10923
use Tqdev \PhpCrudApi \Controller \JsonResponder ;
10835
10924
use Tqdev \PhpCrudApi \Controller \OpenApiController ;
10836
10925
use Tqdev \PhpCrudApi \Controller \RecordController ;
10926
+ use Tqdev \PhpCrudApi \Controller \StatusController ;
10837
10927
use Tqdev \PhpCrudApi \Database \GenericDB ;
10838
10928
use Tqdev \PhpCrudApi \GeoJson \GeoJsonService ;
10839
10929
use Tqdev \PhpCrudApi \Middleware \AuthorizationMiddleware ;
@@ -10958,6 +11048,9 @@ public function __construct(Config $config)
10958
11048
$ geoJson = new GeoJsonService ($ reflection , $ records );
10959
11049
new GeoJsonController ($ router , $ responder , $ geoJson );
10960
11050
break ;
11051
+ case 'status ' :
11052
+ new StatusController ($ router , $ responder , $ cache , $ db );
11053
+ break ;
10961
11054
}
10962
11055
}
10963
11056
foreach ($ config ->getCustomControllers () as $ className ) {
@@ -11049,7 +11142,7 @@ class Config
11049
11142
'database ' => null ,
11050
11143
'tables ' => '' ,
11051
11144
'middlewares ' => 'cors,errors ' ,
11052
- 'controllers ' => 'records,geojson,openapi ' ,
11145
+ 'controllers ' => 'records,geojson,openapi,status ' ,
11053
11146
'customControllers ' => '' ,
11054
11147
'customOpenApiBuilders ' => '' ,
11055
11148
'cacheType ' => 'TempFile ' ,
0 commit comments