File tree Expand file tree Collapse file tree 8 files changed +100
-5
lines changed Expand file tree Collapse file tree 8 files changed +100
-5
lines changed Original file line number Diff line number Diff line change 3
3
namespace App \ApiJson \Method ;
4
4
5
5
use App \ApiJson \Interface \QueryInterface ;
6
+ use App \Event \ApiJson \QueryExecuteAfter ;
7
+ use Hyperf \Utils \ApplicationContext ;
6
8
use Hyperf \Utils \Arr ;
9
+ use Psr \EventDispatcher \EventDispatcherInterface ;
7
10
8
11
class DeleteMethod extends AbstractMethod
9
12
{
@@ -34,6 +37,11 @@ protected function process()
34
37
$ this ->buildQuery ();
35
38
$ this ->query ->delete ($ id ) && $ deletedIds [] = $ id ; //这里主键应可配置
36
39
}
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 ;
38
46
}
39
47
}
Original file line number Diff line number Diff line change 3
3
namespace App \ApiJson \Method ;
4
4
5
5
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 ;
6
10
7
11
class GetMethod extends AbstractMethod
8
12
{
@@ -20,14 +24,28 @@ protected function process()
20
24
if (!$ queryMany ) {
21
25
$ this ->tableEntity ->getConditionEntity ()->setLimit (1 );
22
26
}
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
+
24
38
if ($ queryMany ) {
25
39
foreach ($ result as $ key => $ item ) {
26
40
$ result [$ key ] = $ this ->arrayQuery ? [$ this ->tableEntity ->getTableName () => $ item ] : $ item ;
27
41
}
28
42
} else {
29
43
$ result = current ($ result );
30
44
}
45
+
46
+ $ event = new QueryExecuteAfter ($ this ->query ->toSql (), $ result );
47
+ ApplicationContext::getContainer ()->get (EventDispatcherInterface::class)->dispatch ($ event );
48
+
31
49
return $ result ?: [];
32
50
}
33
51
}
Original file line number Diff line number Diff line change 3
3
namespace App \ApiJson \Method ;
4
4
5
5
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 ;
6
10
7
11
class HeadMethod extends AbstractMethod
8
12
{
@@ -15,8 +19,21 @@ protected function process()
15
19
{
16
20
$ handle = new Handle ($ this ->tableEntity ->getConditionEntity (), $ this ->tableEntity );
17
21
$ 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
+
18
35
return [
19
- 'count ' => $ this -> query -> count ()
36
+ 'count ' => $ count
20
37
];
21
38
}
22
39
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace App \ApiJson \Method ;
4
4
5
+ use App \Event \ApiJson \QueryExecuteAfter ;
6
+ use Hyperf \Utils \ApplicationContext ;
5
7
use Hyperf \Utils \Arr ;
8
+ use Psr \EventDispatcher \EventDispatcherInterface ;
6
9
7
10
class PostMethod extends AbstractMethod
8
11
{
@@ -25,6 +28,11 @@ protected function process()
25
28
foreach ($ insertData as $ insertItem ) {
26
29
$ insertIds [] = $ this ->query ->insertGetId ($ insertItem ); //因为需要返回ID 直接insert($insertData)不能得到本次插入的ID 未找到相关可用方法替代
27
30
}
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 ;
29
37
}
30
38
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace App \ApiJson \Method ;
4
4
5
+ use App \Event \ApiJson \QueryExecuteAfter ;
6
+ use Hyperf \Utils \ApplicationContext ;
5
7
use Hyperf \Utils \Arr ;
8
+ use Psr \EventDispatcher \EventDispatcherInterface ;
6
9
7
10
class PutMethod extends AbstractMethod
8
11
{
@@ -41,6 +44,11 @@ protected function process()
41
44
$ this ->query ->update ($ updateItem ) && $ updateIds [] = $ id ;
42
45
}
43
46
}
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 ;
45
53
}
46
54
}
Original file line number Diff line number Diff line change @@ -12,6 +12,9 @@ class MysqlQuery implements QueryInterface
12
12
/** @var string $primaryKey */
13
13
protected string $ primaryKey = 'id ' ;
14
14
15
+ /** @var bool $build 是否已经生成条件 */
16
+ protected bool $ build = false ;
17
+
15
18
/** @var Builder $db */
16
19
protected Builder $ db ;
17
20
@@ -54,18 +57,21 @@ public function count($columns = '*'): int
54
57
55
58
public function insertGetId (array $ values , $ sequence = null ): int
56
59
{
60
+ $ this ->build = true ;
57
61
return $ this ->db ->insertGetId ($ values , $ sequence );
58
62
}
59
63
60
64
public function update (array $ values ): bool
61
65
{
66
+ $ this ->build = true ;
62
67
$ this ->buildQuery (false );
63
68
if (empty ($ this ->db ->getBindings ()['where ' ])) return false ; // 不允许空条件修改
64
69
return $ this ->db ->update ($ values );
65
70
}
66
71
67
72
public function delete ($ id = null ): bool
68
73
{
74
+ $ this ->build = true ;
69
75
return $ this ->db ->delete ($ id );
70
76
}
71
77
@@ -88,6 +94,8 @@ public function getBindings(): array
88
94
89
95
protected function buildQuery (bool $ query = true )
90
96
{
97
+ if ($ this ->build ) return ;
98
+ $ this ->build = true ;
91
99
$ queryWhere = $ this ->conditionEntity ->getQueryWhere ();
92
100
foreach ($ queryWhere as $ itemWhere ) {
93
101
$ this ->db ->whereRaw ($ itemWhere ['sql ' ], $ itemWhere ['bind ' ]);
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments