Skip to content

Commit 2d60bec

Browse files
committed
add tables config
1 parent deeba13 commit 2d60bec

File tree

9 files changed

+87
-22
lines changed

9 files changed

+87
-22
lines changed

api.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5228,6 +5228,7 @@ class GenericDB
52285228
private $address;
52295229
private $port;
52305230
private $database;
5231+
private $tables;
52315232
private $username;
52325233
private $password;
52335234
private $pdo;
@@ -5305,26 +5306,27 @@ private function initPdo(): bool
53055306
foreach ($commands as $command) {
53065307
$this->pdo->addInitCommand($command);
53075308
}
5308-
$this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database);
5309-
$this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database);
5309+
$this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database, $this->tables);
5310+
$this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database, $this->tables);
53105311
$this->conditions = new ConditionsBuilder($this->driver);
53115312
$this->columns = new ColumnsBuilder($this->driver);
53125313
$this->converter = new DataConverter($this->driver);
53135314
return $result;
53145315
}
53155316

5316-
public function __construct(string $driver, string $address, int $port, string $database, string $username, string $password)
5317+
public function __construct(string $driver, string $address, int $port, string $database, array $tables, string $username, string $password)
53175318
{
53185319
$this->driver = $driver;
53195320
$this->address = $address;
53205321
$this->port = $port;
53215322
$this->database = $database;
5323+
$this->tables = $tables;
53225324
$this->username = $username;
53235325
$this->password = $password;
53245326
$this->initPdo();
53255327
}
53265328

5327-
public function reconstruct(string $driver, string $address, int $port, string $database, string $username, string $password): bool
5329+
public function reconstruct(string $driver, string $address, int $port, string $database, array $tables, string $username, string $password): bool
53285330
{
53295331
if ($driver) {
53305332
$this->driver = $driver;
@@ -5338,6 +5340,9 @@ public function reconstruct(string $driver, string $address, int $port, string $
53385340
if ($database) {
53395341
$this->database = $database;
53405342
}
5343+
if ($tables) {
5344+
$this->tables = $tables;
5345+
}
53415346
if ($username) {
53425347
$this->username = $username;
53435348
}
@@ -5529,6 +5534,7 @@ public function getCacheKey(): string
55295534
$this->address,
55305535
$this->port,
55315536
$this->database,
5537+
$this->tables,
55325538
$this->username
55335539
]));
55345540
}
@@ -5550,13 +5556,13 @@ class GenericDefinition
55505556
private $typeConverter;
55515557
private $reflection;
55525558

5553-
public function __construct(LazyPdo $pdo, string $driver, string $database)
5559+
public function __construct(LazyPdo $pdo, string $driver, string $database, array $tables)
55545560
{
55555561
$this->pdo = $pdo;
55565562
$this->driver = $driver;
55575563
$this->database = $database;
55585564
$this->typeConverter = new TypeConverter($driver);
5559-
$this->reflection = new GenericReflection($pdo, $driver, $database);
5565+
$this->reflection = new GenericReflection($pdo, $driver, $database, $tables);
55605566
}
55615567

55625568
private function quote(string $identifier): string
@@ -5970,13 +5976,15 @@ class GenericReflection
59705976
private $pdo;
59715977
private $driver;
59725978
private $database;
5979+
private $tables;
59735980
private $typeConverter;
59745981

5975-
public function __construct(LazyPdo $pdo, string $driver, string $database)
5982+
public function __construct(LazyPdo $pdo, string $driver, string $database, array $tables)
59765983
{
59775984
$this->pdo = $pdo;
59785985
$this->driver = $driver;
59795986
$this->database = $database;
5987+
$this->tables = $tables;
59805988
$this->typeConverter = new TypeConverter($driver);
59815989
}
59825990

