1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Xhgui_Saver_Mongo implements Xhgui_Saver_Interface |
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* @var MongoCollection |
7
|
|
|
*/ |
8
|
|
|
private $_collection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var MongoId lastProfilingId |
12
|
|
|
*/ |
13
|
|
|
private static $lastProfilingId; |
14
|
|
|
|
15
|
|
|
public function __construct(MongoCollection $collection) |
16
|
|
|
{ |
17
|
|
|
$this->_collection = $collection; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function save(array $data) |
21
|
|
|
{ |
22
|
|
|
if (!isset($data['_id'])) { |
23
|
|
|
$data['_id'] = self::getLastProfilingId(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// Escape profile data keys according to the standard https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names |
27
|
|
|
if (isset($data['profile'])) { |
28
|
|
|
foreach ($data['profile'] as $key => $data) { |
29
|
|
|
$escapedKey = str_replace(array(".", "$"), "_", $key); |
30
|
|
|
$data['profile'][$escapedKey] = $data; |
31
|
|
|
unset($data['profile'][$key]); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (isset($data['meta']['request_ts'])) { |
36
|
|
|
$data['meta']['request_ts'] = new MongoDate($data['meta']['request_ts']['sec']); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (isset($data['meta']['request_ts_micro'])) { |
40
|
|
|
$data['meta']['request_ts_micro'] = new MongoDate( |
41
|
|
|
$data['meta']['request_ts_micro']['sec'], |
42
|
|
|
$data['meta']['request_ts_micro']['usec'] |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
return $this->_collection->insert($data, array('w' => 0)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return profiling ID |
52
|
|
|
* @return MongoId lastProfilingId |
53
|
|
|
*/ |
54
|
|
|
public static function getLastProfilingId() { |
55
|
|
|
if (!self::$lastProfilingId) { |
56
|
|
|
self::$lastProfilingId = new MongoId(); |
57
|
|
|
} |
58
|
|
|
return self::$lastProfilingId; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|