1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MongoDbHandler.php |
5
|
|
|
* |
6
|
|
|
* @author Dennis de Greef <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Link0\Profiler\PersistenceHandler; |
10
|
|
|
|
11
|
|
|
use Link0\Profiler\PersistenceHandler; |
12
|
|
|
use Link0\Profiler\PersistenceHandler\MongoDbHandler\MongoClientInterface; |
13
|
|
|
use Link0\Profiler\PersistenceHandlerInterface; |
14
|
|
|
use Link0\Profiler\ProfileInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* MongoDb implementation for Persistence |
18
|
|
|
* |
19
|
|
|
* @package Link0\Profiler\PersistenceHandler |
20
|
|
|
*/ |
21
|
|
|
final class MongoDbHandler extends PersistenceHandler implements PersistenceHandlerInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var MongoClientInterface |
25
|
|
|
*/ |
26
|
|
|
private $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $namespace; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param MongoClientInterface $client |
35
|
|
|
* @param string $databaseName |
36
|
|
|
* @param string $collection |
37
|
|
|
*/ |
38
|
|
|
public function __construct(MongoClientInterface $client, $databaseName = 'xhprof', $collection = 'results') |
39
|
|
|
{ |
40
|
|
|
parent::__construct(); |
41
|
|
|
|
42
|
|
|
$this->client = $client; |
43
|
|
|
$this->namespace = $databaseName . '.' . $collection; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns a list of Identifier strings |
48
|
|
|
* Unfortunately the list() method is reserved |
49
|
|
|
* |
50
|
|
|
* @return string[] |
51
|
|
|
*/ |
52
|
|
|
public function getList() |
53
|
|
|
{ |
54
|
|
|
// Awaiting https://github.com/link0/profiler/issues/60 for refactoring |
55
|
|
|
// The getList interface (or renamed method) should return an Iterator of some kind |
56
|
|
|
return $this->client->executeQuery($this->namespace, array()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $identifier |
61
|
|
|
* |
62
|
|
|
* @return ProfileInterface|null $profile |
63
|
|
|
*/ |
64
|
|
|
public function retrieve($identifier) |
65
|
|
|
{ |
66
|
|
|
$profiles = $this->client->executeQuery( |
67
|
|
|
$this->namespace, |
68
|
|
|
array( |
69
|
|
|
'identifier' => $identifier, |
70
|
|
|
), |
71
|
|
|
array( |
72
|
|
|
'limit' => 1, |
73
|
|
|
)); |
74
|
|
|
|
75
|
|
|
$profileData = reset($profiles); |
76
|
|
|
|
77
|
|
|
if ($profileData === false) { |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($profileData !== null) { |
82
|
|
|
return $this->createProfileFromProfileData($profileData['profile']); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param ProfileInterface $profile |
90
|
|
|
* |
91
|
|
|
* @return PersistenceHandlerInterface $this |
92
|
|
|
*/ |
93
|
|
|
public function persist(ProfileInterface $profile) |
94
|
|
|
{ |
95
|
|
|
// This is messed up, but this is finally compatible with XHGui, which is more important to me now. |
96
|
|
|
// Find a way to abstract this nicely! BUT FIRST! Release time! YEAH! (I am _SO_ gonna regret this...) |
97
|
|
|
$mongoRequestDateTime = $this->getMongoRequestDateTime($profile); |
98
|
|
|
$uri = $this->getMongoUri($profile); |
99
|
|
|
|
100
|
|
|
$mongoDocument = array( |
101
|
|
|
'identifier' => $profile->getIdentifier(), |
102
|
|
|
'profile' => $profile->getProfileData(), |
103
|
|
|
'meta' => array( |
104
|
|
|
'url' => $uri, |
105
|
|
|
'SERVER' => $profile->getServerData(), |
106
|
|
|
'get' => array(), |
107
|
|
|
'env' => array(), |
108
|
|
|
'simple_url' => $uri, |
109
|
|
|
'request_ts' => $mongoRequestDateTime, |
110
|
|
|
'request_ts_micro' => $this->getMongoRequestTimestamp($profile), |
111
|
|
|
'request_date' => $mongoRequestDateTime->toDateTime()->format('Y-m-d'), |
112
|
|
|
) |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$this->client->insert($this->namespace, $mongoDocument); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param ProfileInterface $profile |
122
|
|
|
* @return \MongoDB\BSON\UTCDateTime |
123
|
|
|
*/ |
124
|
|
|
private function getMongoRequestDateTime(ProfileInterface $profile) |
125
|
|
|
{ |
126
|
|
|
$serverData = $profile->getServerData(); |
127
|
|
|
|
128
|
|
|
$requestTimeStamp = isset($serverData['REQUEST_TIME']) ? $serverData['REQUEST_TIME'] : time(); |
129
|
|
|
$requestTime = new \DateTime(); |
130
|
|
|
$requestTime->setTimestamp($requestTimeStamp); |
131
|
|
|
|
132
|
|
|
// NOTE: Even though my local documentation for this class says you cannot pass a DateTimeInterface, |
133
|
|
|
// this is actually possible according to php.net. Actually testing it verifies this. |
134
|
|
|
return new \MongoDB\BSON\UTCDateTime($requestTime); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param ProfileInterface $profile |
139
|
|
|
* @return \MongoDB\BSON\Timestamp |
140
|
|
|
*/ |
141
|
|
|
private function getMongoRequestTimestamp(ProfileInterface $profile) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$requestTimeFloat = isset($serverData['REQUEST_TIME_FLOAT']) ? $serverData['REQUEST_TIME_FLOAT'] : microtime(true); |
|
|
|
|
144
|
|
|
$timeParts = explode('.', $requestTimeFloat); |
145
|
|
|
if (!isset($timeParts[1])) { |
146
|
|
|
$timeParts[1] = 0; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return new \MongoDB\BSON\Timestamp($timeParts[1], $timeParts[0]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param ProfileInterface $profile |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
private function getMongoUri(ProfileInterface $profile) |
157
|
|
|
{ |
158
|
|
|
$serverData = $profile->getServerData(); |
159
|
|
|
$scriptName = isset($serverData['SCRIPT_NAME']) ? $serverData['SCRIPT_NAME'] : '__unknown__'; |
160
|
|
|
$uri = isset($serverData['REQUEST_URI']) ? $serverData['REQUEST_URI'] : $scriptName; |
161
|
|
|
|
162
|
|
|
return $uri; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.