@@ -6049,6 +6057,10 @@ public function getTables(): array
60496057
{
60506058
$sql = $this->getTablesSQL();
60516059
$results = $this->query($sql, [$this->database]);
6060+
$tables = $this->tables;
6061+
$results = array_filter($results, function ($v) use ($tables) {
6062+
return !$tables || in_array($v['TABLE_NAME'], $tables);
6063+
});
60526064
foreach ($results as &$result) {
60536065
switch ($this->driver) {
60546066
case 'mysql':
@@ -7953,6 +7965,15 @@ private function getDatabase(): string
79537965
return '';
79547966
}
79557967

7968+
private function getTables(): array
7969+
{
7970+
$tablesHandler = $this->getProperty('tablesHandler', '');
7971+
if ($tablesHandler) {
7972+
return call_user_func($tablesHandler);
7973+
}
7974+
return [];
7975+
}
7976+
79567977
private function getUsername(): string
79577978
{
79587979
$usernameHandler = $this->getProperty('usernameHandler', '');
@@ -7977,10 +7998,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
79777998
$address = $this->getAddress();
79787999
$port = $this->getPort();
79798000
$database = $this->getDatabase();
8001+
$tables = $this->getTables();
79808002
$username = $this->getUsername();
79818003
$password = $this->getPassword();
7982-
if ($driver || $address || $port || $database || $username || $password) {
7983-
$this->db->reconstruct($driver, $address, $port, $database, $username, $password);
8004+
if ($driver || $address || $port || $database || $tables || $username || $password) {
8005+
$this->db->reconstruct($driver, $address, $port, $database, $tables, $username, $password);
79848006
}
79858007
return $next->handle($request);
79868008
}
@@ -10017,6 +10039,7 @@ public function __construct(Config $config)
1001710039
$config->getAddress(),
1001810040
$config->getPort(),
1001910041
$config->getDatabase(),
10042+
$config->getTables(),
1002010043
$config->getUsername(),
1002110044
$config->getPassword()
1002210045
);
@@ -10195,6 +10218,7 @@ class Config
1019510218
'username' => null,
1019610219
'password' => null,
1019710220
'database' => null,
10221+
'tables' => '',
1019810222
'middlewares' => 'cors',
1019910223
'controllers' => 'records,geojson,openapi',
1020010224
'customControllers' => '',
@@ -10316,6 +10340,11 @@ public function getDatabase(): string
1031610340
return $this->values['database'];
1031710341
}
1031810342

10343+
public function getTables(): array
10344+
{
10345+
return array_filter(array_map('trim', explode(',', $this->values['tables'])));
10346+
}
10347+
1031910348
public function getMiddlewares(): array
1032010349
{
1032110350
return $this->values['middlewares'];
@@ -10627,8 +10656,7 @@ public static function toString(ResponseInterface $response): string
1062710656
$config = new Config([
1062810657
'username' => 'php-crud-api',
1062910658
'password' => 'php-crud-api',
10630-
'database' => 'php-crud-api',
10631-
'controllers' => 'records,columns'
10659+
'database' => 'php-crud-api'
1063210660
]);
1063310661
$request = RequestFactory::fromGlobals();
1063410662
$api = new Api($config);

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function __construct(Config $config)
5050
$config->getAddress(),
5151
$config->getPort(),
5252
$config->getDatabase(),
53+
$config->getTables(),
5354
$config->getUsername(),
5455
$config->getPassword()
5556
);

src/Tqdev/PhpCrudApi/Config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Config
1111
'username' => null,
1212
'password' => null,
1313
'database' => null,
14+
'tables' => '',
1415
'middlewares' => 'cors',
1516
'controllers' => 'records,geojson,openapi',
1617
'customControllers' => '',
@@ -132,6 +133,11 @@ public function getDatabase(): string
132133
return $this->values['database'];
133134
}
134135

136+
public function getTables(): array
137+
{
138+
return array_filter(array_map('trim', explode(',', $this->values['tables'])));
139+
}
140+
135141
public function getMiddlewares(): array
136142
{
137143
return $this->values['middlewares'];

src/Tqdev/PhpCrudApi/Database/GenericDB.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class GenericDB
1313
private $address;
1414
private $port;
1515
private $database;
16+
private $tables;
1617
private $username;
1718
private $password;
1819
private $pdo;
@@ -90,26 +91,27 @@ private function initPdo(): bool
9091
foreach ($commands as $command) {
9192
$this->pdo->addInitCommand($command);
9293
}
93-
$this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database);
94-
$this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database);
94+
$this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database, $this->tables);
95+
$this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database, $this->tables);
9596
$this->conditions = new ConditionsBuilder($this->driver);
9697
$this->columns = new ColumnsBuilder($this->driver);
9798
$this->converter = new DataConverter($this->driver);
9899
return $result;
99100
}
100101

101-
public function __construct(string $driver, string $address, int $port, string $database, string $username, string $password)
102+
public function __construct(string $driver, string $address, int $port, string $database, array $tables, string $username, string $password)
102103
{
103104
$this->driver = $driver;
104105
$this->address = $address;
105106
$this->port = $port;
106107
$this->database = $database;
108+
$this->tables = $tables;
107109
$this->username = $username;
108110
$this->password = $password;
109111
$this->initPdo();
110112
}
111113

