@@ -5171,23 +5171,27 @@ public function convertColumnValues(ReflectedTable $table, array &$columnValues)
5171
5171
class GenericDB
5172
5172
{
5173
5173
private $ driver ;
5174
+ private $ address ;
5175
+ private $ port ;
5174
5176
private $ database ;
5177
+ private $ username ;
5178
+ private $ password ;
5175
5179
private $ pdo ;
5176
5180
private $ reflection ;
5177
5181
private $ definition ;
5178
5182
private $ conditions ;
5179
5183
private $ columns ;
5180
5184
private $ converter ;
5181
5185
5182
- private function getDsn (string $ address , int $ port , string $ database ): string
5186
+ private function getDsn (): string
5183
5187
{
5184
5188
switch ($ this ->driver ) {
5185
5189
case 'mysql ' :
5186
- return "$ this ->driver :host= $ address;port= $ port;dbname= $ database;charset=utf8mb4 " ;
5190
+ return "$ this ->driver :host= $ this -> address ;port= $ this -> port ;dbname= $ this -> database ;charset=utf8mb4 " ;
5187
5191
case 'pgsql ' :
5188
- return "$ this ->driver :host= $ address port= $ port dbname= $ database options='--client_encoding=UTF8' " ;
5192
+ return "$ this ->driver :host= $ this -> address port= $ this -> port dbname= $ this -> database options='--client_encoding=UTF8' " ;
5189
5193
case 'sqlsrv ' :
5190
- return "$ this ->driver :Server= $ address, $ port;Database= $ database " ;
5194
+ return "$ this ->driver :Server= $ this -> address , $ this -> port ;Database= $ this -> database " ;
5191
5195
}
5192
5196
}
5193
5197
@@ -5235,22 +5239,58 @@ private function getOptions(): array
5235
5239
}
5236
5240
}
5237
5241
5238
- public function __construct ( string $ driver , string $ address , int $ port , string $ database , string $ username , string $ password )
5242
+ private function initPdo (): bool
5239
5243
{
5240
- $ this ->driver = $ driver ;
5241
- $ this ->database = $ database ;
5242
- $ dsn = $ this ->getDsn ($ address , $ port , $ database );
5243
- $ options = $ this ->getOptions ();
5244
- $ this ->pdo = new LazyPdo ($ dsn , $ username , $ password , $ options );
5244
+ if ($ this ->pdo ) {
5245
+ $ result = $ this ->pdo ->reconstruct ($ this ->getDsn (), $ this ->username , $ this ->password , $ this ->getOptions ());
5246
+ } else {
5247
+ $ this ->pdo = new LazyPdo ($ this ->getDsn (), $ this ->username , $ this ->password , $ this ->getOptions ());
5248
+ $ result = true ;
5249
+ }
5245
5250
$ commands = $ this ->getCommands ();
5246
5251
foreach ($ commands as $ command ) {
5247
5252
$ this ->pdo ->addInitCommand ($ command );
5248
5253
}
5249
- $ this ->reflection = new GenericReflection ($ this ->pdo , $ driver , $ database );
5250
- $ this ->definition = new GenericDefinition ($ this ->pdo , $ driver , $ database );
5251
- $ this ->conditions = new ConditionsBuilder ($ driver );
5252
- $ this ->columns = new ColumnsBuilder ($ driver );
5253
- $ this ->converter = new DataConverter ($ driver );
5254
+ $ this ->reflection = new GenericReflection ($ this ->pdo , $ this ->driver , $ this ->database );
5255
+ $ this ->definition = new GenericDefinition ($ this ->pdo , $ this ->driver , $ this ->database );
5256
+ $ this ->conditions = new ConditionsBuilder ($ this ->driver );
5257
+ $ this ->columns = new ColumnsBuilder ($ this ->driver );
5258
+ $ this ->converter = new DataConverter ($ this ->driver );
5259
+ return $ result ;
5260
+ }
5261
+
5262
+ public function __construct (string $ driver , string $ address , int $ port , string $ database , string $ username , string $ password )
5263
+ {
5264
+ $ this ->driver = $ driver ;
5265
+ $ this ->address = $ address ;
5266
+ $ this ->port = $ port ;
5267
+ $ this ->database = $ database ;
5268
+ $ this ->username = $ username ;
5269
+ $ this ->password = $ password ;
5270
+ $ this ->initPdo ();
5271
+ }
5272
+
5273
+ public function reconstruct (string $ driver , string $ address , int $ port , string $ database , string $ username , string $ password ): bool
5274
+ {
5275
+ if ($ driver ) {
5276
+ $ this ->driver = $ driver ;
5277
+ }
5278
+ if ($ address ) {
5279
+ $ this ->address = $ address ;
5280
+ }
5281
+ if ($ port ) {
5282
+ $ this ->port = $ port ;
5283
+ }
5284
+ if ($ database ) {
5285
+ $ this ->database = $ database ;
5286
+ }
5287
+ if ($ username ) {
5288
+ $ this ->username = $ username ;
5289
+ }
5290
+ if ($ password ) {
5291
+ $ this ->password = $ password ;
5292
+ }
5293
+ return $ this ->initPdo ();
5254
5294
}
5255
5295
5256
5296
public function pdo (): LazyPdo
@@ -6036,15 +6076,18 @@ private function pdo()
6036
6076
return $ this ->pdo ;
6037
6077
}
6038
6078
6039
- public function reauthenticate ( /*?string*/ $ user , /*?string*/ $ password ): bool
6079
+ public function reconstruct ( string $ dsn , /*?string*/ $ user = null , /*?string*/ $ password = null , array $ options = array () ): bool
6040
6080
{
6081
+ $ this ->dsn = $ dsn ;
6041
6082
$ this ->user = $ user ;
6042
6083
$ this ->password = $ password ;
6084
+ $ this ->options = $ options ;
6085
+ $ this ->commands = array ();
6043
6086
if ($ this ->pdo ) {
6044
6087
$ this ->pdo = null ;
6045
- return false ;
6088
+ return true ;
6046
6089
}
6047
- return true ;
6090
+ return false ;
6048
6091
}
6049
6092
6050
6093
public function inTransaction (): bool
@@ -7739,7 +7782,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
7739
7782
}
7740
7783
}
7741
7784
7742
- // file: src/Tqdev/PhpCrudApi/Middleware/ReAuthMiddleware .php
7785
+ // file: src/Tqdev/PhpCrudApi/Middleware/ReconnectMiddleware .php
7743
7786
namespace Tqdev \PhpCrudApi \Middleware {
7744
7787
7745
7788
use Psr \Http \Message \ResponseInterface ;
@@ -7751,7 +7794,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
7751
7794
use Tqdev \PhpCrudApi \Middleware \Base \Middleware ;
7752
7795
use Tqdev \PhpCrudApi \Middleware \Router \Router ;
7753
7796
7754
- class ReAuthMiddleware extends Middleware
7797
+ class ReconnectMiddleware extends Middleware
7755
7798
{
7756
7799
private $ reflection ;
7757
7800
private $ db ;
@@ -7763,6 +7806,42 @@ public function __construct(Router $router, Responder $responder, array $propert
7763
7806
$ this ->db = $ db ;
7764
7807
}
7765
7808
7809
+ private function getDriver (): string
7810
+ {
7811
+ $ driverHandler = $ this ->getProperty ('driverHandler ' , '' );
7812
+ if ($ driverHandler ) {
7813
+ return call_user_func ($ driverHandler );
7814
+ }
7815
+ return '' ;
7816
+ }
7817
+
7818
+ private function getAddress (): string
7819
+ {
7820
+ $ addressHandler = $ this ->getProperty ('addressHandler ' , '' );
7821
+ if ($ addressHandler ) {
7822
+ return call_user_func ($ addressHandler );
7823
+ }
7824
+ return '' ;
7825
+ }
7826
+
7827
+ private function getPort (): int
7828
+ {
7829
+ $ portHandler = $ this ->getProperty ('portHandler ' , '' );
7830
+ if ($ portHandler ) {
7831
+ return call_user_func ($ portHandler );
7832
+ }
7833
+ return 0 ;
7834
+ }
7835
+
7836
+ private function getDatabase (): string
7837
+ {
7838
+ $ databaseHandler = $ this ->getProperty ('databaseHandler ' , '' );
7839
+ if ($ databaseHandler ) {
7840
+ return call_user_func ($ databaseHandler );
7841
+ }
7842
+ return '' ;
7843
+ }
7844
+
7766
7845
private function getUsername (): string
7767
7846
{
7768
7847
$ usernameHandler = $ this ->getProperty ('usernameHandler ' , '' );
@@ -7783,10 +7862,14 @@ private function getPassword(): string
7783
7862
7784
7863
public function process (ServerRequestInterface $ request , RequestHandlerInterface $ next ): ResponseInterface
7785
7864
{
7865
+ $ driver = $ this ->getDriver ();
7866
+ $ address = $ this ->getAddress ();
7867
+ $ port = $ this ->getPort ();
7868
+ $ database = $ this ->getDatabase ();
7786
7869
$ username = $ this ->getUsername ();
7787
7870
$ password = $ this ->getPassword ();
7788
- if ($ username && $ password ) {
7789
- $ this ->db ->pdo ()-> reauthenticate ( $ username , $ password );
7871
+ if ($ driver || $ address || $ port || $ database || $ username || $ password ) {
7872
+ $ this ->db ->reconstruct ( $ driver , $ address , $ port , $ database , $ username , $ password );
7790
7873
}
7791
7874
return $ next ->handle ($ request );
7792
7875
}
@@ -9577,7 +9660,7 @@ private function setHabtmValues(ReflectedTable $t1, ReflectedTable $t2, array &$
9577
9660
use Tqdev \PhpCrudApi \Middleware \IpAddressMiddleware ;
9578
9661
use Tqdev \PhpCrudApi \Middleware \JoinLimitsMiddleware ;
9579
9662
use Tqdev \PhpCrudApi \Middleware \JwtAuthMiddleware ;
9580
- use Tqdev \PhpCrudApi \Middleware \ReAuthMiddleware ;
9663
+ use Tqdev \PhpCrudApi \Middleware \ReconnectMiddleware ;
9581
9664
use Tqdev \PhpCrudApi \Middleware \MultiTenancyMiddleware ;
9582
9665
use Tqdev \PhpCrudApi \Middleware \PageLimitsMiddleware ;
9583
9666
use Tqdev \PhpCrudApi \Middleware \Router \SimpleRouter ;
@@ -9627,8 +9710,8 @@ public function __construct(Config $config)
9627
9710
case 'dbAuth ' :
9628
9711
new DbAuthMiddleware ($ router , $ responder , $ properties , $ reflection , $ db );
9629
9712
break ;
9630
- case 'reAuth ' :
9631
- new ReAuthMiddleware ($ router , $ responder , $ properties , $ reflection , $ db );
9713
+ case 'reconnect ' :
9714
+ new ReconnectMiddleware ($ router , $ responder , $ properties , $ reflection , $ db );
9632
9715
break ;
9633
9716
case 'validation ' :
9634
9717
new ValidationMiddleware ($ router , $ responder , $ properties , $ reflection );
0 commit comments