@@ -25,7 +25,7 @@ class Middleware implements MiddlewareInterface
2525 /**
2626 * @var string
2727 */
28- public $ logs = '' ;
28+ public static $ logs = '' ;
2929
3030 /**
3131 * @param Request $request
@@ -41,7 +41,7 @@ public function process(Request $request, callable $next): Response
4141
4242 // 记录ip 请求等信息
4343 $ logs = $ request ->getRealIp () . ' ' . $ request ->method () . ' ' . trim ($ request ->fullUrl (), '/ ' );
44- $ this -> logs = '' ;
44+ static :: $ logs = '' ;
4545
4646 // 清理think-orm的日志
4747 if (class_exists (ThinkDb::class, false ) && class_exists (Mysql::class, false )) {
@@ -61,7 +61,7 @@ public function process(Request $request, callable $next): Response
6161 if ($ request ->method () === 'POST ' ) {
6262 $ logs .= "[POST] \t" . var_export ($ request ->post (), true ) . PHP_EOL ;
6363 }
64- $ logs .= $ this -> logs ;
64+ $ logs .= static :: $ logs ;
6565
6666 // think-orm如果被使用,则记录think-orm的日志
6767 if ($ loaded_think_db = (class_exists (ThinkDb::class, false ) && class_exists (Mysql::class, false ))) {
@@ -135,7 +135,10 @@ protected function initDbListen()
135135 }
136136 try {
137137 $ capsule = $ this ->getCapsule ();
138- $ dispatcher = new Dispatcher (new Container );
138+ $ dispatcher = $ capsule ->getEventDispatcher ();
139+ if (!$ dispatcher ) {
140+ $ dispatcher = new Dispatcher (new Container );
141+ }
139142 $ dispatcher ->listen (QueryExecuted::class, function (QueryExecuted $ query ) {
140143 $ sql = trim ($ query ->sql );
141144 if (strtolower ($ sql ) === 'select 1 ' ) {
@@ -155,7 +158,7 @@ protected function initDbListen()
155158 try {
156159 $ log = vsprintf ($ sql , $ query ->bindings );
157160 } catch (\Throwable $ e ) {}
158- $ this -> logs .= "[SQL] \t[connection: {$ query ->connectionName }] $ log [ {$ query ->time } ms] " . PHP_EOL ;
161+ static :: $ logs .= "[SQL] \t[connection: {$ query ->connectionName }] $ log [ {$ query ->time } ms] " . PHP_EOL ;
159162 });
160163 $ capsule ->setEventDispatcher ($ dispatcher );
161164 } catch (\Throwable $ e ) {
@@ -188,7 +191,7 @@ protected function tryInitRedisListen(): array
188191 $ item = implode ('\', \'' , $ item );
189192 }
190193 }
191- $ this -> logs .= "[Redis] \t[connection: {$ command ->connectionName }] Redis:: {$ command ->command }(' " . implode ('\', \'' , $ command ->parameters ) . "') ( {$ command ->time } ms) " . PHP_EOL ;
194+ static :: $ logs .= "[Redis] \t[connection: {$ command ->connectionName }] Redis:: {$ command ->command }(' " . implode ('\', \'' , $ command ->parameters ) . "') ( {$ command ->time } ms) " . PHP_EOL ;
192195 });
193196 $ listened_names [$ name ] = $ name ;
194197 $ new_names [] = $ name ;
0 commit comments