forked from kvnZero/hyperf-APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetMethod.php
More file actions
64 lines (50 loc) · 2.12 KB
/
GetMethod.php
File metadata and controls
64 lines (50 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace App\ApiJson\Method;
use App\ApiJson\Parse\Handle;
use App\Event\ApiJson\QueryExecuteAfter;
use App\Event\ApiJson\QueryExecuteBefore;
use App\Event\ApiJson\QueryResult;
use Hyperf\Utils\ApplicationContext;
use Psr\EventDispatcher\EventDispatcherInterface;
class GetMethod extends AbstractMethod
{
protected function validateCondition(): bool
{
return $this->method == 'GET';
}
protected function process()
{
$queryMany = $this->isQueryMany();
if (!$queryMany) {
$this->tableEntity->getConditionEntity()->setLimit(1);
}
$handle = new Handle($this->tableEntity->getConditionEntity(), $this->tableEntity);
$handle->buildQuery();
//该事件鼓励是做语句缓存或者事件触发 不赞成修改语句做法 修改语句应在更上层的QueryHandle事件
$event = new QueryExecuteBefore($this->query->toSql(), $this->method);
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
if(is_null($event->result)) {
$result = $this->query->all();
$queryEvent = new QueryResult($result, $this->query->toSql());
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($queryEvent);
$result = $queryEvent->result;
if (!empty($this->tableEntity->getConditionEntity()->getProcedure())) {
foreach ($result as $i => $item) {
$result[$i]['procedure'] = $this->query->callProcedure($item);
}
}
} else {
$result = $event->result;
}
if ($queryMany) {
foreach ($result as $key => $item) {
$result[$key] = $this->arrayQuery ? [$this->tableEntity->getTableName() => $item] : $item;
}
} else {
$result = current($result);
}
$event = new QueryExecuteAfter($this->query->toSql(), $this->method, $result);
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
return $event->result ?: [];
}
}