2
2
3
3
require __DIR__ . '/vendor/autoload.php ' ;
4
4
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
7
33
{
8
34
private $ stream ;
9
35
@@ -18,6 +44,7 @@ public function serverOpening(MongoDB\Driver\Monitoring\ServerOpeningEvent $even
18
44
'Server opening on %s:%s\n ' ,
19
45
$ event ->getHost (),
20
46
$ event ->getPort (),
47
+ PHP_EOL ,
21
48
);
22
49
}
23
50
@@ -30,20 +57,23 @@ public function topologyChanged(MongoDB\Driver\Monitoring\TopologyChangedEvent $
30
57
public function topologyClosed (MongoDB \Driver \Monitoring \TopologyClosedEvent $ event ): void {}
31
58
public function topologyOpening (MongoDB \Driver \Monitoring \TopologyOpeningEvent $ event ): void {}
32
59
}
33
- // end-mysubscriber
60
+ // end-sdam-subscriber
34
61
35
62
$ uri = getenv ('MONGODB_URI ' ) ?: throw new RuntimeException ('Set the MONGODB_URI variable to your connection URI ' );
36
63
$ client = new MongoDB \Client ($ uri );
37
64
38
65
$ collection = $ client ->db ->my_coll ;
39
66
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
44
74
45
75
$ collection ->insertOne (['x ' => 100 ]);
46
76
47
77
// start-remove-sub
48
- $ client ->removeSubscriber ($ subscriber );
78
+ $ client ->removeSubscriber ($ commandSub );
49
79
// end-remove-sub
0 commit comments