Skip to content

Commit ba11860

Browse files
committed
Fixes #443
1 parent 53dcc97 commit ba11860

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,19 @@ If your tenants are identified by the "customer_id" column you can use the follo
695695
This construct adds a filter requiring "customer_id" to be "12" to every operation (except for "create").
696696
It also sets the column "customer_id" on "create" to "12" and removes the column from any other write operation.
697697

698+
### Custom handlers
699+
700+
You may use the "custom" middleware to implement any other functionality.
701+
702+
'custom.beforeHandler' => function ($operation, $tableName, $request, $environment) {
703+
$environment->start = microtime(true);
704+
},
705+
'custom.afterHandler' => function ($operation, $tableName, $response, $environment) {
706+
$response->addHeader('X-Time-Taken', microtime(true) - $environment->start);
707+
},
708+
709+
The above example will add a header "X-Time-Taken" with the number of seconds the API call has taken.
710+
698711
## OpenAPI specification
699712

700713
On the "/openapi" end-point the OpenAPI 3.0 (formerly called "Swagger") specification is served.

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Tqdev\PhpCrudApi\Middleware\AuthorizationMiddleware;
1414
use Tqdev\PhpCrudApi\Middleware\BasicAuthMiddleware;
1515
use Tqdev\PhpCrudApi\Middleware\CorsMiddleware;
16+
use Tqdev\PhpCrudApi\Middleware\CustomMiddleware;
1617
use Tqdev\PhpCrudApi\Middleware\FirewallMiddleware;
1718
use Tqdev\PhpCrudApi\Middleware\JwtAuthMiddleware;
1819
use Tqdev\PhpCrudApi\Middleware\MultiTenancyMiddleware;
@@ -69,6 +70,9 @@ public function __construct(Config $config)
6970
case 'authorization':
7071
new AuthorizationMiddleware($router, $responder, $properties, $reflection);
7172
break;
73+
case 'custom':
74+
new CustomMiddleware($router, $responder, $properties, $reflection);
75+
break;
7276
}
7377
}
7478
foreach ($config->getControllers() as $controller) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace Tqdev\PhpCrudApi\Middleware;
3+
4+
use Tqdev\PhpCrudApi\Column\ReflectionService;
5+
use Tqdev\PhpCrudApi\Controller\Responder;
6+
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
7+
use Tqdev\PhpCrudApi\Middleware\Router\Router;
8+
use Tqdev\PhpCrudApi\Record\RequestUtils;
9+
use Tqdev\PhpCrudApi\Request;
10+
use Tqdev\PhpCrudApi\Response;
11+
12+
class CustomMiddleware extends Middleware
13+
{
14+
private $reflection;
15+
16+
public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
17+
{
18+
parent::__construct($router, $responder, $properties);
19+
$this->reflection = $reflection;
20+
$this->utils = new RequestUtils($reflection);
21+
}
22+
23+
public function handle(Request $request): Response
24+
{
25+
$operation = $this->utils->getOperation($request);
26+
$tableName = $request->getPathSegment(2);
27+
$beforeHandler = $this->getProperty('beforeHandler', '');
28+
$environment = (object) array();
29+
if ($beforeHandler !== '') {
30+
call_user_func($beforeHandler, $operation, $tableName, $request, $environment);
31+
}
32+
$response = $this->next->handle($request);
33+
$afterHandler = $this->getProperty('afterHandler', '');
34+
if ($afterHandler !== '') {
35+
call_user_func($afterHandler, $operation, $tableName, $response, $environment);
36+
}
37+
return $response;
38+
}
39+
}

tests/config/base.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'username' => 'php-crud-api',
55
'password' => 'php-crud-api',
66
'controllers' => 'records,columns,cache,openapi',
7-
'middlewares' => 'cors,jwtAuth,basicAuth,authorization,validation,sanitation,multiTenancy',
7+
'middlewares' => 'cors,jwtAuth,basicAuth,authorization,validation,sanitation,multiTenancy,custom',
88
'jwtAuth.time' => '1538207605',
99
'jwtAuth.secret' => 'axpIrCGNGqxzx2R9dtXLIPUSqPo778uhb8CA0F4Hx',
1010
'basicAuth.passwordFile' => __DIR__ . DIRECTORY_SEPARATOR . '.htpasswd',
@@ -26,5 +26,13 @@
2626
'multiTenancy.handler' => function ($operation, $tableName) {
2727
return ($tableName == 'kunsthåndværk') ? ['user_id' => 1] : [];
2828
},
29+
'custom.beforeHandler' => function ($operation, $tableName, $request, $environment) {
30+
$environment->start = 0.003/*microtime(true)*/;
31+
},
32+
'custom.afterHandler' => function ($operation, $tableName, $response, $environment) {
33+
if ($tableName == 'kunsthåndværk' && $operation == 'increment') {
34+
$response->addHeader('X-Time-Taken', 0.006/*microtime(true)*/ - $environment->start);
35+
}
36+
},
2937
'debug' => true,
3038
];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
PATCH /records/kunsthåndværk/e42c77c6-06a4-4502-816c-d112c7142e6d
2+
3+
{"Umlauts ä_ö_ü-COUNT":10}
4+
===
5+
200
6+
Content-Type: application/json
7+
Content-Length: 1
8+
X-Time-Taken: 0.003
9+
10+
1
11+
===
12+
PATCH /records/kunsthåndværk/e42c77c6-06a4-4502-816c-d112c7142e6d
13+
14+
{"Umlauts ä_ö_ü-COUNT":-10}
15+
===
16+
200
17+
Content-Type: application/json
18+
Content-Length: 1
19+
X-Time-Taken: 0.003
20+
21+
1

0 commit comments

Comments
 (0)