1 | <?php declare(strict_types = 1); |
||
15 | final class MongoDbDataCollector extends DataCollector |
||
16 | { |
||
17 | const QUERY_KEYWORD = 'queries'; |
||
18 | const CONNECTION_KEYWORD = 'connections'; |
||
19 | const TIME_KEYWORD = 'totalTime'; |
||
20 | |||
21 | /** @var DataCollectorLoggerInterface */ |
||
22 | private $logger; |
||
23 | /** @var array */ |
||
24 | protected $data; |
||
25 | |||
26 | 1 | public function __construct() |
|
27 | { |
||
28 | 1 | $this->data = [ |
|
29 | 1 | self::QUERY_KEYWORD => [], |
|
30 | 1 | self::TIME_KEYWORD => 0.0, |
|
31 | 1 | self::CONNECTION_KEYWORD => [], |
|
32 | ]; |
||
33 | 1 | } |
|
34 | |||
35 | /** |
||
36 | * @param DataCollectorLoggerInterface $logger |
||
37 | */ |
||
38 | 1 | public function setLogger(DataCollectorLoggerInterface $logger) |
|
39 | { |
||
40 | 1 | $this->logger = $logger; |
|
41 | 1 | } |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 1 | public function collect(Request $request, Response $response, \Exception $exception = null) |
|
47 | { |
||
48 | 1 | while ($this->logger->hasLoggedEvents()) { |
|
49 | /** @var Query $event */ |
||
50 | 1 | $event = $this->logger->getLoggedEvent(); |
|
51 | |||
52 | // with extension version under 1.2.4 some Mongo objects can't be automatically serialized |
||
53 | 1 | if (-1 === version_compare(phpversion('mongodb'), '1.2.4')) { |
|
54 | 1 | MongoQuerySerializer::serialize($event); |
|
55 | } |
||
56 | |||
57 | 1 | $this->data[self::QUERY_KEYWORD][] = $event; |
|
58 | 1 | $this->data[self::TIME_KEYWORD] += $event->getExecutionTime(); |
|
59 | } |
||
60 | |||
61 | 1 | $this->data[self::CONNECTION_KEYWORD] = $this->logger->getConnections(); |
|
62 | 1 | } |
|
63 | |||
64 | /** |
||
65 | * @return int |
||
66 | */ |
||
67 | 1 | public function getQueryCount(): int |
|
71 | |||
72 | /** |
||
73 | * @return array |
||
74 | */ |
||
75 | 1 | public function getQueries(): array |
|
79 | |||
80 | /** |
||
81 | * @return float |
||
82 | */ |
||
83 | 1 | public function getTime(): float |
|
87 | |||
88 | /** |
||
89 | * @return int |
||
90 | */ |
||
91 | 1 | public function getConnectionsCount(): int |
|
95 | /** |
||
96 | * @return array|string[] |
||
97 | */ |
||
98 | 1 | public function getConnections(): array |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 1 | public function getName() |
|
110 | } |
||
111 |