-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
63 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
<?php | ||
|
||
$header = <<<'EOF' | ||
This file is part of Hyperf. | ||
This file is part of project hyperf-soar. | ||
@author [email protected] | ||
@link https://github.com/wilbur-yu | ||
@link https://www.hyperf.io | ||
@document https://hyperf.wiki | ||
|
@@ -10,123 +13,11 @@ | |
EOF; | ||
|
||
return (new PhpCsFixer\Config())->setRiskyAllowed(true) | ||
->setRules([ | ||
'@PSR2' => true, // 开启预设的规则 | ||
'@Symfony' => true, // 开启预设的规则 | ||
'@DoctrineAnnotation' => true, // 开启预设的规则 | ||
'@PhpCsFixer' => true, | ||
'header_comment' => [ | ||
'comment_type' => 'PHPDoc', | ||
'header' => $header, | ||
'separate' => 'none', | ||
'location' => 'after_declare_strict', | ||
], | ||
'array_syntax' => [ | ||
'syntax' => 'short', // array 使用短声明 | ||
], | ||
'list_syntax' => [ | ||
'syntax' => 'short', // list 使用短声明 | ||
], | ||
'concat_space' => [ | ||
'spacing' => 'one', // 点拼接必须前后有空格分割 | ||
], | ||
'declare_equal_normalize' => [ | ||
'space' => 'single', // 严格类型声明语句中的等号应包含空格 | ||
], | ||
'blank_line_before_statement' => [ // 空行换行必须在任何已配置的语句之前 | ||
'statements' => [ | ||
'break', | ||
'continue', | ||
'declare', | ||
'return', | ||
'throw', | ||
'try' | ||
], | ||
], | ||
'general_phpdoc_annotation_remove' => [ | ||
'annotations' => [ | ||
'author' | ||
], | ||
], | ||
'ordered_imports' => [ | ||
'imports_order' => [ | ||
'class', 'function', 'const', // 按顺序use导入 | ||
], | ||
'sort_algorithm' => 'alpha', | ||
], | ||
'single_line_comment_style' => [ | ||
'comment_types' => [ | ||
], | ||
], | ||
'yoda_style' => [ | ||
'always_move_variable' => false, | ||
'equal' => false, | ||
'identical' => false, | ||
], | ||
'multiline_whitespace_before_semicolons' => [ | ||
'strategy' => 'no_multi_line', | ||
], | ||
'constant_case' => [ | ||
'case' => 'lower', // 常量(true,false,null)使用大写(upper)还是小写(lower)语法 | ||
], | ||
'binary_operator_spaces' => [ | ||
'default' => 'align_single_space', // 等号对齐、数字箭头符号对齐 | ||
], | ||
'increment_style' => true, // 自增自减运算符开启前置样式 | ||
'lowercase_cast' => false, // 类型强制小写 | ||
'array_indentation' => true, // 数组的每个元素必须缩进一次 | ||
'no_superfluous_phpdoc_tags' => false, // 移出没有用的注释 | ||
'normalize_index_brace' => true, | ||
'class_attributes_separation' => true, | ||
'combine_consecutive_unsets' => true, // 当多个 unset 使用的时候,合并处理 | ||
'declare_strict_types' => true, | ||
'linebreak_after_opening_tag' => true, | ||
'lowercase_static_reference' => true, // 静态调用为小写 | ||
'no_useless_else' => true, // 删除没有使用的else节点 | ||
'no_useless_return' => true, // 删除没有使用的return语句 | ||
'no_unused_imports' => true, // 删除没用到的use | ||
'no_singleline_whitespace_before_semicolons' => true, //禁止只有单行空格和分号的写法 | ||
'not_operator_with_successor_space' => true, | ||
'not_operator_with_space' => false, | ||
'ordered_class_elements' => true, | ||
'php_unit_strict' => false, | ||
'single_quote' => true, // 简单字符串应该使用单引号代替双引号 | ||
'standardize_not_equals' => true, // 使用 <> 代替 != | ||
'multiline_comment_opening_closing' => true, | ||
'self_accessor' => true, // 在当前类中使用 self 代替类名 | ||
'no_whitespace_in_blank_line' => true, // 删除空行中的空格 | ||
'no_empty_statement' => true, // 去除多余的分号 | ||
'no_extra_blank_lines' => true, | ||
'no_blank_lines_after_class_opening' => true, | ||
'include' => true, | ||
'no_trailing_comma_in_list_call' => true, | ||
'no_leading_namespace_whitespace' => true, | ||
|
||
// 在方法参数和方法调用中,每个逗号之前不能有空格,每个逗号之后必须有空格。参数列表可以分成多行,后面的每一行都缩进一次。这样做时,列表中的第一项必须在下一行上,并且每行必须只有一个参数。 | ||
'method_argument_space' => [ | ||
'on_multiline' => 'ensure_fully_multiline', // 确保多行参数列表中的每个参数都在自己的行上 | ||
'after_heredoc' => false, // 是否应删除heredoc末尾和逗号之间的空格 | ||
'keep_multiple_spaces_after_comma' => false, // 逗号后是否保留多个空格 | ||
], | ||
|
||
'phpdoc_separation' => true, // 不同注释部分按照单空行隔开 | ||
'phpdoc_single_line_var_spacing' => true, | ||
'phpdoc_indent' => true, // phpdoc 缩进 | ||
'phpdoc_align' => [ | ||
'align' => 'vertical', | ||
'tags' => [ | ||
'param', 'return', 'throws', 'type', 'var', 'property' | ||
] | ||
], | ||
'align_multiline_comment' => [ | ||
'comment_type' => 'all_multiline', // 多行注释的每一行都必须带有星号[PSR-5],并且必须与第一行对齐。 | ||
], | ||
]) | ||
->setRules(['@PSR12' => true]) | ||
->setFinder( | ||
PhpCsFixer\Finder::create() | ||
->exclude('public') | ||
->exclude('runtime') | ||
->exclude('vendor') | ||
->in(__DIR__) | ||
) | ||
->setUsingCache(false); | ||
)->setUsingCache(false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
/** | ||
* This file is part of project hyperf-soar. | ||
* | ||
|
@@ -12,9 +12,11 @@ | |
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Wilbur\HyperfSoar\Aspect; | ||
|
||
use Hyperf\Config\Annotation\Value; | ||
use Hyperf\Contract\ConfigInterface; | ||
use Hyperf\Di\Annotation\Aspect; | ||
use Hyperf\Di\Annotation\Inject; | ||
use Hyperf\Di\Aop\AbstractAspect; | ||
|
@@ -27,56 +29,51 @@ | |
use Throwable; | ||
use Wilbur\HyperfSoar\Listener\QueryExecListener; | ||
use Wilbur\HyperfSoar\SoarService; | ||
use Psr\Container\ContainerInterface; | ||
|
||
use function array_merge; | ||
use function class_basename; | ||
use function co; | ||
use function explode; | ||
use function json_decode; | ||
use function json_encode; | ||
|
||
use const JSON_UNESCAPED_UNICODE; | ||
|
||
/** | ||
* @Aspect | ||
*/ | ||
class ResponseAspect extends AbstractAspect | ||
{ | ||
public $classes = [ | ||
'Hyperf\HttpServer\Response::json', | ||
]; | ||
|
||
/** | ||
* @Value("soar") | ||
* | ||
* @var array | ||
*/ | ||
public $classes; | ||
protected $config; | ||
|
||
/** | ||
* @Inject | ||
* | ||
* @var SoarService | ||
*/ | ||
protected $service; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->service = $container->get(SoarService::class); | ||
$this->config = $container->get(ConfigInterface::class)->get('soar'); | ||
$this->classes = $this->config['cut_classes'] ?? ['Hyperf\HttpServer\Response::json',]; | ||
} | ||
|
||
/** | ||
* @throws \Guanguans\SoarPHP\Exceptions\InvalidArgumentException | ||
* @throws \Hyperf\Di\Exception\Exception | ||
* @throws \JsonException | ||
*/ | ||
public function process(ProceedingJoinPoint $proceedingJoinPoint) | ||
{ | ||
$sqlKey = class_basename(QueryExecListener::class); | ||
|
||
if (! $this->config['enabled'] || ! Context::has($sqlKey) || ! is_file($this->config['-soar-path'])) { | ||
if (!$this->config['enabled'] || !Context::has($sqlKey) || !is_file($this->config['-soar-path'])) { | ||
return $proceedingJoinPoint->process(); | ||
} | ||
|
||
$eventSqlList = Context::get($sqlKey); | ||
|
||
$explains = []; | ||
$channel = new Channel(); | ||
$channel = new Channel(); | ||
$response = $proceedingJoinPoint->process(); | ||
$oldBody = json_decode( | ||
$oldBody = json_decode( | ||
$response->getBody()->getContents(), | ||
true, | ||
512, | ||
|
@@ -85,9 +82,9 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) | |
try { | ||
foreach ($eventSqlList as $sql) { | ||
co(function () use ($sql, $channel) { | ||
$soar = $this->service->score($sql); | ||
$soar = $this->service->score($sql); | ||
$explain = [ | ||
'query' => $sql, | ||
'query' => $sql, | ||
'explain' => $this->formatting($soar), | ||
]; | ||
$channel->push($explain); | ||
|
@@ -96,10 +93,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) | |
} | ||
} catch (Throwable $throwable) { | ||
$explains = [ | ||
'code' => $throwable->getCode(), | ||
'code' => $throwable->getCode(), | ||
'message' => $throwable->getMessage(), | ||
'file' => $throwable->getFile(), | ||
'line' => $throwable->getLine(), | ||
'file' => $throwable->getFile(), | ||
'line' => $throwable->getLine(), | ||
]; | ||
} | ||
|
||
|
@@ -113,15 +110,15 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) | |
|
||
protected function getScore(?string $severity = null): ?int | ||
{ | ||
if (! $severity) { | ||
if (!$severity) { | ||
return null; | ||
} | ||
$fullScore = 100; | ||
$unitScore = 5; | ||
$levels = explode(',', $severity); | ||
$subScore = 0; | ||
$levels = explode(',', $severity); | ||
$subScore = 0; | ||
foreach ($levels as $level) { | ||
$level = (int) Str::after($level, 'L'); | ||
$level = (int)Str::after($level, 'L'); | ||
$subScore += ($level * $unitScore); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
/** | ||
* This file is part of project hyperf-soar. | ||
* | ||
|
@@ -12,6 +12,7 @@ | |
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Wilbur\HyperfSoar; | ||
|
||
use Wilbur\HyperfSoar\Aspect\ResponseAspect; | ||
|
@@ -37,10 +38,10 @@ public function __invoke(): array | |
], | ||
'publish' => [ | ||
[ | ||
'id' => 'config', | ||
'id' => 'config', | ||
'description' => 'The config for soar.', | ||
'source' => __DIR__ . '/../publish/soar.php', | ||
'destination' => BASE_PATH . '/config/autoload/soar.php', | ||
'source' => __DIR__.'/../publish/soar.php', | ||
'destination' => BASE_PATH.'/config/autoload/soar.php', | ||
], | ||
], | ||
]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
/** | ||
* This file is part of project hyperf-soar. | ||
* | ||
|
@@ -12,6 +12,7 @@ | |
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Wilbur\HyperfSoar; | ||
|
||
use Guanguans\SoarPHP\Exceptions\InvalidArgumentException; | ||
|
@@ -21,7 +22,7 @@ | |
trait Exec | ||
{ | ||
/** | ||
* @param string $command | ||
* @param string $command | ||
* | ||
* @throws \Guanguans\SoarPHP\Exceptions\InvalidArgumentException | ||
* @throws \Guanguans\SoarPHP\Exceptions\RuntimeException | ||
|
@@ -30,7 +31,7 @@ trait Exec | |
*/ | ||
public function exec(string $command): string | ||
{ | ||
if (! is_string($command)) { | ||
if (!is_string($command)) { | ||
throw new InvalidArgumentException('Command type must be a string'); | ||
} | ||
|
||
|
Oops, something went wrong.