1515
1616namespace ReinanHS \SqlCommenterHyperf \Aspect ;
1717
18- use Hyperf \Context \Context ;
18+ use Hyperf \Context \RequestContext ;
1919use Hyperf \Contract \ConfigInterface ;
2020use Hyperf \Database \Connection ;
2121use Hyperf \Di \Aop \AbstractAspect ;
2222use Hyperf \Di \Aop \ProceedingJoinPoint ;
2323use Hyperf \HttpServer \Router \Dispatched ;
24- use Psr \Http \Message \ServerRequestInterface ;
2524use ReinanHS \SqlCommenterHyperf \Opentelemetry ;
2625use ReinanHS \SqlCommenterHyperf \SwitchManager ;
2726use ReinanHS \SqlCommenterHyperf \Utils ;
@@ -38,10 +37,11 @@ public function __construct(private readonly ConfigInterface $config, private re
3837
3938 public function process (ProceedingJoinPoint $ proceedingJoinPoint )
4039 {
41- if (! $ this ->config ->get ('sqlcommenter.enable ' , true )) {
40+ if (! $ this ->config ->get ('sqlcommenter.enable ' , true ) || ! isset ( $ proceedingJoinPoint -> arguments [ ' keys ' ][ ' query ' ]) ) {
4241 return $ proceedingJoinPoint ->process ();
4342 }
4443
44+ /** @var string $query */
4545 $ query = $ proceedingJoinPoint ->arguments ['keys ' ]['query ' ];
4646
4747 /** @var Connection $dbInstance */
@@ -61,33 +61,31 @@ private function appendSqlComments(string $query, string $dbDriver): string
6161 }
6262
6363 if ($ this ->switchManager ->isEnable ('application ' )) {
64- $ comments ['application ' ] = $ this ->config ->get ('app_name ' );
64+ $ comments ['application ' ] = ( string ) $ this ->config ->get ('app_name ' );
6565 }
6666
6767 if ($ this ->switchManager ->isEnable ('db_driver ' )) {
6868 $ comments ['db_driver ' ] = $ dbDriver ;
6969 }
7070
71- $ request = Context::get (ServerRequestInterface::class);
72- if ($ request ) {
73- if ($ this ->switchManager ->isEnable ('route ' )) {
74- $ comments ['route ' ] = $ request ->getUri ()->getPath ();
75- }
71+ $ request = RequestContext::get ();
72+ if ($ this ->switchManager ->isEnable ('route ' )) {
73+ $ comments ['route ' ] = $ request ->getUri ()->getPath ();
74+ }
7675
77- if ($ this ->switchManager ->isEnable ('controller ' ) || $ this ->switchManager ->isEnable ('action ' )) {
78- /** @var null|Dispatched $dispatched */
79- $ dispatched = $ request ->getAttribute (Dispatched::class);
76+ if ($ this ->switchManager ->isEnable ('controller ' ) || $ this ->switchManager ->isEnable ('action ' )) {
77+ /** @var null|Dispatched $dispatched */
78+ $ dispatched = $ request ->getAttribute (Dispatched::class);
8079
81- if ($ dispatched && $ dispatched ->isFound ()) {
82- $ parts = Utils::extractCallback ($ dispatched ->handler ?->callback);
80+ if ($ dispatched && $ dispatched ->isFound ()) {
81+ $ parts = Utils::extractCallback ($ dispatched ->handler ?->callback);
8382
84- if ($ this ->switchManager ->isEnable ('controller ' )) {
85- $ comments ['controller ' ] = $ parts [0 ];
86- }
83+ if ($ this ->switchManager ->isEnable ('controller ' )) {
84+ $ comments ['controller ' ] = ( string ) $ parts [0 ];
85+ }
8786
88- if ($ this ->switchManager ->isEnable ('action ' )) {
89- $ comments ['action ' ] = $ parts [1 ];
90- }
87+ if ($ this ->switchManager ->isEnable ('action ' )) {
88+ $ comments ['action ' ] = (string ) $ parts [1 ];
9189 }
9290 }
9391 }
0 commit comments