1<?php 2use MongoDB\Driver\Monitoring\CommandFailedEvent; 3use MongoDB\Driver\Monitoring\CommandStartedEvent; 4use MongoDB\Driver\Monitoring\CommandSucceededEvent; 5use MongoDB\Driver\Monitoring\CommandSubscriber; 6/** 7 * Observes command documents using the driver's monitoring API. 8 */ 9class CommandObserver implements CommandSubscriber 10{ 11 private $commands = []; 12 public function observe(callable $execution, callable $commandCallback) 13 { 14 $this->commands = []; 15 \MongoDB\Driver\Monitoring\addSubscriber($this); 16 try { 17 call_user_func($execution); 18 } finally { 19 \MongoDB\Driver\Monitoring\removeSubscriber($this); 20 foreach ($this->commands as $command) { 21 call_user_func($commandCallback, $command); 22 } 23 } 24 } 25 public function commandStarted(CommandStartedEvent $event) 26 { 27 $this->commands[] = $event->getCommand(); 28 } 29 public function commandSucceeded(CommandSucceededEvent $event) 30 { 31 } 32 public function commandFailed(CommandFailedEvent $event) 33 { 34 } 35} 36?> 37