Skip to content

Commit 1dc14ce

Browse files
authored
Merge pull request kvnZero#11 from kvnZero/feat/执行事件
add 引入事件机制 埋点事件
2 parents aa2bb4d + 24fc7f1 commit 1dc14ce

File tree

7 files changed

+93
-11
lines changed

7 files changed

+93
-11
lines changed

app/ApiJson/ApiJson.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
use App\ApiJson\Parse\Parse;
66
use App\Constants\ResponseCode;
7+
use App\Event\ApiJson\RequestHandleAfter;
8+
use App\Event\ApiJson\RequestHandleBefore;
79
use Hyperf\HttpServer\Contract\RequestInterface;
10+
use Hyperf\Utils\ApplicationContext;
11+
use Psr\EventDispatcher\EventDispatcherInterface;
812

913
class ApiJson
1014
{
@@ -14,16 +18,28 @@ public function __construct(protected RequestInterface $request, protected strin
1418

1519
public function Query(): array
1620
{
17-
if (!is_array(json_decode($this->request->getBody()->getContents(), true))) {
18-
return [
19-
'code' => ResponseCode::CODE_UNSUPPORTED_ENCODING,
20-
'msg' => ResponseCode::getMessage(ResponseCode::CODE_UNSUPPORTED_ENCODING)
21-
];
21+
$content = $this->request->getBody()->getContents();
22+
$beforeEvent = new RequestHandleBefore($content, $this->method);
23+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($beforeEvent);
24+
25+
if (empty($beforeEvent->response)) { //提供更多可能性, 如接入缓存功能
26+
if (!is_array(json_decode($beforeEvent->content, true))) {
27+
return [
28+
'code' => ResponseCode::CODE_UNSUPPORTED_ENCODING,
29+
'msg' => ResponseCode::getMessage(ResponseCode::CODE_UNSUPPORTED_ENCODING)
30+
];
31+
}
32+
$parse = new Parse(json_decode($beforeEvent->content, true), $this->method, $this->request->input('tag', ''));
33+
$response = $parse->handle();
34+
} else {
35+
$response = $beforeEvent->response;
2236
}
23-
$parse = new Parse(json_decode($this->request->getBody()->getContents(), true), $this->method, $this->request->input('tag', ''));
37+
38+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch(new RequestHandleAfter($beforeEvent->content, $response));
39+
2440
return array_merge([
2541
'code' => ResponseCode::CODE_SUCCESS,
2642
'msg' => ResponseCode::getMessage(ResponseCode::CODE_SUCCESS)
27-
], $parse->handle());
43+
], $response);
2844
}
2945
}

app/ApiJson/Handle/AbstractHandle.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
use App\ApiJson\Entity\ConditionEntity;
66
use App\ApiJson\Entity\TableEntity;
77
use App\ApiJson\Interface\QueryInterface;
8+
use App\Event\ApiJson\QueryHandleAfter;
9+
use App\Event\ApiJson\QueryHandleBefore;
10+
use Hyperf\Utils\ApplicationContext;
11+
use Psr\EventDispatcher\EventDispatcherInterface;
812
use App\ApiJson\Parse\Handle;
913
use Hyperf\Contract\ConfigInterface;
10-
use Hyperf\Utils\ApplicationContext;
1114

1215
abstract class AbstractHandle
1316
{
@@ -28,8 +31,10 @@ protected function sanitizeKey(string $key): string
2831

2932
public function handle()
3033
{
34+
$this->handleBefore();
3135
$this->buildModel();
3236
$this->unsetKeySaveCondition();
37+
$this->handleAfter();
3338
}
3439

3540
protected function unsetKeySaveCondition()
@@ -42,6 +47,20 @@ protected function unsetKeySaveCondition()
4247
$this->condition->setCondition($condition);
4348
}
4449

50+
protected function handleBefore()
51+
{
52+
$event = new QueryHandleBefore($this->condition);
53+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
54+
$this->condition = $event->condition;
55+
}
56+
57+
protected function handleAfter()
58+
{
59+
$event = new QueryHandleAfter($this->condition);
60+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
61+
$this->condition = $event->condition;
62+
}
63+
4564
protected function subTableQuery(array $data): QueryInterface
4665
{
4766
$tableName = $data['from'];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Event\ApiJson;
4+
5+
use App\ApiJson\Entity\ConditionEntity;
6+
7+
class QueryHandleAfter
8+
{
9+
public function __construct(public ConditionEntity $condition)
10+
{
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Event\ApiJson;
4+
5+
use App\ApiJson\Entity\ConditionEntity;
6+
7+
class QueryHandleBefore
8+
{
9+
public function __construct(public ConditionEntity $condition)
10+
{
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Event\ApiJson;
4+
5+
class RequestHandleAfter
6+
{
7+
public function __construct(public string $content, public array $response)
8+
{
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Event\ApiJson;
4+
5+
class RequestHandleBefore
6+
{
7+
public array $response = []; //如果这里被赋值 则不会执行代码 而会直接抛出该结果
8+
9+
public function __construct(public string $content, public string $method)
10+
{
11+
}
12+
}

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
"hyperf/cache": "~2.2.0",
1818
"hyperf/command": "~2.2.0",
1919
"hyperf/config": "~2.2.0",
20+
"hyperf/constants": "~2.2.0",
21+
"hyperf/database": "~2.2.0",
2022
"hyperf/db-connection": "~2.2.0",
23+
"hyperf/event": "^2.2",
2124
"hyperf/framework": "~2.2.0",
2225
"hyperf/guzzle": "~2.2.0",
2326
"hyperf/http-server": "~2.2.0",
2427
"hyperf/logger": "~2.2.0",
2528
"hyperf/memory": "~2.2.0",
26-
"hyperf/process": "~2.2.0",
27-
"hyperf/database": "~2.2.0",
28-
"hyperf/constants": "~2.2.0"
29+
"hyperf/process": "~2.2.0"
2930
},
3031
"require-dev": {
3132
"friendsofphp/php-cs-fixer": "^3.0",

0 commit comments

Comments
 (0)