AppendSqlDebugMiddleware.php 799 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\PHPInfo\Middleware;
  4. use Closure;
  5. use think\DbManager;
  6. use think\Request;
  7. use think\Response;
  8. class AppendSqlDebugMiddleware
  9. {
  10. private array $logList = [];
  11. public function __construct(private readonly DbManager $dbManager)
  12. {
  13. $this->dbManager->setLog(function (string $type, string $log) {
  14. $this->logList[] = $log;
  15. });
  16. }
  17. public function handle(Request $request, Closure $next): Response
  18. {
  19. $response = $next($request);
  20. if ($response instanceof Response && is_array($data = $response->getData())) {
  21. $data['sql'] = $this->logList;
  22. if (!empty($data['sql'])) {
  23. $response->data($data);
  24. }
  25. }
  26. return $response;
  27. }
  28. }