77use Illuminate \Database \Events \QueryExecuted ;
88use Illuminate \Events \Dispatcher ;
99use Illuminate \Redis \Events \CommandExecuted ;
10+ use Webman \Http \Request ;
11+ use Webman \Http \Response ;
12+ use Webman \MiddlewareInterface ;
13+ use Throwable ;
14+ use RuntimeException ;
1015use support \Db ;
1116use support \Log ;
1217use support \Redis ;
1318use think \db \connector \Mysql ;
19+ use think \DbManager ;
1420use think \facade \Db as ThinkDb ;
15- use Throwable ;
16- use Webman \Http \Request ;
17- use Webman \Http \Response ;
18- use Webman \MiddlewareInterface ;
19- use RuntimeException ;
21+ use think \Container as ThinkContainer ;
2022
2123class Middleware implements MiddlewareInterface
2224{
@@ -85,19 +87,19 @@ public function process(Request $request, callable $next): Response
8587 }
8688
8789 // 判断Db是否有未提交的事务
88- $ has_uncommited_transaction = false ;
90+ $ has_uncommitted_transaction = false ;
8991 if (class_exists (Connection::class, false )) {
90- if ($ log = $ this ->checkDbUncommitTransaction ()) {
91- $ has_uncommited_transaction = true ;
92+ if ($ log = $ this ->checkDbUncommittedTransaction ()) {
93+ $ has_uncommitted_transaction = true ;
9294 $ method = 'error ' ;
9395 $ logs .= $ log ;
9496 }
9597 }
9698
9799 // 判断think-orm是否有未提交的事务
98100 if ($ loaded_think_db ) {
99- if ($ log = $ this ->checkTpUncommitTransaction ()) {
100- $ has_uncommited_transaction = true ;
101+ if ($ log = $ this ->checkTpUncommittedTransaction ()) {
102+ $ has_uncommitted_transaction = true ;
101103 $ method = 'error ' ;
102104 $ logs .= $ log ;
103105 }
@@ -114,7 +116,7 @@ public function process(Request $request, callable $next): Response
114116
115117 call_user_func ([Log::class, $ method ], $ logs );
116118
117- if ($ has_uncommited_transaction ) {
119+ if ($ has_uncommitted_transaction ) {
118120 throw new RuntimeException ('Uncommitted transactions found ' );
119121 }
120122
@@ -161,31 +163,35 @@ protected function initDbListen()
161163 /**
162164 * 尝试初始化redis日志监听
163165 *
164- * @return void
166+ * @return array
165167 */
166- protected function tryInitRedisListen ()
168+ protected function tryInitRedisListen (): array
167169 {
168170 static $ listened_names = [];
169171 if (!class_exists (CommandExecuted::class)) {
170172 return [];
171173 }
172174 $ new_names = [];
173- foreach (Redis::instance ()->connections () ?: [] as $ connection ) {
174- /* @var \Illuminate\Redis\Connections\Connection $connection */
175- $ name = $ connection ->getName ();
176- if (isset ($ listened_names [$ name ])) {
177- continue ;
178- }
179- $ connection ->listen (function (CommandExecuted $ command ) {
180- foreach ($ command ->parameters as &$ item ) {
181- if (is_array ($ item )) {
182- $ item = implode ('\', \'' , $ item );
183- }
175+ try {
176+ foreach (Redis::instance ()->connections () ?: [] as $ connection ) {
177+ /* @var \Illuminate\Redis\Connections\Connection $connection */
178+ $ name = $ connection ->getName ();
179+ if (isset ($ listened_names [$ name ])) {
180+ continue ;
184181 }
185- $ this ->logs .= "[Redis] \t[connection: {$ command ->connectionName }] Redis:: {$ command ->command }(' " . implode ('\', \'' , $ command ->parameters ) . "') ( {$ command ->time } ms) " . PHP_EOL ;
186- });
187- $ listened_names [$ name ] = $ name ;
188- $ new_names [] = $ name ;
182+ $ connection ->listen (function (CommandExecuted $ command ) {
183+ foreach ($ command ->parameters as &$ item ) {
184+ if (is_array ($ item )) {
185+ $ item = implode ('\', \'' , $ item );
186+ }
187+ }
188+ $ this ->logs .= "[Redis] \t[connection: {$ command ->connectionName }] Redis:: {$ command ->command }(' " . implode ('\', \'' , $ command ->parameters ) . "') ( {$ command ->time } ms) " . PHP_EOL ;
189+ });
190+ $ listened_names [$ name ] = $ name ;
191+ $ new_names [] = $ name ;
192+ }
193+ } catch (Throwable $ e ) {
194+ echo $ e ;
189195 }
190196 return $ new_names ;
191197 }
@@ -212,21 +218,23 @@ protected function getCapsule()
212218 * 检查Db是否有未提交的事务
213219 *
214220 * @return string
215- * @throws Throwable
216221 */
217- protected function checkDbUncommitTransaction ()
222+ protected function checkDbUncommittedTransaction (): string
218223 {
219224 $ logs = '' ;
220- foreach ($ this ->getCapsule ()->getDatabaseManager ()->getConnections () as $ connection ) {
221- /* @var \Illuminate\Database\MySqlConnection $connection * */
222- if (\in_array ($ connection ->getConfig ('driver ' ), ['mysql ' , 'pgsql ' , 'sqlite ' , 'sqlsrv ' ])) {
223- $ pdo = $ connection ->getPdo ();
224- if ($ pdo && $ pdo ->inTransaction ()) {
225- $ connection ->rollBack ();
226- $ method = 'error ' ;
227- $ logs .= "[ERROR] \tUncommitted transaction found and try to rollback " . PHP_EOL ;
225+ try {
226+ foreach ($ this ->getCapsule ()->getDatabaseManager ()->getConnections () as $ connection ) {
227+ /* @var \Illuminate\Database\MySqlConnection $connection * */
228+ if (\in_array ($ connection ->getConfig ('driver ' ), ['mysql ' , 'pgsql ' , 'sqlite ' , 'sqlsrv ' ])) {
229+ $ pdo = $ connection ->getPdo ();
230+ if ($ pdo && $ pdo ->inTransaction ()) {
231+ $ connection ->rollBack ();
232+ $ logs .= "[ERROR] \tUncommitted transaction found and try to rollback " . PHP_EOL ;
233+ }
228234 }
229235 }
236+ } catch (Throwable $ e ) {
237+ echo $ e ;
230238 }
231239 return $ logs ;
232240 }
@@ -235,30 +243,37 @@ protected function checkDbUncommitTransaction()
235243 * 检查think-orm是否有未提交的事务
236244 *
237245 * @return string
238- * @throws \ReflectionException
239246 */
240- protected function checkTpUncommitTransaction ()
247+ protected function checkTpUncommittedTransaction (): string
241248 {
242- static $ reflect , $ instance ;
243- if (!$ reflect ) {
244- $ reflect = new \ReflectionClass (\think \facade \Db::class);
245- $ property = $ reflect ->getProperty ('instance ' );
246- $ property ->setAccessible (true );
247- $ instance = $ property ->getValue ();
248- $ reflect = new \ReflectionClass ($ property ->getValue ());
249- }
250- $ property = $ reflect ->getProperty ('instance ' );
251- $ property ->setAccessible (true );
252- $ instances = $ property ->getValue ($ instance );
249+ static $ property , $ manager_instance ;
253250 $ logs = '' ;
254- foreach ($ instances as $ connection ) {
255- /* @var \think\db\connector\Mysql $connection */
256- $ pdo = $ connection ->getPdo ();
257- if ($ pdo && $ pdo ->inTransaction ()) {
258- $ connection ->rollBack ();
259- $ method = 'error ' ;
260- $ logs .= "[ERROR] \tUncommitted transaction found and try to rollback " . PHP_EOL ;
251+ try {
252+ if (!$ property ) {
253+ if (class_exists (ThinkContainer::class, false )) {
254+ $ manager_instance = ThinkContainer::getInstance ()->make (DbManager::class);
255+ } else {
256+ $ reflect = new \ReflectionClass (ThinkDb::class);
257+ $ property = $ reflect ->getProperty ('instance ' );
258+ $ property ->setAccessible (true );
259+ $ manager_instance = $ property ->getValue ();
260+ }
261+ $ reflect = new \ReflectionClass ($ manager_instance );
262+ $ property = $ reflect ->getProperty ('instance ' );
263+ $ property ->setAccessible (true );
264+ }
265+
266+ $ instances = $ property ->getValue ($ manager_instance );
267+ foreach ($ instances as $ connection ) {
268+ /* @var \think\db\connector\Mysql $connection */
269+ $ pdo = $ connection ->getPdo ();
270+ if ($ pdo && $ pdo ->inTransaction ()) {
271+ $ connection ->rollBack ();
272+ $ logs .= "[ERROR] \tUncommitted transaction found and try to rollback " . PHP_EOL ;
273+ }
261274 }
275+ } catch (Throwable $ e ) {
276+ echo $ e ;
262277 }
263278 return $ logs ;
264279 }
@@ -269,7 +284,7 @@ protected function checkTpUncommitTransaction()
269284 * @param Throwable $e
270285 * @return bool
271286 */
272- protected function shouldntReport ($ e )
287+ protected function shouldntReport ($ e ): bool
273288 {
274289 foreach (config ('plugin.webman.log.app.exception.dontReport ' , []) as $ type ) {
275290 if ($ e instanceof $ type ) {
0 commit comments