Maurits van der Schee 6 years ago
parent
commit
ba1186055f

+ 13
- 0
README.md View File

@@ -695,6 +695,19 @@ If your tenants are identified by the "customer_id" column you can use the follo
695 695
 This construct adds a filter requiring "customer_id" to be "12" to every operation (except for "create").
696 696
 It also sets the column "customer_id" on "create" to "12" and removes the column from any other write operation.
697 697
 
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
+
698 711
 ## OpenAPI specification
699 712
 
700 713
 On the "/openapi" end-point the OpenAPI 3.0 (formerly called "Swagger") specification is served. 

+ 4
- 0
src/Tqdev/PhpCrudApi/Api.php View File

@@ -13,6 +13,7 @@ use Tqdev\PhpCrudApi\Database\GenericDB;
13 13
 use Tqdev\PhpCrudApi\Middleware\AuthorizationMiddleware;
14 14
 use Tqdev\PhpCrudApi\Middleware\BasicAuthMiddleware;
15 15
 use Tqdev\PhpCrudApi\Middleware\CorsMiddleware;
16
+use Tqdev\PhpCrudApi\Middleware\CustomMiddleware;
16 17
 use Tqdev\PhpCrudApi\Middleware\FirewallMiddleware;
17 18
 use Tqdev\PhpCrudApi\Middleware\JwtAuthMiddleware;
18 19
 use Tqdev\PhpCrudApi\Middleware\MultiTenancyMiddleware;
@@ -69,6 +70,9 @@ class Api
69 70
                 case 'authorization':
70 71
                     new AuthorizationMiddleware($router, $responder, $properties, $reflection);
71 72
                     break;
73
+                case 'custom':
74
+                    new CustomMiddleware($router, $responder, $properties, $reflection);
75
+                    break;
72 76
             }
73 77
         }
74 78
         foreach ($config->getControllers() as $controller) {

+ 39
- 0
src/Tqdev/PhpCrudApi/Middleware/CustomMiddleware.php View File

@@ -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
+}

+ 9
- 1
tests/config/base.php View File

@@ -4,7 +4,7 @@ $settings = [
4 4
     'username' => 'php-crud-api',
5 5
     'password' => 'php-crud-api',
6 6
     'controllers' => 'records,columns,cache,openapi',
7
-    'middlewares' => 'cors,jwtAuth,basicAuth,authorization,validation,sanitation,multiTenancy',
7
+    'middlewares' => 'cors,jwtAuth,basicAuth,authorization,validation,sanitation,multiTenancy,custom',
8 8
     'jwtAuth.time' => '1538207605',
9 9
     'jwtAuth.secret' => 'axpIrCGNGqxzx2R9dtXLIPUSqPo778uhb8CA0F4Hx',
10 10
     'basicAuth.passwordFile' => __DIR__ . DIRECTORY_SEPARATOR . '.htpasswd',
@@ -26,5 +26,13 @@ $settings = [
26 26
     'multiTenancy.handler' => function ($operation, $tableName) {
27 27
         return ($tableName == 'kunsthåndværk') ? ['user_id' => 1] : [];
28 28
     },
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
+    },
29 37
     'debug' => true,
30 38
 ];

+ 21
- 0
tests/functional/001_records/074_custom_kunsthåndværk.log View File

@@ -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

Loading…
Cancel
Save