vendor/doctrine/mongodb-odm-bundle/DataCollector/CommandDataCollector.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Bundle\MongoDBBundle\DataCollector;
  4. use Doctrine\ODM\MongoDB\APM\Command;
  5. use Doctrine\ODM\MongoDB\APM\CommandLogger;
  6. use stdClass;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  10. use Throwable;
  11. use function array_map;
  12. use function array_reduce;
  13. use function count;
  14. use function json_decode;
  15. use function MongoDB\BSON\fromPHP;
  16. use function MongoDB\BSON\toCanonicalExtendedJSON;
  17. class CommandDataCollector extends DataCollector
  18. {
  19.     private CommandLogger $commandLogger;
  20.     public function __construct(CommandLogger $commandLogger)
  21.     {
  22.         $this->commandLogger $commandLogger;
  23.     }
  24.     public function collect(Request $requestResponse $response, ?Throwable $exception null): void
  25.     {
  26.         $this->data = [
  27.             'num_commands' => count($this->commandLogger),
  28.             'commands' => array_map(
  29.                 static function (Command $command): array {
  30.                     $dbProperty '$db';
  31.                     return [
  32.                         'database' => $command->getCommand()->$dbProperty ?? '',
  33.                         'command' => json_decode(toCanonicalExtendedJSON(fromPHP($command->getCommand()))),
  34.                         'durationMicros' => $command->getDurationMicros(),
  35.                     ];
  36.                 },
  37.                 $this->commandLogger->getAll(),
  38.             ),
  39.             'time' => array_reduce(
  40.                 $this->commandLogger->getAll(),
  41.                 static fn (int $totalCommand $command): int => $total $command->getDurationMicros(),
  42.                 0,
  43.             ),
  44.         ];
  45.     }
  46.     public function reset(): void
  47.     {
  48.         $this->commandLogger->clear();
  49.         $this->data = [
  50.             'num_commands' => 0,
  51.             'commands' => [],
  52.         ];
  53.     }
  54.     public function getCommandCount(): int
  55.     {
  56.         return $this->data['num_commands'];
  57.     }
  58.     public function getTime(): int
  59.     {
  60.         return $this->data['time'];
  61.     }
  62.     /** @return array<array{database: string, command: stdClass, durationMicros: int}> */
  63.     public function getCommands(): array
  64.     {
  65.         return $this->data['commands'];
  66.     }
  67.     public function getName(): string
  68.     {
  69.         return 'mongodb';
  70.     }
  71. }