1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Doctrine MongoDBBundle |
5
|
|
|
* |
6
|
|
|
* The code was originally distributed inside the Symfony framework. |
7
|
|
|
* |
8
|
|
|
* (c) Fabien Potencier <[email protected]> |
9
|
|
|
* (c) Doctrine Project |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace As3\Bundle\ModlrBundle\DataCollector\MongoDb; |
16
|
|
|
|
17
|
|
|
use Doctrine\MongoDB\GridFSFile; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A data collector that formats pretty queries. |
23
|
|
|
* |
24
|
|
|
* @author Kris Wallsmith <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class PrettyDataCollector extends StandardDataCollector |
27
|
|
|
{ |
28
|
|
|
private $batchInsertThreshold; |
29
|
|
|
|
30
|
|
|
public function setBatchInsertThreshold($batchInsertThreshold) |
31
|
|
|
{ |
32
|
|
|
$this->batchInsertThreshold = $batchInsertThreshold; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
36
|
|
|
{ |
37
|
|
|
$this->data['queries'] = array(); |
38
|
|
|
$this->data['nb_queries'] = 0; |
39
|
|
|
|
40
|
|
|
$grouped = array(); |
41
|
|
|
$ordered = array(); |
42
|
|
|
foreach ($this->queries as $query) { |
43
|
|
|
if (!isset($query['query']) || !isset($query['fields'])) { |
44
|
|
|
// no grouping necessary |
45
|
|
|
$ordered[] = array($query); |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$cursor = serialize($query['query']).serialize($query['fields']); |
50
|
|
|
|
51
|
|
|
// append if issued from cursor (currently just "sort") |
52
|
|
|
if (isset($query['sort'])) { |
53
|
|
|
unset($query['query'], $query['fields']); |
54
|
|
|
$grouped[$cursor][count($grouped[$cursor]) - 1][] = $query; |
55
|
|
|
} else { |
56
|
|
|
$grouped[$cursor][] = array($query); |
57
|
|
|
$ordered[] =& $grouped[$cursor][count($grouped[$cursor]) - 1]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$i = 0; |
62
|
|
|
$db = ''; |
63
|
|
|
$query = ''; |
64
|
|
|
foreach ($ordered as $logs) { |
65
|
|
|
foreach ($logs as $log) { |
66
|
|
|
if (isset($log['db']) && $db != $log['db']) { |
67
|
|
|
// for readability |
68
|
|
|
$this->data['queries'][$i++] = 'use '.$log['db'].';'; |
69
|
|
|
$db = $log['db']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (isset($log['collection'])) { |
73
|
|
|
// flush the previous and start a new query |
74
|
|
View Code Duplication |
if (!empty($query)) { |
|
|
|
|
75
|
|
|
if ('.' == $query[0]) { |
76
|
|
|
$query = 'db'.$query; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->data['queries'][$i++] = $query.';'; |
80
|
|
|
++$this->data['nb_queries']; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$query = 'db.'.$log['collection']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// format the method call |
87
|
|
|
if (isset($log['authenticate'])) { |
88
|
|
|
$query .= '.authenticate()'; |
89
|
|
|
} elseif (isset($log['batchInsert'])) { |
90
|
|
|
if (1 === $log['num']) { |
91
|
|
|
$query .= '.insert('.$this->bsonEncode($log['data']).')'; |
92
|
|
|
} elseif (null !== $this->batchInsertThreshold && $this->batchInsertThreshold <= $log['num']) { |
93
|
|
|
$query .= '.batchInsert(**'.$log['num'].' items**)'; |
94
|
|
|
} else { |
95
|
|
|
$query .= '.batchInsert('.$this->bsonEncode($log['data']).')'; |
96
|
|
|
} |
97
|
|
|
} elseif (isset($log['command'])) { |
98
|
|
|
$query .= '.runCommand('.$this->bsonEncode($log['data']).')'; |
99
|
|
|
} elseif (isset($log['count'])) { |
100
|
|
|
$query .= '.count('; |
101
|
|
|
if ($log['query'] || $log['limit'] || $log['skip']) { |
102
|
|
|
$query .= $this->bsonEncode($log['query']); |
103
|
|
View Code Duplication |
if ($log['limit'] || $log['skip']) { |
|
|
|
|
104
|
|
|
$query .= ', '.$this->bsonEncode($log['limit']); |
105
|
|
|
if ($log['skip']) { |
106
|
|
|
$query .= ', '.$this->bsonEncode($log['skip']); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
$query .= ')'; |
111
|
|
|
} elseif (isset($log['skip'])) { |
112
|
|
|
$query .= '.skip('.$log['skipNum'].')'; |
113
|
|
|
} elseif (isset($log['limit'])) { |
114
|
|
|
$query .= '.limit('.$log['limitNum'].')'; |
115
|
|
|
} elseif (isset($log['createCollection'])) { |
116
|
|
|
$query .= '.createCollection()'; |
117
|
|
|
} elseif (isset($log['createDBRef'])) { |
118
|
|
|
$query .= '.createDBRef()'; |
119
|
|
|
} elseif (isset($log['deleteIndex'])) { |
120
|
|
|
$query .= '.dropIndex('.$this->bsonEncode($log['keys']).')'; |
121
|
|
|
} elseif (isset($log['deleteIndexes'])) { |
122
|
|
|
$query .= '.dropIndexes()'; |
123
|
|
|
} elseif (isset($log['drop'])) { |
124
|
|
|
$query .= '.drop()'; |
125
|
|
|
} elseif (isset($log['dropDatabase'])) { |
126
|
|
|
$query .= '.dropDatabase()'; |
127
|
|
View Code Duplication |
} elseif (isset($log['ensureIndex'])) { |
|
|
|
|
128
|
|
|
$query .= '.ensureIndex('.$this->bsonEncode($log['keys']).', '.$this->bsonEncode($log['options']).')'; |
129
|
|
|
} elseif (isset($log['execute'])) { |
130
|
|
|
$query .= '.execute()'; |
131
|
|
|
} elseif (isset($log['find'])) { |
132
|
|
|
$query .= '.find('; |
133
|
|
View Code Duplication |
if ($log['query'] || $log['fields']) { |
|
|
|
|
134
|
|
|
$query .= $this->bsonEncode($log['query']); |
135
|
|
|
if ($log['fields']) { |
136
|
|
|
$query .= ', '.$this->bsonEncode($log['fields']); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
$query .= ')'; |
140
|
|
|
} elseif (isset($log['findOne'])) { |
141
|
|
|
$query .= '.findOne('; |
142
|
|
View Code Duplication |
if ($log['query'] || $log['fields']) { |
|
|
|
|
143
|
|
|
$query .= $this->bsonEncode($log['query']); |
144
|
|
|
if ($log['fields']) { |
145
|
|
|
$query .= ', '.$this->bsonEncode($log['fields']); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
$query .= ')'; |
149
|
|
|
} elseif (isset($log['getDBRef'])) { |
150
|
|
|
$query .= '.getDBRef()'; |
151
|
|
|
} elseif (isset($log['group'])) { |
152
|
|
|
$query .= '.group('.$this->bsonEncode(array( |
153
|
|
|
'key' => $log['keys'], |
154
|
|
|
'initial' => $log['initial'], |
155
|
|
|
'reduce' => $log['reduce'], |
156
|
|
|
)).')'; |
157
|
|
|
} elseif (isset($log['insert'])) { |
158
|
|
|
$query .= '.insert('.$this->bsonEncode($log['document']).')'; |
159
|
|
|
} elseif (isset($log['remove'])) { |
160
|
|
|
$query .= '.remove('.$this->bsonEncode($log['query']).')'; |
161
|
|
|
} elseif (isset($log['save'])) { |
162
|
|
|
$query .= '.save('.$this->bsonEncode($log['document']).')'; |
163
|
|
|
} elseif (isset($log['sort'])) { |
164
|
|
|
$query .= '.sort('.$this->bsonEncode($log['sortFields']).')'; |
165
|
|
View Code Duplication |
} elseif (isset($log['update'])) { |
|
|
|
|
166
|
|
|
// todo: include $log['options'] |
|
|
|
|
167
|
|
|
$query .= '.update('.$this->bsonEncode($log['query']).', '.$this->bsonEncode($log['newObj']).')'; |
168
|
|
|
} elseif (isset($log['validate'])) { |
169
|
|
|
$query .= '.validate()'; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
View Code Duplication |
if (!empty($query)) { |
|
|
|
|
175
|
|
|
if ('.' == $query[0]) { |
176
|
|
|
$query = 'db'.$query; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$this->data['queries'][$i++] = $query.';'; |
180
|
|
|
++$this->data['nb_queries']; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @todo Move this to a collaborator |
186
|
|
|
*/ |
187
|
|
|
private function bsonEncode($query, $array = true) |
188
|
|
|
{ |
189
|
|
|
$parts = array(); |
190
|
|
|
|
191
|
|
|
foreach ($query as $key => $value) { |
192
|
|
|
if (!is_numeric($key)) { |
193
|
|
|
$array = false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (null === $value) { |
197
|
|
|
$formatted = 'null'; |
198
|
|
|
} elseif (is_bool($value)) { |
199
|
|
|
$formatted = $value ? 'true' : 'false'; |
200
|
|
|
} elseif (is_int($value) || is_float($value)) { |
201
|
|
|
$formatted = $value; |
202
|
|
|
} elseif (is_scalar($value)) { |
203
|
|
|
$formatted = '"'.$value.'"'; |
204
|
|
|
} elseif (is_array($value)) { |
205
|
|
|
$formatted = $this->bsonEncode($value); |
206
|
|
|
} elseif ($value instanceof \MongoId) { |
207
|
|
|
$formatted = 'ObjectId("'.$value.'")'; |
208
|
|
|
} elseif ($value instanceof \MongoDate) { |
209
|
|
|
$formatted = 'new ISODate("'.date('c', $value->sec).'")'; |
210
|
|
|
} elseif ($value instanceof \DateTime) { |
211
|
|
|
$formatted = 'new ISODate("'.date('c', $value->getTimestamp()).'")'; |
212
|
|
|
} elseif ($value instanceof \MongoRegex) { |
213
|
|
|
$formatted = 'new RegExp("'.$value->regex.'", "'.$value->flags.'")'; |
214
|
|
|
} elseif ($value instanceof \MongoMinKey) { |
215
|
|
|
$formatted = 'new MinKey()'; |
216
|
|
|
} elseif ($value instanceof \MongoMaxKey) { |
217
|
|
|
$formatted = 'new MaxKey()'; |
218
|
|
|
} elseif ($value instanceof \MongoBinData) { |
219
|
|
|
$formatted = 'new BinData('.$value->type.', "'.base64_encode($value->bin).'")'; |
220
|
|
|
} elseif ($value instanceof \MongoGridFSFile || $value instanceof GridFSFile) { |
|
|
|
|
221
|
|
|
$formatted = 'new MongoGridFSFile("'.$value->getFilename().'")'; |
222
|
|
|
} elseif ($value instanceof \stdClass) { |
223
|
|
|
$formatted = $this->bsonEncode((array) $value); |
224
|
|
|
} else { |
225
|
|
|
$formatted = (string) $value; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$parts['"'.$key.'"'] = $formatted; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if (0 == count($parts)) { |
232
|
|
|
return $array ? '[ ]' : '{ }'; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if ($array) { |
236
|
|
|
return '[ '.implode(', ', $parts).' ]'; |
237
|
|
|
} else { |
238
|
|
|
$mapper = function($key, $value) |
239
|
|
|
{ |
240
|
|
|
return $key.': '.$value; |
241
|
|
|
}; |
242
|
|
|
|
243
|
|
|
return '{ '.implode(', ', array_map($mapper, array_keys($parts), array_values($parts))).' }'; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.