1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the BazingaGeocoderBundle package. |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @license MIT License |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Bazinga\GeocoderBundle\DataCollector; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Michal Dabrowski <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class GeocoderDataCollector extends DataCollector |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ProfilingProvider[] |
24
|
|
|
*/ |
25
|
|
|
private $instances = []; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->data['queries'] = []; |
30
|
|
|
$this->data['providers'] = []; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
37
|
|
|
{ |
38
|
|
|
foreach ($this->instances as $instance) { |
39
|
|
|
foreach ($instance->getQueries() as $query) { |
40
|
|
|
$query['query'] = $this->cloneVar($query['query']); |
41
|
|
|
$query['result'] = $this->cloneVar($query['result']); |
42
|
|
|
$this->data['queries'][] = $query; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns an array of collected requests. |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getQueries(): array |
53
|
|
|
{ |
54
|
|
|
return $this->data['queries']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the execution time of all collected requests in seconds. |
59
|
|
|
* |
60
|
|
|
* @return float |
61
|
|
|
*/ |
62
|
|
|
public function getTotalDuration() |
63
|
|
|
{ |
64
|
|
|
$time = 0; |
65
|
|
|
foreach ($this->data['queries'] as $command) { |
66
|
|
|
$time += $command['duration']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $time; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function getProviders(): array |
76
|
|
|
{ |
77
|
|
|
return $this->data['providers']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $provider |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function getProviderQueries($provider): array |
86
|
|
|
{ |
87
|
|
|
return array_filter($this->data['queries'], function ($data) use ($provider) { |
88
|
|
|
return $data['providerName'] === $provider; |
89
|
|
|
}); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ProfilingProvider $instance |
94
|
|
|
*/ |
95
|
|
|
public function addInstance(ProfilingProvider $instance) |
96
|
|
|
{ |
97
|
|
|
$this->instances[] = $instance; |
98
|
|
|
$this->data['providers'][] = $instance->getName(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getName() |
105
|
|
|
{ |
106
|
|
|
return 'geocoder'; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|