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 33namespace App \ApiJson \Method ;
44
55use App \ApiJson \Interface \QueryInterface ;
6+ use App \Event \ApiJson \QueryExecuteAfter ;
7+ use Hyperf \Utils \ApplicationContext ;
68use Hyperf \Utils \Arr ;
9+ use Psr \EventDispatcher \EventDispatcherInterface ;
710
811class 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}
Original file line number Diff line number Diff line change 33namespace App \ApiJson \Method ;
44
55use 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
711class 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}
Original file line number Diff line number Diff line change 33namespace App \ApiJson \Method ;
44
55use 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
711class 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}
Original file line number Diff line number Diff line change 22
33namespace App \ApiJson \Method ;
44
5+ use App \Event \ApiJson \QueryExecuteAfter ;
6+ use Hyperf \Utils \ApplicationContext ;
57use Hyperf \Utils \Arr ;
8+ use Psr \EventDispatcher \EventDispatcherInterface ;
69
710class 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}
Original file line number Diff line number Diff line change 22
33namespace App \ApiJson \Method ;
44
5+ use App \Event \ApiJson \QueryExecuteAfter ;
6+ use Hyperf \Utils \ApplicationContext ;
57use Hyperf \Utils \Arr ;
8+ use Psr \EventDispatcher \EventDispatcherInterface ;
69
710class 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}
Original file line number Diff line number Diff 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 ' ]);
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