/** * 上报统计数据 * * @param string $module * @param string $interface * @param bool $success * @param int $code * @param string $msg * @param string $reportAddress * @return boolean */ public static function report($module, $interface, $success, $code, $msg, $reportAddress = '') { // 如果msg是个数组,那么要额外处理转换成字符串 if (is_array($msg)) { // $msg格式为[':message', [':message' => 'TEST']] if (count($msg) == 2 && !Arr::isAssoc($msg) && is_array($msg[1])) { $msg = __($msg[0], $msg[1]); } } if (strpos($msg, '[ip]') !== false) { $msg = str_replace('[ip]', Arr::get($_SERVER, 'REMOTE_ADDR'), $msg); } if (strpos($msg, '[ua]') !== false) { $msg = str_replace('[ua]', Arr::get($_SERVER, 'HTTP_USER_AGENT'), $msg); } $reportAddress = $reportAddress ? $reportAddress : Config::load('statClient')->get('ip'); if (isset(self::$timeMap[$module][$interface]) && self::$timeMap[$module][$interface] > 0) { $startTime = self::$timeMap[$module][$interface]; self::$timeMap[$module][$interface] = 0; } else { if (isset(self::$timeMap['']['']) && self::$timeMap[''][''] > 0) { $startTime = self::$timeMap['']['']; self::$timeMap[''][''] = 0; } else { $startTime = microtime(true); } } //echo "\n"; //echo $startTime . "\n"; $endTime = microtime(true); //echo $endTime . "\n"; $costTime = $endTime - $startTime; //echo $costTime . "\n"; $binData = Protocol::encode($module, $interface, $costTime, $success, $code, $msg); Base::getLog()->debug(__METHOD__ . ' prepare bin data', ['bin' => $binData]); return self::sendData($reportAddress, $binData); }
/** * 检测[Arr::isAssoc]功能是否正常 * * @dataProvider dataIsAssoc * @param mixed $input * @param bool $result */ public function testIsAssoc($input, $result) { $this->assertEquals($result, Arr::isAssoc($input)); }
/** * 字符过滤 * * // 返回 "What the #####, man!" * echo Text::censor('What the f**k, man!', [ * 'f**k' => '#####', * ]); * * @param string $str 要过滤的字符串 * @param array $badwordList 敏感词汇列表 * @param string $replacement 默认替换成这个字符 * @return string */ public static function censor($str, $badwordList, $replacement = '#') { // 如果替换列表不是关联数组,那么就手动处理一次 if (!Arr::isAssoc($badwordList)) { $temp = []; foreach ($badwordList as $badword) { $temp[$badword] = str_repeat($replacement, strlen($badword)); } $badwordList = $temp; } foreach ($badwordList as $badword => $replace) { $str = str_replace($badword, $replace, $str); } return $str; }