1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace XHGui\Saver; |
4
|
|
|
|
5
|
|
|
use MongoCollection; |
6
|
|
|
use MongoDate; |
7
|
|
|
use MongoId; |
8
|
|
|
|
9
|
|
|
class MongoSaver implements SaverInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var MongoCollection |
13
|
|
|
*/ |
14
|
|
|
private $_collection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var MongoId lastProfilingId |
18
|
|
|
*/ |
19
|
|
|
private static $lastProfilingId; |
20
|
|
|
|
21
|
|
|
public function __construct(MongoCollection $collection) |
22
|
|
|
{ |
23
|
|
|
$this->_collection = $collection; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function save(array $data) |
27
|
|
|
{ |
28
|
|
|
// build 'request_ts' and 'request_date' from 'request_ts_micro' |
29
|
|
|
$ts = $data['meta']['request_ts_micro']; |
30
|
|
|
$sec = $ts['sec']; |
31
|
|
|
$usec = $ts['usec']; |
32
|
|
|
|
33
|
|
|
$meta = [ |
34
|
|
|
'url' => $data['meta']['url'], |
35
|
|
|
'get' => $data['meta']['get'], |
36
|
|
|
'env' => $data['meta']['env'], |
37
|
|
|
'SERVER' => $data['meta']['SERVER'], |
38
|
|
|
'simple_url' => $data['meta']['simple_url'], |
39
|
|
|
'request_ts' => new MongoDate($sec), |
40
|
|
|
'request_ts_micro' => new MongoDate($sec, $usec), |
41
|
|
|
'request_date' => date('Y-m-d', $sec), |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
$a = [ |
45
|
|
|
'_id' => $data['_id'] ?? self::getLastProfilingId(), |
46
|
|
|
'meta' => $meta, |
47
|
|
|
'profile' => $data['profile'], |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
return $this->_collection->insert($a, ['w' => 0]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return profiling ID |
55
|
|
|
* @return MongoId lastProfilingId |
56
|
|
|
*/ |
57
|
|
|
public static function getLastProfilingId() |
58
|
|
|
{ |
59
|
|
|
if (!self::$lastProfilingId) { |
60
|
|
|
self::$lastProfilingId = new MongoId(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return self::$lastProfilingId; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|