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