-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathGeocoderDataCollector.php
111 lines (94 loc) · 2.88 KB
/
GeocoderDataCollector.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
/*
* This file is part of the BazingaGeocoderBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Bazinga\GeocoderBundle\DataCollector;
use Bazinga\GeocoderBundle\Plugin\ProfilingPlugin;
use Geocoder\Query\Query;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
/**
* @author Michal Dabrowski <[email protected]>
*/
class GeocoderDataCollector extends DataCollector
{
/**
* @var ProfilingPlugin[]
*/
private array $instances = [];
public function __construct()
{
$this->data['queries'] = [];
$this->data['providers'] = [];
}
public function reset(): void
{
$this->instances = [];
$this->data['queries'] = [];
$this->data['providers'] = [];
}
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
{
if (!empty($this->data['queries'])) {
// To avoid collection more that once.
return;
}
$instances = $this->instances;
foreach ($instances as $instance) {
foreach ($instance->getQueries() as $query) {
$query['query'] = $this->cloneVar($query['query']);
$query['result'] = $this->cloneVar($query['result']);
$this->data['queries'][] = $query;
}
}
}
/**
* Returns an array of collected requests.
*
* @return list<array{query: Query, queryString: string, duration: float, providerName: string, result: mixed, resultCount: int}>
*/
public function getQueries(): array
{
return $this->data['queries'];
}
/**
* Returns the execution time of all collected requests in seconds.
*/
public function getTotalDuration(): float
{
$time = 0;
foreach ($this->data['queries'] as $command) {
$time += $command['duration'];
}
return $time;
}
/**
* @return string[]
*/
public function getProviders(): array
{
return $this->data['providers'];
}
/**
* @return list<array{query: Query, queryString: string, duration: float, providerName: string, result: mixed, resultCount: int}>
*/
public function getProviderQueries(string $provider): array
{
return array_filter($this->data['queries'], static fn (array $data): bool => $data['providerName'] === $provider);
}
public function addInstance(ProfilingPlugin $instance): void
{
$this->instances[] = $instance;
$this->data['providers'][] = $instance->getName();
}
public function getName(): string
{
return 'geocoder';
}
}