forked from hpaul/mongor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoprofiler.php
executable file
·72 lines (55 loc) · 1.66 KB
/
mongoprofiler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php namespace Mongor;
use Laravel\View;
use Laravel\File;
use Laravel\Event;
use Laravel\Config;
use Laravel\Request;
use Laravel\Database;
class MongoProfiler extends \Laravel\Profiling\Profiler
{
/**
* Add a performed MongoDB query to the Profiler.
*
* @param string $sql
* @param array $bindings
* @param float $time
*
* @return void
*/
public static function mongo_query($db, $sql, $bindings, $time)
{
$sql = 'Command:' . $sql .', params [';
$sql = $sql . implode(', ', MongoProfiler::format_parameters($bindings));
$sql .= '] (DB ' . $db . ')';
static::$data['queries'][] = array($sql, $time);
}
protected static function format_parameters($bindings)
{
$statements = array();
foreach ($bindings as $key => $binding) {
if(isset($binding) && !empty($binding)) {
$statement = $key . ':';
if (is_array($binding)) {
$statements = array_merge($statements, MongoProfiler::format_parameters($binding));
}
else {
$statement .= htmlspecialchars($binding);
}
$statements[] = $statement;
}
}
return $statements;
}
/**
* Attach the Profiler's event listeners.
*
* @return void
*/
public static function attach()
{
\Laravel\Profiling\Profiler::attach();
Event::listen('laravel.mongoquery', function ($db, $sql, $bindings, $time) {
MongoProfiler::mongo_query($db, $sql, $bindings, $time);
});
}
}