112-
public function reconstruct(string $driver, string $address, int $port, string $database, string $username, string $password): bool
114+
public function reconstruct(string $driver, string $address, int $port, string $database, array $tables, string $username, string $password): bool
113115
{
114116
if ($driver) {
115117
$this->driver = $driver;
@@ -123,6 +125,9 @@ public function reconstruct(string $driver, string $address, int $port, string $
123125
if ($database) {
124126
$this->database = $database;
125127
}
128+
if ($tables) {
129+
$this->tables = $tables;
130+
}
126131
if ($username) {
127132
$this->username = $username;
128133
}
@@ -314,6 +319,7 @@ public function getCacheKey(): string
314319
$this->address,
315320
$this->port,
316321
$this->database,
322+
$this->tables,
317323
$this->username
318324
]));
319325
}

src/Tqdev/PhpCrudApi/Database/GenericDefinition.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class GenericDefinition
1414
private $typeConverter;
1515
private $reflection;
1616

17-
public function __construct(LazyPdo $pdo, string $driver, string $database)
17+
public function __construct(LazyPdo $pdo, string $driver, string $database, array $tables)
1818
{
1919
$this->pdo = $pdo;
2020
$this->driver = $driver;
2121
$this->database = $database;
2222
$this->typeConverter = new TypeConverter($driver);
23-
$this->reflection = new GenericReflection($pdo, $driver, $database);
23+
$this->reflection = new GenericReflection($pdo, $driver, $database, $tables);
2424
}
2525

2626
private function quote(string $identifier): string

src/Tqdev/PhpCrudApi/Database/GenericReflection.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ class GenericReflection
99
private $pdo;
1010
private $driver;
1111
private $database;
12+
private $tables;
1213
private $typeConverter;
1314

14-
public function __construct(LazyPdo $pdo, string $driver, string $database)
15+
public function __construct(LazyPdo $pdo, string $driver, string $database, array $tables)
1516
{
1617
$this->pdo = $pdo;
1718
$this->driver = $driver;
1819
$this->database = $database;
20+
$this->tables = $tables;
1921
$this->typeConverter = new TypeConverter($driver);
2022
}
2123

@@ -88,6 +90,10 @@ public function getTables(): array
8890
{
8991
$sql = $this->getTablesSQL();
9092
$results = $this->query($sql, [$this->database]);
93+
$tables = $this->tables;
94+
$results = array_filter($results, function ($v) use ($tables) {
95+
return !$tables || in_array($v['TABLE_NAME'], $tables);
96+
});
9197
foreach ($results as &$result) {
9298
switch ($this->driver) {
9399
case 'mysql':

src/Tqdev/PhpCrudApi/Middleware/ReconnectMiddleware.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ private function getDatabase(): string
5959
return '';
6060
}
6161

62+
private function getTables(): array
63+
{
64+
$tablesHandler = $this->getProperty('tablesHandler', '');
65+
if ($tablesHandler) {
66+
return call_user_func($tablesHandler);
67+
}
68+
return [];
69+
}
70+
6271
private function getUsername(): string
6372
{
6473
$usernameHandler = $this->getProperty('usernameHandler', '');
@@ -83,10 +92,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
8392
$address = $this->getAddress();
8493
$port = $this->getPort();
8594
$database = $this->getDatabase();
95+
$tables = $this->getTables();
8696
$username = $this->getUsername();
8797
$password = $this->getPassword();
88-
if ($driver || $address || $port || $database || $username || $password) {
89-
$this->db->reconstruct($driver, $address, $port, $database, $username, $password);
98+
if ($driver || $address || $port || $database || $tables || $username || $password) {
99+
$this->db->reconstruct($driver, $address, $port, $database, $tables, $username, $password);
90100
}
91101
return $next->handle($request);
92102
}

src/index.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
$config = new Config([
1313
'username' => 'php-crud-api',
1414
'password' => 'php-crud-api',
15-
'database' => 'php-crud-api',
16-
'controllers' => 'records,columns'
15+
'database' => 'php-crud-api'
1716
]);
1817
$request = RequestFactory::fromGlobals();
1918
$api = new Api($config);

test.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ function getDatabase(Config $config)
8585
return $config->getMiddlewares()['reconnect']['databaseHandler']();
8686
}
8787

88+
function getTables(Config $config)
89+
{
90+
if (!isset($config->getMiddlewares()['reconnect']['tablesHandler'])) {
91+
return $config->getTables();
92+
}
93+
return $config->getMiddlewares()['reconnect']['tablesHandler']();
94+
}
95+
8896
function getUsername(Config $config)
8997
{
9098
if (!isset($config->getMiddlewares()['reconnect']['usernameHandler'])) {
@@ -112,6 +120,7 @@ function loadFixture(string $dir, Config $config)
112120
$config->getAddress(),
113121
$config->getPort(),
114122
getDatabase($config),
123+
getTables($config),
115124
getUsername($config),
116125
getPassword($config)
117126
);

0 commit comments

Comments
 (0)