Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4abd28

Browse files
author
guanwei
committed
Feature: support windows
1 parent d18475f commit a4abd28

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

SensorsAnalytics.php

+36-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
define('SENSORS_ANALYTICS_SDK_VERSION', '1.7.2');
3+
define('SENSORS_ANALYTICS_SDK_VERSION', '1.10.0');
44

55
class SensorsAnalyticsException extends \Exception {
66
}
@@ -21,16 +21,17 @@ class SensorsAnalytics {
2121

2222
private $_consumer;
2323
private $_super_properties;
24-
24+
private $_is_win;
2525
/**
2626
* 初始化一个 SensorsAnalytics 的实例用于数据发送。
2727
*
2828
* @param AbstractConsumer $consumer
2929
*/
3030
public function __construct($consumer) {
31+
$this->_is_win = false;
3132
// 不支持 Windows,因为 Windows 版本的 PHP 都不支持 long
3233
if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") {
33-
throw new SensorsAnalyticsException("Sensors Analytics PHP SDK dons't not support Windows");
34+
$this->_is_win = true;
3435
}
3536
$this->_consumer = $consumer;
3637
$this->clear_super_properties();
@@ -47,14 +48,33 @@ private function _normalize_data($data) {
4748
$data['distinct_id'] = strval($data['distinct_id']);
4849

4950
// 检查 time
50-
$ts = (int)($data['time']);
51-
$ts_num = strlen($ts);
52-
if ($ts_num < 10 || $ts_num > 13) {
53-
throw new SensorsAnalyticsIllegalDataException("property [time] must be a timestamp in microseconds");
54-
}
51+
if ($this->_is_win) { // windows use string(windows 32bit do not support int64)
52+
if (!is_string($data['time'])) {
53+
throw new SensorsAnalyticsIllegalDataException("property [time] type must be string");
54+
}
55+
$ts = $data['time'];
56+
$ts_num = strlen($ts);
57+
if (strlen($ts_num) == 15) {
58+
$ts = substr($ts, 0, 13);
59+
}
60+
61+
if ($ts_num < 10 || $ts_num > 13) {
62+
throw new SensorsAnalyticsIllegalDataException("property [time] must be a timestamp in microseconds");
63+
}
5564

56-
if ($ts_num == 10) {
57-
$ts *= 1000;
65+
if ($ts_num == 10) {
66+
$ts .= "000";
67+
}
68+
} else { // linux use int
69+
$ts = (int)($data['time']);
70+
$ts_num = strlen($ts);
71+
if ($ts_num < 10 || $ts_num > 13) {
72+
throw new SensorsAnalyticsIllegalDataException("property [time] must be a timestamp in microseconds");
73+
}
74+
75+
if ($ts_num == 10) {
76+
$ts *= 1000;
77+
}
5878
}
5979
$data['time'] = $ts;
6080

@@ -119,15 +139,19 @@ private function _normalize_data($data) {
119139
* 如果用户传入了 $time 字段,则不使用当前时间。
120140
*
121141
* @param array $properties
122-
* @return int
142+
* @return int/string
123143
*/
124144
private function _extract_user_time(&$properties = array()) {
125145
if (array_key_exists('$time', $properties)) {
126146
$time = $properties['$time'];
127147
unset($properties['$time']);
128148
return $time;
129149
}
130-
return (int)(microtime(true) * 1000);
150+
if ($this->_is_win) { // windows return string
151+
return substr((microtime(true) * 1000), 0, 13);
152+
} else {
153+
return (int)(microtime(true) * 1000);
154+
}
131155
}
132156

133157
/**

0 commit comments

Comments
 (0)