Skip to content

Commit fbdfa35

Browse files
committed
增加切入类可配置项
1 parent d605bd2 commit fbdfa35

File tree

7 files changed

+63
-165
lines changed

7 files changed

+63
-165
lines changed

.php-cs-fixer.php

Lines changed: 6 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22

33
$header = <<<'EOF'
4-
This file is part of Hyperf.
4+
This file is part of project hyperf-soar.
5+
6+
@author wenbo@wenber.club
7+
@link https://github.com/wilbur-yu
58
69
@link https://www.hyperf.io
710
@document https://hyperf.wiki
@@ -10,123 +13,11 @@
1013
EOF;
1114

1215
return (new PhpCsFixer\Config())->setRiskyAllowed(true)
13-
->setRules([
14-
'@PSR2' => true, // 开启预设的规则
15-
'@Symfony' => true, // 开启预设的规则
16-
'@DoctrineAnnotation' => true, // 开启预设的规则
17-
'@PhpCsFixer' => true,
18-
'header_comment' => [
19-
'comment_type' => 'PHPDoc',
20-
'header' => $header,
21-
'separate' => 'none',
22-
'location' => 'after_declare_strict',
23-
],
24-
'array_syntax' => [
25-
'syntax' => 'short', // array 使用短声明
26-
],
27-
'list_syntax' => [
28-
'syntax' => 'short', // list 使用短声明
29-
],
30-
'concat_space' => [
31-
'spacing' => 'one', // 点拼接必须前后有空格分割
32-
],
33-
'declare_equal_normalize' => [
34-
'space' => 'single', // 严格类型声明语句中的等号应包含空格
35-
],
36-
'blank_line_before_statement' => [ // 空行换行必须在任何已配置的语句之前
37-
'statements' => [
38-
'break',
39-
'continue',
40-
'declare',
41-
'return',
42-
'throw',
43-
'try'
44-
],
45-
],
46-
'general_phpdoc_annotation_remove' => [
47-
'annotations' => [
48-
'author'
49-
],
50-
],
51-
'ordered_imports' => [
52-
'imports_order' => [
53-
'class', 'function', 'const', // 按顺序use导入
54-
],
55-
'sort_algorithm' => 'alpha',
56-
],
57-
'single_line_comment_style' => [
58-
'comment_types' => [
59-
],
60-
],
61-
'yoda_style' => [
62-
'always_move_variable' => false,
63-
'equal' => false,
64-
'identical' => false,
65-
],
66-
'multiline_whitespace_before_semicolons' => [
67-
'strategy' => 'no_multi_line',
68-
],
69-
'constant_case' => [
70-
'case' => 'lower', // 常量(true,false,null)使用大写(upper)还是小写(lower)语法
71-
],
72-
'binary_operator_spaces' => [
73-
'default' => 'align_single_space', // 等号对齐、数字箭头符号对齐
74-
],
75-
'increment_style' => true, // 自增自减运算符开启前置样式
76-
'lowercase_cast' => false, // 类型强制小写
77-
'array_indentation' => true, // 数组的每个元素必须缩进一次
78-
'no_superfluous_phpdoc_tags' => false, // 移出没有用的注释
79-
'normalize_index_brace' => true,
80-
'class_attributes_separation' => true,
81-
'combine_consecutive_unsets' => true, // 当多个 unset 使用的时候,合并处理
82-
'declare_strict_types' => true,
83-
'linebreak_after_opening_tag' => true,
84-
'lowercase_static_reference' => true, // 静态调用为小写
85-
'no_useless_else' => true, // 删除没有使用的else节点
86-
'no_useless_return' => true, // 删除没有使用的return语句
87-
'no_unused_imports' => true, // 删除没用到的use
88-
'no_singleline_whitespace_before_semicolons' => true, //禁止只有单行空格和分号的写法
89-
'not_operator_with_successor_space' => true,
90-
'not_operator_with_space' => false,
91-
'ordered_class_elements' => true,
92-
'php_unit_strict' => false,
93-
'single_quote' => true, // 简单字符串应该使用单引号代替双引号
94-
'standardize_not_equals' => true, // 使用 <> 代替 !=
95-
'multiline_comment_opening_closing' => true,
96-
'self_accessor' => true, // 在当前类中使用 self 代替类名
97-
'no_whitespace_in_blank_line' => true, // 删除空行中的空格
98-
'no_empty_statement' => true, // 去除多余的分号
99-
'no_extra_blank_lines' => true,
100-
'no_blank_lines_after_class_opening' => true,
101-
'include' => true,
102-
'no_trailing_comma_in_list_call' => true,
103-
'no_leading_namespace_whitespace' => true,
104-
105-
// 在方法参数和方法调用中,每个逗号之前不能有空格,每个逗号之后必须有空格。参数列表可以分成多行,后面的每一行都缩进一次。这样做时,列表中的第一项必须在下一行上,并且每行必须只有一个参数。
106-
'method_argument_space' => [
107-
'on_multiline' => 'ensure_fully_multiline', // 确保多行参数列表中的每个参数都在自己的行上
108-
'after_heredoc' => false, // 是否应删除heredoc末尾和逗号之间的空格
109-
'keep_multiple_spaces_after_comma' => false, // 逗号后是否保留多个空格
110-
],
111-
112-
'phpdoc_separation' => true, // 不同注释部分按照单空行隔开
113-
'phpdoc_single_line_var_spacing' => true,
114-
'phpdoc_indent' => true, // phpdoc 缩进
115-
'phpdoc_align' => [
116-
'align' => 'vertical',
117-
'tags' => [
118-
'param', 'return', 'throws', 'type', 'var', 'property'
119-
]
120-
],
121-
'align_multiline_comment' => [
122-
'comment_type' => 'all_multiline', // 多行注释的每一行都必须带有星号[PSR-5],并且必须与第一行对齐。
123-
],
124-
])
16+
->setRules(['@PSR12' => true])
12517
->setFinder(
12618
PhpCsFixer\Finder::create()
12719
->exclude('public')
12820
->exclude('runtime')
12921
->exclude('vendor')
13022
->in(__DIR__)
131-
)
132-
->setUsingCache(false);
23+
)->setUsingCache(false);

