'SQL 执行层', // PHP 值:~think\\db\\PDOConnection::.*~(每个分隔位置两个反斜杠字符) 'regex' => '~think\\\\db\\\\PDOConnection::.*~', 'note' => 'PDO 连接层查询执行(真实形态实测)', 'threshold_ms' => 500, ], [ 'name' => '业务层控制器', 'regex' => '~app\\\\admin\\\\controller\\\\.*~', 'note' => '控制器中心主义,业务逻辑所在', 'threshold_ms' => 1000, ], [ 'name' => '数据保存操作', 'regex' => '~.*::save$~', 'note' => 'Model/log/session 的 save 调用', 'threshold_ms' => 0, ], ]; foreach ($rows as $row) { $sql = sprintf( "INSERT INTO `%sxhprof_watch` (`name`, `regex`, `note`, `threshold_ms`, `status`, `create_time`, `update_time`) VALUES (%s, %s, %s, %d, 1, %d, %d) ON DUPLICATE KEY UPDATE `id` = `id`", $this->getAdapter()->getOption('table_prefix'), $this->quoteLiteral($row['name']), $this->quoteLiteral($row['regex']), $this->quoteLiteral($row['note']), $row['threshold_ms'], $now, $now ); $this->execute($sql); } } public function down() { $prefix = $this->getAdapter()->getOption('table_prefix'); $regexes = implode(', ', array_map(function ($regex) { return $this->quoteLiteral($regex); }, [ '~think\\\\db\\\\PDOConnection::.*~', '~app\\\\admin\\\\controller\\\\.*~', '~.*::save$~', ])); $this->execute("DELETE FROM `{$prefix}xhprof_watch` WHERE `regex` IN ({$regexes})"); } /** * MySQL 字面量包装:单引号包裹 + 反斜杠转义(保证落库值与 PHP 值逐字符一致). */ private function quoteLiteral(string $value): string { return "'" . addcslashes($value, "\\") . "'"; } }