22
33require __DIR__ . '/vendor/autoload.php ' ;
44
5- // start-mysubscriber
6- class MySubscriber implements MongoDB \Driver \Monitoring \SDAMSubscriber
5+ // start-command-subscriber
6+ class MyCommandSubscriber implements MongoDB \Driver \Monitoring \CommandSubscriber
7+ {
8+ private $ stream ;
9+
10+ public function __construct ($ stream )
11+ {
12+ $ this ->stream = $ stream ;
13+ }
14+
15+ public function commandStarted (MongoDB \Driver \Monitoring \CommandStartedEvent $ event ): void
16+ {
17+ fwrite ($ this ->stream , sprintf (
18+ 'Started command #%d "%s": %s%s ' ,
19+ $ event ->getRequestId (),
20+ $ event ->getCommandName (),
21+ MongoDB \BSON \Document::fromPHP ($ event ->getCommand ())->toCanonicalExtendedJSON (),
22+ PHP_EOL ,
23+ ));
24+ }
25+
26+ public function commandSucceeded (MongoDB \Driver \Monitoring \CommandSucceededEvent $ event ): void {}
27+ public function commandFailed (MongoDB \Driver \Monitoring \CommandFailedEvent $ event ): void {}
28+ }
29+ // end-command-subscriber
30+
31+ // start-sdam-subscriber
32+ class MySDAMSubscriber implements MongoDB \Driver \Monitoring \SDAMSubscriber
733{
834 private $ stream ;
935
@@ -18,6 +44,7 @@ public function serverOpening(MongoDB\Driver\Monitoring\ServerOpeningEvent $even
1844 'Server opening on %s:%s\n ' ,
1945 $ event ->getHost (),
2046 $ event ->getPort (),
47+ PHP_EOL ,
2148 );
2249 }
2350
@@ -30,20 +57,23 @@ public function topologyChanged(MongoDB\Driver\Monitoring\TopologyChangedEvent $
3057 public function topologyClosed (MongoDB \Driver \Monitoring \TopologyClosedEvent $ event ): void {}
3158 public function topologyOpening (MongoDB \Driver \Monitoring \TopologyOpeningEvent $ event ): void {}
3259}
33- // end-mysubscriber
60+ // end-sdam-subscriber
3461
3562$ uri = getenv ('MONGODB_URI ' ) ?: throw new RuntimeException ('Set the MONGODB_URI variable to your connection URI ' );
3663$ client = new MongoDB \Client ($ uri );
3764
3865$ collection = $ client ->db ->my_coll ;
3966
40- // start-add-sub
41- $ subscriber = new MySubscriber (STDERR );
42- $ client ->addSubscriber ($ subscriber );
43- // end-add-sub
67+ // start-add-subs
68+ $ commandSub = new MyCommandSubscriber (STDERR );
69+ $ sdamSub = new MySDAMSubscriber (STDERR );
70+
71+ $ client ->addSubscriber ($ commandSub );
72+ $ client ->addSubscriber ($ sdamSub );
73+ // end-add-subs
4474
4575$ collection ->insertOne (['x ' => 100 ]);
4676
4777// start-remove-sub
48- $ client ->removeSubscriber ($ subscriber );
78+ $ client ->removeSubscriber ($ commandSub );
4979// end-remove-sub
0 commit comments