@@ -5555,6 +5555,7 @@ class GenericDB
5555
5555
private $ address ;
5556
5556
private $ port ;
5557
5557
private $ database ;
5558
+ private $ command ;
5558
5559
private $ tables ;
5559
5560
private $ mapping ;
5560
5561
private $ username ;
@@ -5585,22 +5586,30 @@ private function getCommands(): array
5585
5586
{
5586
5587
switch ($ this ->driver ) {
5587
5588
case 'mysql ' :
5588
- return [
5589
+ $ commands = [
5589
5590
'SET SESSION sql_warnings=1; ' ,
5590
5591
'SET NAMES utf8mb4; ' ,
5591
5592
'SET SESSION sql_mode = "ANSI,TRADITIONAL"; ' ,
5592
5593
];
5594
+ break ;
5593
5595
case 'pgsql ' :
5594
- return [
5596
+ $ commands = [
5595
5597
"SET NAMES 'UTF8'; " ,
5596
5598
];
5599
+ break ;
5597
5600
case 'sqlsrv ' :
5598
- return [];
5601
+ $ commands = [];
5602
+ break ;
5599
5603
case 'sqlite ' :
5600
- return [
5604
+ $ commands = [
5601
5605
'PRAGMA foreign_keys = on; ' ,
5602
5606
];
5607
+ break ;
5603
5608
}
5609
+ if ($ this ->command != '' ) {
5610
+ $ commands [] = $ this ->command ;
5611
+ }
5612
+ return $ commands ;
5604
5613
}
5605
5614
5606
5615
private function getOptions (): array
@@ -5647,20 +5656,21 @@ private function initPdo(): bool
5647
5656
return $ result ;
5648
5657
}
5649
5658
5650
- public function __construct (string $ driver , string $ address , int $ port , string $ database , array $ tables , array $ mapping , string $ username , string $ password )
5659
+ public function __construct (string $ driver , string $ address , int $ port , string $ database , string $ command , array $ tables , array $ mapping , string $ username , string $ password )
5651
5660
{
5652
5661
$ this ->driver = $ driver ;
5653
5662
$ this ->address = $ address ;
5654
5663
$ this ->port = $ port ;
5655
5664
$ this ->database = $ database ;
5665
+ $ this ->command = $ command ;
5656
5666
$ this ->tables = $ tables ;
5657
5667
$ this ->mapping = $ mapping ;
5658
5668
$ this ->username = $ username ;
5659
5669
$ this ->password = $ password ;
5660
5670
$ this ->initPdo ();
5661
5671
}
5662
5672
5663
- public function reconstruct (string $ driver , string $ address , int $ port , string $ database , array $ tables , array $ mapping , string $ username , string $ password ): bool
5673
+ public function reconstruct (string $ driver , string $ address , int $ port , string $ database , string $ command , array $ tables , array $ mapping , string $ username , string $ password ): bool
5664
5674
{
5665
5675
if ($ driver ) {
5666
5676
$ this ->driver = $ driver ;
@@ -5674,6 +5684,9 @@ public function reconstruct(string $driver, string $address, int $port, string $
5674
5684
if ($ database ) {
5675
5685
$ this ->database = $ database ;
5676
5686
}
5687
+ if ($ command ) {
5688
+ $ this ->command = $ command ;
5689
+ }
5677
5690
if ($ tables ) {
5678
5691
$ this ->tables = $ tables ;
5679
5692
}
@@ -8832,6 +8845,15 @@ private function getDatabase(): string
8832
8845
return '' ;
8833
8846
}
8834
8847
8848
+ private function getCommand (): string
8849
+ {
8850
+ $ commandHandler = $ this ->getProperty ('commandHandler ' , '' );
8851
+ if ($ commandHandler ) {
8852
+ return call_user_func ($ commandHandler );
8853
+ }
8854
+ return '' ;
8855
+ }
8856
+
8835
8857
private function getTables (): array
8836
8858
{
8837
8859
$ tablesHandler = $ this ->getProperty ('tablesHandler ' , '' );
@@ -8874,12 +8896,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
8874
8896
$ address = $ this ->getAddress ();
8875
8897
$ port = $ this ->getPort ();
8876
8898
$ database = $ this ->getDatabase ();
8899
+ $ command = $ this ->getCommand ();
8877
8900
$ tables = $ this ->getTables ();
8878
8901
$ mapping = $ this ->getMapping ();
8879
8902
$ username = $ this ->getUsername ();
8880
8903
$ password = $ this ->getPassword ();
8881
- if ($ driver || $ address || $ port || $ database || $ tables || $ mapping || $ username || $ password ) {
8882
- $ this ->db ->reconstruct ($ driver , $ address , $ port , $ database , $ tables , $ mapping , $ username , $ password );
8904
+ if ($ driver || $ address || $ port || $ database || $ command || $ tables || $ mapping || $ username || $ password ) {
8905
+ $ this ->db ->reconstruct ($ driver , $ address , $ port , $ database , $ command , $ tables , $ mapping , $ username , $ password );
8883
8906
}
8884
8907
return $ next ->handle ($ request );
8885
8908
}
@@ -11529,6 +11552,7 @@ public function __construct(Config $config)
11529
11552
$ config ->getAddress (),
11530
11553
$ config ->getPort (),
11531
11554
$ config ->getDatabase (),
11555
+ $ config ->getCommand (),
11532
11556
$ config ->getTables (),
11533
11557
$ config ->getMapping (),
11534
11558
$ config ->getUsername (),
@@ -11601,7 +11625,7 @@ public function __construct(Config $config)
11601
11625
case 'json ' :
11602
11626
new JsonMiddleware ($ router , $ responder , $ properties );
11603
11627
break ;
11604
- }
11628
+ }
11605
11629
}
11606
11630
foreach ($ config ->getControllers () as $ controller ) {
11607
11631
switch ($ controller ) {
@@ -11627,7 +11651,7 @@ public function __construct(Config $config)
11627
11651
break ;
11628
11652
case 'status ' :
11629
11653
new StatusController ($ router , $ responder , $ cache , $ db );
11630
- break ;
11654
+ break ;
11631
11655
}
11632
11656
}
11633
11657
foreach ($ config ->getCustomControllers () as $ className ) {
@@ -11714,6 +11738,7 @@ class Config
11714
11738
'username ' => '' ,
11715
11739
'password ' => '' ,
11716
11740
'database ' => '' ,
11741
+ 'command ' => '' ,
11717
11742
'tables ' => '' ,
11718
11743
'mapping ' => '' ,
11719
11744
'middlewares ' => 'cors,errors ' ,
@@ -11781,15 +11806,15 @@ private function applyEnvironmentVariables(array $values, string $prefix = 'PHP_
11781
11806
$ suffix = strtoupper (preg_replace ('/(?<!^)[A-Z]/ ' , '_$0 ' , str_replace ('. ' , '_ ' , $ key )));
11782
11807
$ newPrefix = $ prefix . "_ " . $ suffix ;
11783
11808
if (is_array ($ value )) {
11784
- $ newPrefix = str_replace ('PHP_CRUD_API_MIDDLEWARES_ ' ,'PHP_CRUD_API_ ' ,$ newPrefix );
11809
+ $ newPrefix = str_replace ('PHP_CRUD_API_MIDDLEWARES_ ' , 'PHP_CRUD_API_ ' , $ newPrefix );
11785
11810
$ result [$ key ] = $ this ->applyEnvironmentVariables ($ value , $ newPrefix );
11786
11811
} else {
11787
11812
$ result [$ key ] = getenv ($ newPrefix , true ) ?: $ value ;
11788
11813
}
11789
11814
}
11790
11815
return $ result ;
11791
11816
}
11792
-
11817
+
11793
11818
public function __construct (array $ values )
11794
11819
{
11795
11820
$ driver = $ this ->getDefaultDriver ($ values );
@@ -11860,15 +11885,22 @@ public function getDatabase(): string
11860
11885
return $ this ->values ['database ' ];
11861
11886
}
11862
11887
11888
+ public function getCommand (): string
11889
+ {
11890
+ return $ this ->values ['command ' ];
11891
+ }
11892
+
11863
11893
public function getTables (): array
11864
11894
{
11865
11895
return array_filter (array_map ('trim ' , explode (', ' , $ this ->values ['tables ' ])));
11866
11896
}
11867
11897
11868
11898
public function getMapping (): array
11869
11899
{
11870
- $ mapping = array_map (function ($ v ){ return explode ('= ' , $ v ); }, array_filter (array_map ('trim ' , explode (', ' , $ this ->values ['mapping ' ]))));
11871
- return array_combine (array_column ($ mapping ,0 ),array_column ($ mapping ,1 ));
11900
+ $ mapping = array_map (function ($ v ) {
11901
+ return explode ('= ' , $ v );
11902
+ }, array_filter (array_map ('trim ' , explode (', ' , $ this ->values ['mapping ' ]))));
11903
+ return array_combine (array_column ($ mapping , 0 ), array_column ($ mapping , 1 ));
11872
11904
}
11873
11905
11874
11906
public function getMiddlewares (): array
0 commit comments