Skip to content

Commit 8113307

Browse files
authored
Merge pull request kvnZero#12 from kvnZero/feat/执行事件
add 语句查询埋点
2 parents 1dc14ce + 663b897 commit 8113307

File tree

8 files changed

+100
-5
lines changed

8 files changed

+100
-5
lines changed

app/ApiJson/Method/DeleteMethod.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace App\ApiJson\Method;
44

55
use App\ApiJson\Interface\QueryInterface;
6+
use App\Event\ApiJson\QueryExecuteAfter;
7+
use Hyperf\Utils\ApplicationContext;
68
use Hyperf\Utils\Arr;
9+
use Psr\EventDispatcher\EventDispatcherInterface;
710

811
class DeleteMethod extends AbstractMethod
912
{
@@ -34,6 +37,11 @@ protected function process()
3437
$this->buildQuery();
3538
$this->query->delete($id) && $deletedIds[] = $id; //这里主键应可配置
3639
}
37-
return $this->parseManyResponse($deletedIds, $queryMany);
40+
$result = $this->parseManyResponse($deletedIds, $queryMany);
41+
42+
$event = new QueryExecuteAfter($this->query->toSql(), $result);
43+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
44+
45+
return $result;
3846
}
3947
}

app/ApiJson/Method/GetMethod.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace App\ApiJson\Method;
44

55
use App\ApiJson\Parse\Handle;
6+
use App\Event\ApiJson\QueryExecuteAfter;
7+
use App\Event\ApiJson\QueryExecuteBefore;
8+
use Hyperf\Utils\ApplicationContext;
9+
use Psr\EventDispatcher\EventDispatcherInterface;
610

711
class GetMethod extends AbstractMethod
812
{
@@ -20,14 +24,28 @@ protected function process()
2024
if (!$queryMany) {
2125
$this->tableEntity->getConditionEntity()->setLimit(1);
2226
}
23-
$result = $this->query->all();
27+
28+
//该事件鼓励是做语句缓存或者事件触发 不赞成修改语句做法 修改语句应在更上层的QueryHandle事件
29+
$event = new QueryExecuteBefore($this->query->toSql(), $this->method);
30+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
31+
32+
if(is_null($event->result)) {
33+
$result = $this->query->all();
34+
} else {
35+
$result = $event->result;
36+
}
37+
2438
if ($queryMany) {
2539
foreach ($result as $key => $item) {
2640
$result[$key] = $this->arrayQuery ? [$this->tableEntity->getTableName() => $item] : $item;
2741
}
2842
} else {
2943
$result = current($result);
3044
}
45+
46+
$event = new QueryExecuteAfter($this->query->toSql(), $result);
47+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
48+
3149
return $result ?: [];
3250
}
3351
}

app/ApiJson/Method/HeadMethod.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace App\ApiJson\Method;
44

55
use App\ApiJson\Parse\Handle;
6+
use App\Event\ApiJson\QueryExecuteAfter;
7+
use App\Event\ApiJson\QueryExecuteBefore;
8+
use Hyperf\Utils\ApplicationContext;
9+
use Psr\EventDispatcher\EventDispatcherInterface;
610

711
class HeadMethod extends AbstractMethod
812
{
@@ -15,8 +19,21 @@ protected function process()
1519
{
1620
$handle = new Handle($this->tableEntity->getConditionEntity(), $this->tableEntity);
1721
$handle->build();
22+
23+
$event = new QueryExecuteBefore($this->query->toSql(), $this->method);
24+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
25+
26+
if(is_null($event->result)) {
27+
$count = $this->query->count();
28+
} else {
29+
$count = $event->result;
30+
}
31+
32+
$event = new QueryExecuteAfter($this->query->toSql(), $count);
33+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
34+
1835
return [
19-
'count' => $this->query->count()
36+
'count' => $count
2037
];
2138
}
2239
}

