1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Pomm's SymfonyBidge package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PommProject\SymfonyBridge; |
12
|
|
|
|
13
|
|
|
use PommProject\Foundation\Exception\SqlException; |
14
|
|
|
use PommProject\Foundation\Listener\Listener; |
15
|
|
|
use PommProject\Foundation\Session\Session; |
16
|
|
|
|
17
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Data collector for the database profiler. |
24
|
|
|
* |
25
|
|
|
* @package PommSymfonyBridge |
26
|
|
|
* @copyright 2014 Grégoire HUBERT |
27
|
|
|
* @author Jérôme MACIAS |
28
|
|
|
* @author Grégoire HUBERT |
29
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
30
|
|
|
* @see DataCollector |
31
|
|
|
*/ |
32
|
|
|
class DatabaseDataCollector extends DataCollector |
33
|
|
|
{ |
34
|
|
|
/** @var Stopwatch */ |
35
|
|
|
private $stopwatch; |
36
|
|
|
|
37
|
|
|
public function __construct($unused = null, Stopwatch $stopwatch = null) |
38
|
|
|
{ |
39
|
|
|
if ($unused !== null) { |
40
|
|
|
trigger_error("The parameter Pomm has been deleted for to delete the high dependency.", E_USER_DEPRECATED); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->stopwatch = $stopwatch; |
44
|
|
|
$this->data = [ |
45
|
|
|
'time' => 0, |
46
|
|
|
'queries' => [], |
47
|
|
|
'exception' => null, |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $name |
53
|
|
|
* @param array $data |
54
|
|
|
* @param $session |
55
|
|
|
* |
56
|
|
|
* @return null |
57
|
|
|
*/ |
58
|
|
|
public function execute($name, $data, Session $session) |
59
|
|
|
{ |
60
|
|
|
switch ($name) { |
61
|
|
|
case 'query:post': |
62
|
|
|
$this->data['time'] += (float) $data['time_ms']; |
63
|
|
|
$data += array_pop($this->data['queries']); |
64
|
|
|
/* fall-through */ |
65
|
|
|
case 'query:pre': |
66
|
|
|
$this->data['queries'][] = $data; |
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->watch($name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function watch($name) |
74
|
|
|
{ |
75
|
|
|
if ($this->stopwatch !== null) { |
76
|
|
|
switch ($name) { |
77
|
|
|
case 'query:pre': |
78
|
|
|
$this->stopwatch->start('query.pomm', 'pomm'); |
79
|
|
|
break; |
80
|
|
|
case 'query:post': |
81
|
|
|
$this->stopwatch->stop('query.pomm'); |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function collect(Request $request, Response $response, \Throwable $exception = null) |
91
|
|
|
{ |
92
|
|
|
if ($exception instanceof SqlException) { |
93
|
|
|
$this->data['exception'] = $exception->getMessage(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return the list of queries sent. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function getQueries() |
103
|
|
|
{ |
104
|
|
|
return $this->data['queries']; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return the number of queries sent. |
109
|
|
|
* |
110
|
|
|
* @return integer |
111
|
|
|
*/ |
112
|
|
|
public function getQuerycount() |
113
|
|
|
{ |
114
|
|
|
return count($this->data['queries']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return queries total time. |
119
|
|
|
* |
120
|
|
|
* @return float |
121
|
|
|
*/ |
122
|
|
|
public function getTime() |
123
|
|
|
{ |
124
|
|
|
return $this->data['time']; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return sql exception. |
129
|
|
|
* |
130
|
|
|
* @return \PommProject\Foundation\Exception\SqlException|null |
131
|
|
|
*/ |
132
|
|
|
public function getException() |
133
|
|
|
{ |
134
|
|
|
return $this->data['exception']; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Return profiler identifier. |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function getName() |
143
|
|
|
{ |
144
|
|
|
return 'pomm'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function reset() |
151
|
|
|
{ |
152
|
|
|
$this->stopwatch->reset(); |
153
|
|
|
$this->data = array(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|