|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Neo4j\Neo4jBundle\Collector; |
| 6 | + |
| 7 | +use Neo4j\Neo4jBundle\EventListener\Neo4jProfileListener; |
| 8 | +use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector; |
| 9 | +use Symfony\Component\HttpFoundation\Request; |
| 10 | +use Symfony\Component\HttpFoundation\Response; |
| 11 | + |
| 12 | +/** |
| 13 | + * @var array{ |
| 14 | + * successful_statements_count: int, |
| 15 | + * failed_statements_count: int, |
| 16 | + * statements: array<array-key, array<string, mixed>> | list<array{ |
| 17 | + * statement: mixed, |
| 18 | + * exception: mixed, |
| 19 | + * alias: string|null |
| 20 | + * }>, |
| 21 | + * } $data |
| 22 | + */ |
| 23 | +final class Neo4jDataCollector extends AbstractDataCollector |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private readonly Neo4jProfileListener $subscriber, |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + public function collect(Request $request, Response $response, ?\Throwable $exception = null): void |
| 31 | + { |
| 32 | + $t = $this; |
| 33 | + $profiledSummaries = $this->subscriber->getProfiledSummaries(); |
| 34 | + $successfulStatements = []; |
| 35 | + foreach ($profiledSummaries as $summary) { |
| 36 | + $statement = ['status' => 'success']; |
| 37 | + foreach ($summary as $key => $value) { |
| 38 | + if (!is_array($value) && !is_object($value)) { |
| 39 | + $statement[$key] = $value; |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + $statement[$key] = $t->recursiveToArray($value); |
| 44 | + } |
| 45 | + $successfulStatements[] = $statement; |
| 46 | + } |
| 47 | + |
| 48 | + $failedStatements = array_map( |
| 49 | + static fn (array $x) => [ |
| 50 | + 'status' => 'failure', |
| 51 | + 'time' => $x['time'], |
| 52 | + 'timestamp' => $x['timestamp'], |
| 53 | + 'result' => [ |
| 54 | + 'statement' => $x['statement']->toArray(), |
| 55 | + ], |
| 56 | + 'exception' => [ |
| 57 | + 'code' => $x['exception']->getErrors()[0]->getCode(), |
| 58 | + 'message' => $x['exception']->getErrors()[0]->getMessage(), |
| 59 | + 'classification' => $x['exception']->getErrors()[0]->getClassification(), |
| 60 | + 'category' => $x['exception']->getErrors()[0]->getCategory(), |
| 61 | + 'title' => $x['exception']->getErrors()[0]->getTitle(), |
| 62 | + ], |
| 63 | + 'alias' => $x['alias'], |
| 64 | + ], |
| 65 | + $this->subscriber->getProfiledFailures() |
| 66 | + ); |
| 67 | + |
| 68 | + $this->data['successful_statements_count'] = count($successfulStatements); |
| 69 | + $this->data['failed_statements_count'] = count($failedStatements); |
| 70 | + $mergedArray = array_merge($successfulStatements, $failedStatements); |
| 71 | + uasort( |
| 72 | + $mergedArray, |
| 73 | + static fn (array $a, array $b) => $a['start_time'] <=> $b['timestamp'] |
| 74 | + ); |
| 75 | + $this->data['statements'] = $mergedArray; |
| 76 | + } |
| 77 | + |
| 78 | + public function reset(): void |
| 79 | + { |
| 80 | + parent::reset(); |
| 81 | + $this->subscriber->reset(); |
| 82 | + } |
| 83 | + |
| 84 | + public function getName(): string |
| 85 | + { |
| 86 | + return 'neo4j'; |
| 87 | + } |
| 88 | + |
| 89 | + /** @api */ |
| 90 | + public function getStatements(): array |
| 91 | + { |
| 92 | + return $this->data['statements']; |
| 93 | + } |
| 94 | + |
| 95 | + public function getSuccessfulStatements(): array |
| 96 | + { |
| 97 | + return array_filter( |
| 98 | + $this->data['statements'], |
| 99 | + static fn (array $x) => 'success' === $x['status'] |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + public function getFailedStatements(): array |
| 104 | + { |
| 105 | + return array_filter( |
| 106 | + $this->data['statements'], |
| 107 | + static fn (array $x) => 'failure' === $x['status'] |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + /** @api */ |
| 112 | + public function getFailedStatementsCount(): array |
| 113 | + { |
| 114 | + return $this->data['failed_statements_count']; |
| 115 | + } |
| 116 | + |
| 117 | + /** @api */ |
| 118 | + public function getSuccessfulStatementsCount(): array |
| 119 | + { |
| 120 | + return $this->data['successful_statements_count']; |
| 121 | + } |
| 122 | + |
| 123 | + public function getQueryCount(): int |
| 124 | + { |
| 125 | + return count($this->data['statements']); |
| 126 | + } |
| 127 | + |
| 128 | + public static function getTemplate(): ?string |
| 129 | + { |
| 130 | + return '@Neo4j/web_profiler.html.twig'; |
| 131 | + } |
| 132 | + |
| 133 | + private function recursiveToArray(mixed $obj): mixed |
| 134 | + { |
| 135 | + if (is_array($obj)) { |
| 136 | + return array_map( |
| 137 | + fn (mixed $x): mixed => $this->recursiveToArray($x), |
| 138 | + $obj |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + if (is_object($obj) && method_exists($obj, 'toArray')) { |
| 143 | + return $obj->toArray(); |
| 144 | + } |
| 145 | + |
| 146 | + return $obj; |
| 147 | + } |
| 148 | +} |
0 commit comments