From f65e0e45349d2f74c47ec6731136834c1c107338 Mon Sep 17 00:00:00 2001 From: abigeater Date: Sat, 6 Nov 2021 18:37:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=95=B0=E7=BB=84=E5=86=85?= =?UTF-8?q?=E4=B8=80=E5=AF=B9=E4=B8=80=E6=9F=A5=E8=AF=A2=20&&=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0debug=E5=85=B3=E9=94=AE=E5=AD=97=20=E5=8F=AF=E8=BF=94?= =?UTF-8?q?=E8=BF=98debug=E6=95=B0=E7=BB=84=E5=8C=85=E5=90=AB=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=9A=84=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/ApiJson/Method/GetMethod.php | 2 - app/ApiJson/Parse/Parse.php | 54 +++++++++++++++++++++--- app/ApiJson/Replace/QuoteReplace.php | 6 +-- app/Listener/DbQueryExecutedListener.php | 14 +++--- 4 files changed, 58 insertions(+), 18 deletions(-) diff --git a/app/ApiJson/Method/GetMethod.php b/app/ApiJson/Method/GetMethod.php index 6ecd709..2b4b8ef 100644 --- a/app/ApiJson/Method/GetMethod.php +++ b/app/ApiJson/Method/GetMethod.php @@ -2,8 +2,6 @@ namespace App\ApiJson\Method; -use App\ApiJson\Interface\QueryInterface; - class GetMethod extends AbstractMethod { protected function validateCondition(): bool diff --git a/app/ApiJson/Parse/Parse.php b/app/ApiJson/Parse/Parse.php index 844d17e..81e3698 100644 --- a/app/ApiJson/Parse/Parse.php +++ b/app/ApiJson/Parse/Parse.php @@ -9,11 +9,13 @@ use App\ApiJson\Method\HeadMethod; use App\ApiJson\Method\PostMethod; use App\ApiJson\Method\PutMethod; +use Hyperf\Utils\Context; class Parse { protected array $tagColumn = [ - 'tag' => null + 'tag' => null, + 'debug' => false ]; protected array $globalKey = [ @@ -47,9 +49,8 @@ public function handle(bool $isQueryMany = false, array $extendData = []): array $this->globalKey[$tableName] = $condition; continue; } - if ($tableName == '[]') { - $parse = new self($condition, $this->method, $condition['tag'] ?? ''); - $result[$tableName] = $parse->handle(true, array_merge($result, $extendData)); //提供行为 + if ($tableName == '[]' && $this->method == 'GET') { + $result[$tableName] = $this->handleArray($condition, array_merge($result, $extendData)); //提供行为 continue; //跳出不往下执行 } if (str_ends_with($tableName, '[]')) { @@ -70,7 +71,50 @@ public function handle(bool $isQueryMany = false, array $extendData = []): array } } } - //TODO: 抽象操作方法 + return $this->resultExtendHandle($result); + } + + protected function resultExtendHandle(array $result) + { + if ($this->tagColumn['debug']) { + $result['debug'] = (new Context())->get('querySql'); + } + return $result; + } + + /** + * 处理[]的数据 + * @param array $jsonData + * @param array $extendData + * @return array + */ + protected function handleArray(array $jsonData, array $extendData = []): array + { + $result = [[]]; + foreach ($jsonData as $tableName => $condition) { //可以优化成协程行为(如果没有依赖赋值的前提下) + foreach ($result as $key => $item) { + if (in_array($tableName, $this->filterKey())) { + $this->tagColumn[$tableName] = $condition; + continue; + } + if (in_array($tableName, $this->globalKey())) { + $this->globalKey[$tableName] = $condition; + continue; + } + $extendData['currentItem'] = $item; + $this->tableEntities['[]'][$tableName] = new TableEntity($tableName, $jsonData, $this->getGlobalArgs(), array_merge($result, $extendData)); + $method = new GetMethod($this->tableEntities['[]'][$tableName], $this->method); + $method->setQueryMany($result == [[]]); + $response = $method->handle(); + if (!is_null($response)) { + if ($result == [[]]) { + $result = $response; //masterData + } else { + $result[$key][$tableName] = $response; + } + } + } + } return $result; } diff --git a/app/ApiJson/Replace/QuoteReplace.php b/app/ApiJson/Replace/QuoteReplace.php index 406e707..1a76711 100644 --- a/app/ApiJson/Replace/QuoteReplace.php +++ b/app/ApiJson/Replace/QuoteReplace.php @@ -2,9 +2,6 @@ namespace App\ApiJson\Replace; -use App\ApiJson\Interface\QueryInterface; -use Hyperf\Utils\Arr; - class QuoteReplace extends AbstractReplace { protected function validateCondition(): bool @@ -14,7 +11,8 @@ protected function validateCondition(): bool protected function process() { - $path = str_replace('/', '.', $this->value); + $path = str_replace(['/', '[]'], ['.', 'currentItem'], $this->value); + $this->value = data_get($this->extendData, $path); $this->key = substr($this->key, 0, strlen($this->key) - 1); diff --git a/app/Listener/DbQueryExecutedListener.php b/app/Listener/DbQueryExecutedListener.php index abe5a52..bd180db 100644 --- a/app/Listener/DbQueryExecutedListener.php +++ b/app/Listener/DbQueryExecutedListener.php @@ -14,25 +14,25 @@ use Hyperf\Database\Events\QueryExecuted; use Hyperf\Event\Annotation\Listener; use Hyperf\Event\Contract\ListenerInterface; -use Hyperf\Logger\LoggerFactory; use Hyperf\Utils\Arr; +use Hyperf\Utils\Context; use Hyperf\Utils\Str; use Psr\Container\ContainerInterface; -use Psr\Log\LoggerInterface; /** * @Listener */ class DbQueryExecutedListener implements ListenerInterface { + /** - * @var LoggerInterface + * @var Context */ - private $logger; + private $context; public function __construct(ContainerInterface $container) { - $this->logger = $container->get(LoggerFactory::class)->get('sql'); + $this->context = new Context(); } public function listen(): array @@ -54,8 +54,8 @@ public function process(object $event) $sql = Str::replaceFirst('?', "'{$value}'", $sql); } } - - $this->logger->info(sprintf('[%s] %s', $event->time, $sql)); + $querySql = $this->context->get('querySql'); + $this->context->set('querySql', array_merge($querySql ?? [], [$sql])); } } }