publish/soar.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

3-
declare(strict_types = 1);
3+
declare(strict_types=1);
4+
45
/**
56
* This file is part of project hyperf-soar.
67
*
@@ -13,18 +14,21 @@
1314
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
1415
*/
1516
return [
16-
'enabled' => env('SOAR_ENABLED', env('APP_ENV') === 'local'),
17+
'enabled' => env('SOAR_ENABLED', env('APP_ENV') === 'local'),
18+
'cut_classes' => [
19+
'Hyperf\HttpServer\Response::json',
20+
],
1721
'-soar-path' => env('SOAR_PATH', ''), // soar 二进制文件的绝对路径
18-
'-test-dsn' => [
19-
'host' => env('SOAR_TEST_DSN_HOST', '127.0.0.1'),
20-
'port' => env('SOAR_TEST_DSN_PORT', '3306'),
21-
'dbname' => env('SOAR_TEST_DSN_DBNAME', 'database'),
22+
'-test-dsn' => [
23+
'host' => env('SOAR_TEST_DSN_HOST', '127.0.0.1'),
24+
'port' => env('SOAR_TEST_DSN_PORT', '3306'),
25+
'dbname' => env('SOAR_TEST_DSN_DBNAME', 'database'),
2226
'username' => env('SOAR_TEST_DSN_USER', 'root'),
2327
'password' => env('SOAR_TEST_DSN_PASSWORD', ''),
24-
'disable' => env('SOAR_TEST_DSN_DISABLE', false),
28+
'disable' => env('SOAR_TEST_DSN_DISABLE', false),
2529
],
26-
'-sampling' => env('SOAR_SAMPLING', true), // 是否开启数据采样开关
27-
'-allow-drop-index' => env('SOAR_ALLOW_DROP_INDEX', true), // 允许输出删除重复索引的建议
30+
'-sampling' => env('SOAR_SAMPLING', true), // 是否开启数据采样开关
31+
'-allow-drop-index' => env('SOAR_ALLOW_DROP_INDEX', true), // 允许输出删除重复索引的建议
2832
'-drop-test-temporary' => env('SOAR_DROP_TEST_TEMPORARY', true), // 是否清理测试环境产生的临时库表
29-
'-log-output' => BASE_PATH . '/runtime/logs/soar.log',
33+
'-log-output' => BASE_PATH.'/runtime/logs/soar.log',
3034
];

src/Aspect/ResponseAspect.php

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare(strict_types = 1);
3+
declare(strict_types=1);
44
/**
55
* This file is part of project hyperf-soar.
66
*
@@ -12,9 +12,11 @@
1212
* @contact group@hyperf.io
1313
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
1414
*/
15+
1516
namespace Wilbur\HyperfSoar\Aspect;
1617

1718
use Hyperf\Config\Annotation\Value;
19+
use Hyperf\Contract\ConfigInterface;
1820
use Hyperf\Di\Annotation\Aspect;
1921
use Hyperf\Di\Annotation\Inject;
2022
use Hyperf\Di\Aop\AbstractAspect;
@@ -27,56 +29,51 @@
2729
use Throwable;
2830
use Wilbur\HyperfSoar\Listener\QueryExecListener;
2931
use Wilbur\HyperfSoar\SoarService;
32+
use Psr\Container\ContainerInterface;
33+
3034
use function array_merge;
3135
use function class_basename;
3236
use function co;
3337
use function explode;
3438
use function json_decode;
3539
use function json_encode;
40+
3641
use const JSON_UNESCAPED_UNICODE;
3742

3843
/**
3944
* @Aspect
4045
*/
4146
class ResponseAspect extends AbstractAspect
4247
{
43-
public $classes = [
44-
'Hyperf\HttpServer\Response::json',
45-
];
46-
47-
/**
48-
* @Value("soar")
49-
*
50-
* @var array
51-
*/
48+
public $classes;
5249
protected $config;
53-
54-
/**
55-
* @Inject
56-
*
57-
* @var SoarService
58-
*/
5950
protected $service;
6051

52+
public function __construct(ContainerInterface $container)
53+
{
54+
$this->service = $container->get(SoarService::class);
55+
$this->config = $container->get(ConfigInterface::class)->get('soar');
56+
$this->classes = $this->config['cut_classes'] ?? ['Hyperf\HttpServer\Response::json',];
57+
}
58+
6159
/**
62-
* @throws \Guanguans\SoarPHP\Exceptions\InvalidArgumentException
6360
* @throws \Hyperf\Di\Exception\Exception
6461
* @throws \JsonException
6562
*/
6663
public function process(ProceedingJoinPoint $proceedingJoinPoint)
6764
{
6865
$sqlKey = class_basename(QueryExecListener::class);
6966

70-
if (! $this->config['enabled'] || ! Context::has($sqlKey) || ! is_file($this->config['-soar-path'])) {
67+
if (!$this->config['enabled'] || !Context::has($sqlKey) || !is_file($this->config['-soar-path'])) {
7168
return $proceedingJoinPoint->process();
7269
}
7370

7471
$eventSqlList = Context::get($sqlKey);
7572

7673
$explains = [];
77-
$channel = new Channel();
74+
$channel = new Channel();
7875
$response = $proceedingJoinPoint->process();
79-
$oldBody = json_decode(
76+
$oldBody = json_decode(
8077
$response->getBody()->getContents(),
8178
true,
8279
512,
@@ -85,9 +82,9 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
8582
try {
8683
foreach ($eventSqlList as $sql) {
8784
co(function () use ($sql, $channel) {
88-
$soar = $this->service->score($sql);
85+
$soar = $this->service->score($sql);
8986
$explain = [
90-
'query' => $sql,
87+
'query' => $sql,
9188
'explain' => $this->formatting($soar),
9289
];
9390
$channel->push($explain);
@@ -96,10 +93,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
9693
}
9794
} catch (Throwable $throwable) {
9895
$explains = [
99-
'code' => $throwable->getCode(),
96+
'code' => $throwable->getCode(),
10097
'message' => $throwable->getMessage(),
101-
'file' => $throwable->getFile(),
102-
'line' => $throwable->getLine(),
98+
'file' => $throwable->getFile(),
99+
'line' => $throwable->getLine(),
103100
];
104101
}
105102

@@ -113,15 +110,15 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
113110

114111
protected function getScore(?string $severity = null): ?int
115112
{
116-
if (! $severity) {
113+
if (!$severity) {
117114
return null;
118115
}
119116
$fullScore = 100;
120117
$unitScore = 5;
121-
$levels = explode(',', $severity);
122-
$subScore = 0;
118+
$levels = explode(',', $severity);
119+
$subScore = 0;
123120
foreach ($levels as $level) {
124-
$level = (int) Str::after($level, 'L');
121+
$level = (int)Str::after($level, 'L');
125122
$subScore += ($level * $unitScore);
126123
}
127124

src/ConfigProvider.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare(strict_types = 1);
3+
declare(strict_types=1);
44
/**
55
* This file is part of project hyperf-soar.
66
*
@@ -12,6 +12,7 @@
1212
* @contact group@hyperf.io
1313
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
1414
*/
15+
1516
namespace Wilbur\HyperfSoar;
1617

1718
use Wilbur\HyperfSoar\Aspect\ResponseAspect;
@@ -37,10 +38,10 @@ public function __invoke(): array
3738
],
3839
'publish' => [
3940
[
40-
'id' => 'config',
41+
'id' => 'config',
4142
'description' => 'The config for soar.',
42-
'source' => __DIR__ . '/../publish/soar.php',
43-
'destination' => BASE_PATH . '/config/autoload/soar.php',
43+
'source' => __DIR__.'/../publish/soar.php',
44+
'destination' => BASE_PATH.'/config/autoload/soar.php',
4445
],
4546
],
4647
];

src/Exec.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare(strict_types = 1);
3+
declare(strict_types=1);
44
/**
55
* This file is part of project hyperf-soar.
66
*
@@ -12,6 +12,7 @@
1212
* @contact group@hyperf.io
1313
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
1414
*/
15+
1516
namespace Wilbur\HyperfSoar;
1617

1718
use Guanguans\SoarPHP\Exceptions\InvalidArgumentException;
@@ -21,7 +22,7 @@
2122
trait Exec
2223
{
2324
/**
24-
* @param string $command
25+
* @param string $command
2526
*
2627
* @throws \Guanguans\SoarPHP\Exceptions\InvalidArgumentException
2728
* @throws \Guanguans\SoarPHP\Exceptions\RuntimeException
@@ -30,7 +31,7 @@ trait Exec
3031
*/
3132
public function exec(string $command): string
3233
{
33-
if (! is_string($command)) {
34+
if (!is_string($command)) {
3435
throw new InvalidArgumentException('Command type must be a string');
3536
}
3637

0 commit comments

Comments
 (0)