Skip to content

Commit 6ff0e01

Browse files
committed
Add custom controllers as proposed in #546
1 parent c435660 commit 6ff0e01

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

api.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10068,6 +10068,12 @@ public function __construct(Config $config)
1006810068
break;
1006910069
}
1007010070
}
10071+
foreach ($config->getCustomControllers() as $className) {
10072+
if (class_exists($className)) {
10073+
$records = new RecordService($db, $reflection);
10074+
new $className($router, $responder, $records);
10075+
}
10076+
}
1007110077
$this->router = $router;
1007210078
$this->responder = $responder;
1007310079
$this->debug = $config->getDebug();
@@ -10155,6 +10161,7 @@ class Config
1015510161
'database' => null,
1015610162
'middlewares' => 'cors',
1015710163
'controllers' => 'records,geojson,openapi',
10164+
'customControllers' => '',
1015810165
'cacheType' => 'TempFile',
1015910166
'cachePath' => '',
1016010167
'cacheTime' => 10,
@@ -10174,18 +10181,24 @@ private function getDefaultDriver(array $values): string
1017410181
private function getDefaultPort(string $driver): int
1017510182
{
1017610183
switch ($driver) {
10177-
case 'mysql':return 3306;
10178-
case 'pgsql':return 5432;
10179-
case 'sqlsrv':return 1433;
10184+
case 'mysql':
10185+
return 3306;
10186+
case 'pgsql':
10187+
return 5432;
10188+
case 'sqlsrv':
10189+
return 1433;
1018010190
}
1018110191
}
1018210192

1018310193
private function getDefaultAddress(string $driver): string
1018410194
{
1018510195
switch ($driver) {
10186-
case 'mysql':return 'localhost';
10187-
case 'pgsql':return 'localhost';
10188-
case 'sqlsrv':return 'localhost';
10196+
case 'mysql':
10197+
return 'localhost';
10198+
case 'pgsql':
10199+
return 'localhost';
10200+
case 'sqlsrv':
10201+
return 'localhost';
1018910202
}
1019010203
}
1019110204

@@ -10273,7 +10286,12 @@ public function getMiddlewares(): array
1027310286

1027410287
public function getControllers(): array
1027510288
{
10276-
return array_map('trim', explode(',', $this->values['controllers']));
10289+
return array_filter(array_map('trim', explode(',', $this->values['controllers'])));
10290+
}
10291+
10292+
public function getCustomControllers(): array
10293+
{
10294+
return array_filter(array_map('trim', explode(',', $this->values['customControllers'])));
1027710295
}
1027810296

1027910297
public function getCacheType(): string

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ public function __construct(Config $config)
131131
break;
132132
}
133133
}
134+
foreach ($config->getCustomControllers() as $className) {
135+
if (class_exists($className)) {
136+
$records = new RecordService($db, $reflection);
137+
new $className($router, $responder, $records);
138+
}
139+
}
134140
$this->router = $router;
135141
$this->responder = $responder;
136142
$this->debug = $config->getDebug();

src/Tqdev/PhpCrudApi/Config.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Config
1313
'database' => null,
1414
'middlewares' => 'cors',
1515
'controllers' => 'records,geojson,openapi',
16+
'customControllers' => '',
1617
'cacheType' => 'TempFile',
1718
'cachePath' => '',
1819
'cacheTime' => 10,
@@ -32,18 +33,24 @@ private function getDefaultDriver(array $values): string
3233
private function getDefaultPort(string $driver): int
3334
{
3435
switch ($driver) {
35-
case 'mysql':return 3306;
36-
case 'pgsql':return 5432;
37-
case 'sqlsrv':return 1433;
36+
case 'mysql':
37+
return 3306;
38+
case 'pgsql':
39+
return 5432;
40+
case 'sqlsrv':
41+
return 1433;
3842
}
3943
}
4044

4145
private function getDefaultAddress(string $driver): string
4246
{
4347
switch ($driver) {
44-
case 'mysql':return 'localhost';
45-
case 'pgsql':return 'localhost';
46-
case 'sqlsrv':return 'localhost';
48+
case 'mysql':
49+
return 'localhost';
50+
case 'pgsql':
51+
return 'localhost';
52+
case 'sqlsrv':
53+
return 'localhost';
4754
}
4855
}
4956

@@ -131,7 +138,12 @@ public function getMiddlewares(): array
131138

132139
public function getControllers(): array
133140
{
134-
return array_map('trim', explode(',', $this->values['controllers']));
141+
return array_filter(array_map('trim', explode(',', $this->values['controllers'])));
142+
}
143+
144+
public function getCustomControllers(): array
145+
{
146+
return array_filter(array_map('trim', explode(',', $this->values['customControllers'])));
135147
}
136148

137149
public function getCacheType(): string

0 commit comments

Comments
 (0)