app/ApiJson/Method/PostMethod.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace App\ApiJson\Method;
44

5+
use App\Event\ApiJson\QueryExecuteAfter;
6+
use Hyperf\Utils\ApplicationContext;
57
use Hyperf\Utils\Arr;
8+
use Psr\EventDispatcher\EventDispatcherInterface;
69

710
class PostMethod extends AbstractMethod
811
{
@@ -25,6 +28,11 @@ protected function process()
2528
foreach ($insertData as $insertItem) {
2629
$insertIds[] = $this->query->insertGetId($insertItem); //因为需要返回ID 直接insert($insertData)不能得到本次插入的ID 未找到相关可用方法替代
2730
}
28-
return $this->parseManyResponse($insertIds, $this->isQueryMany());
31+
$result = $this->parseManyResponse($insertIds, $this->isQueryMany());
32+
33+
$event = new QueryExecuteAfter($this->query->toSql(), $result);
34+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
35+
36+
return $result;
2937
}
3038
}

app/ApiJson/Method/PutMethod.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace App\ApiJson\Method;
44

5+
use App\Event\ApiJson\QueryExecuteAfter;
6+
use Hyperf\Utils\ApplicationContext;
57
use Hyperf\Utils\Arr;
8+
use Psr\EventDispatcher\EventDispatcherInterface;
69

710
class PutMethod extends AbstractMethod
811
{
@@ -41,6 +44,11 @@ protected function process()
4144
$this->query->update($updateItem) && $updateIds[] = $id;
4245
}
4346
}
44-
return $this->parseManyResponse($updateIds, $queryMany);
47+
$result = $this->parseManyResponse($updateIds, $queryMany);
48+
49+
$event = new QueryExecuteAfter($this->query->toSql(), $result);
50+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
51+
52+
return $result;
4553
}
4654
}

app/ApiJson/Model/MysqlQuery.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class MysqlQuery implements QueryInterface
1212
/** @var string $primaryKey */
1313
protected string $primaryKey = 'id';
1414

15+
/** @var bool $build 是否已经生成条件 */
16+
protected bool $build = false;
17+
1518
/** @var Builder $db */
1619
protected Builder $db;
1720

@@ -54,18 +57,21 @@ public function count($columns = '*'): int
5457

5558
public function insertGetId(array $values, $sequence = null): int
5659
{
60+
$this->build = true;
5761
return $this->db->insertGetId($values, $sequence);
5862
}
5963

6064
public function update(array $values): bool
6165
{
66+
$this->build = true;
6267
$this->buildQuery(false);
6368
if (empty($this->db->getBindings()['where'])) return false; // 不允许空条件修改
6469
return $this->db->update($values);
6570
}
6671

6772
public function delete($id = null): bool
6873
{
74+
$this->build = true;
6975
return $this->db->delete($id);
7076
}
7177

@@ -88,6 +94,8 @@ public function getBindings(): array
8894

8995
protected function buildQuery(bool $query = true)
9096
{
97+
if ($this->build) return;
98+
$this->build = true;
9199
$queryWhere = $this->conditionEntity->getQueryWhere();
92100
foreach ($queryWhere as $itemWhere) {
93101
$this->db->whereRaw($itemWhere['sql'], $itemWhere['bind']);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Event\ApiJson;
4+
5+
/**
6+
* 任何方法都会执行该事件
7+
*/
8+
class QueryExecuteAfter
9+
{
10+
public function __construct(public string $sql, public $result)
11+
{
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Event\ApiJson;
4+
5+
/**
6+
* 查询(GET, HEAD)相关语句执行前会执行该事件
7+
*/
8+
class QueryExecuteBefore
9+
{
10+
public $result = null; //如果这里被赋值 则不会执行代码 而会直接抛出该结果
11+
12+
public function __construct(public string $sql, public string $method)
13+
{
14+
}
15+
}

0 commit comments

Comments
 (0)