Skip to content

Commit c1f7d21

Browse files
committed
修复切面中条件判断错误, 导致不能进入组件的bug
1 parent 0c76416 commit c1f7d21

File tree

4 files changed

+41
-52
lines changed

4 files changed

+41
-52
lines changed

src/Aspect/ResponseAspect.php

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
namespace Wilbur\HyperfSoar\Aspect;
1717

18-
use Hyperf\Config\Annotation\Value;
1918
use Hyperf\Contract\ConfigInterface;
2019
use Hyperf\Di\Annotation\Aspect;
2120
use Hyperf\Di\Annotation\Inject;
@@ -32,53 +31,42 @@
3231
use Psr\Container\ContainerInterface;
3332

3433
use function array_merge;
35-
use function class_basename;
3634
use function co;
3735
use function explode;
3836
use function json_decode;
3937
use function json_encode;
4038

4139
use const JSON_UNESCAPED_UNICODE;
4240

43-
/**
44-
* @Aspect
45-
*/
41+
#[Aspect]
4642
class ResponseAspect extends AbstractAspect
4743
{
48-
/**
49-
* @Value("soar.cut_classes")
50-
* @var array
51-
*/
5244
public $classes = [
5345
'Hyperf\HttpServer\Response::json',
5446
];
55-
/**
56-
* @Value("soar")
57-
* @var array
58-
*/
59-
protected $config;
60-
/**
61-
* @Inject()
62-
* @var \Wilbur\HyperfSoar\SoarService
63-
*/
64-
protected $service;
6547

66-
// public function __construct(ContainerInterface $container)
67-
// {
68-
// $this->service = $container->get(SoarService::class);
69-
// $this->config = $container->get(ConfigInterface::class)->get('soar');
70-
// $this->classes = $this->config['cut_classes'] ?? ['Hyperf\HttpServer\Response::json',];
71-
// }
48+
protected array $config;
49+
50+
#[Inject]
51+
protected SoarService $service;
52+
53+
public function __construct(ContainerInterface $container)
54+
{
55+
$this->service = $container->get(SoarService::class);
56+
$this->config = $container->get(ConfigInterface::class)->get('soar');
57+
if (isset($this->config['cut_classes']) && !empty($this->config['cut_classes'])) {
58+
$this->classes = $this->config['cut_classes'];
59+
}
60+
}
7261

7362
/**
7463
* @throws \Hyperf\Di\Exception\Exception
7564
* @throws \JsonException
7665
*/
7766
public function process(ProceedingJoinPoint $proceedingJoinPoint)
7867
{
79-
$sqlKey = class_basename(QueryExecListener::class);
80-
81-
if (!$this->config['enabled'] || !Context::has($sqlKey) || !is_file($this->config['-soar-path'])) {
68+
$sqlKey = QueryExecListener::SQL_RECORD_KEY;
69+
if (!$this->config['enabled']) {
8270
return $proceedingJoinPoint->process();
8371
}
8472

@@ -97,10 +85,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
9785
foreach ($eventSqlList as $sql) {
9886
co(function () use ($sql, $channel) {
9987
$soar = $this->service->score($sql);
100-
$explain = [
101-
'query' => $sql,
102-
'explain' => $this->formatting($soar),
103-
];
88+
$explain = $this->formatting($soar);
10489
$channel->push($explain);
10590
});
10691
$explains[] = $channel->pop();
@@ -145,6 +130,7 @@ protected function getScore(?string $severity = null): ?int
145130
protected function formatting(string $json): array
146131
{
147132
$results = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
133+
return $results;
148134
$results = Arr::flatten($results, 1);
149135

150136
$items = [];

src/Exec.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,21 @@
1717

1818
use Guanguans\SoarPHP\Exceptions\InvalidArgumentException;
1919
use Guanguans\SoarPHP\Exceptions\RuntimeException;
20+
use Guanguans\SoarPHP\Support\OsHelper;
2021
use Swoole\Coroutine\System;
2122

2223
trait Exec
2324
{
2425
/**
2526
* @param string $command
2627
*
27-
* @throws \Guanguans\SoarPHP\Exceptions\InvalidArgumentException
2828
* @throws \Guanguans\SoarPHP\Exceptions\RuntimeException
2929
*
3030
* @return mixed
3131
*/
3232
public function exec(string $command): string
3333
{
34-
if (!is_string($command)) {
35-
throw new InvalidArgumentException('Command type must be a string');
36-
}
37-
38-
if (stripos($command, 'soar') === false) {
39-
throw new InvalidArgumentException(sprintf("Command error: '%s'", $command));
40-
}
41-
34+
OsHelper::isWindows() && $command = 'powershell '.$command;
4235
$result = System::exec($command);
4336

4437
if ($result['code'] !== 0) {

src/Listener/QueryExecListener.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,11 @@
2525

2626
use function class_basename;
2727

28-
/**
29-
* @Listener
30-
*/
28+
#[Listener]
3129
class QueryExecListener implements ListenerInterface
3230
{
33-
/**
34-
* @var bool
35-
*/
36-
protected $soarIsEnabled;
31+
protected bool $soarIsEnabled;
32+
public const SQL_RECORD_KEY = 'wilbur-yu-hyperf-soar-sql-listener';
3733

3834
public function __construct(ConfigInterface $config)
3935
{
@@ -56,9 +52,9 @@ public function process(object $event): void
5652
$sql = Str::replaceFirst('?', "'{$value}'", $sql);
5753
}
5854
}
59-
$eventSqlList = (array)Context::get(class_basename(__CLASS__));
55+
$eventSqlList = (array)Context::get(self::SQL_RECORD_KEY);
6056
$eventSqlList[] = $sql;
61-
Context::set(class_basename(__CLASS__), $eventSqlList);
57+
Context::set(self::SQL_RECORD_KEY, $eventSqlList);
6258
}
6359
}
6460
}

src/SoarService.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
use Guanguans\SoarPHP\Soar;
1919

20+
use Guanguans\SoarPHP\Support\OsHelper;
21+
2022
use function config;
2123

2224
class SoarService extends Soar
@@ -26,9 +28,21 @@ class SoarService extends Soar
2628
public function __construct(array $config = null)
2729
{
2830
$config = $config ?? config('soar');
29-
unset($config['enabled'], $config['cut_classes']);
31+
$soarPath = $config['-soar-path'];
3032
$config['-report-type'] = 'json';
33+
unset($config['enabled'], $config['cut_classes'], $config['-soar-path']);
34+
parent::__construct($config, $soarPath);
35+
}
36+
37+
protected function getDefaultSoarPath(): string
38+
{
39+
if (OsHelper::isWindows()) {
40+
return __DIR__.'/../vendor/guanguans/soar-php/bin/soar.windows-amd64';
41+
}
42+
if (OsHelper::isMacOS()) {
43+
return __DIR__.'/../vendor/guanguans/soar-php/bin/soar.darwin-amd64';
44+
}
3145

32-
parent::__construct($config);
46+
return __DIR__.'/../vendor/guanguans/soar-php/bin/soar.linux-amd64';
3347
}
3448
}

0 commit comments

Comments
 (0)