File tree Expand file tree Collapse file tree 2 files changed +14
-9
lines changed Expand file tree Collapse file tree 2 files changed +14
-9
lines changed Original file line number Diff line number Diff line change 7
7
8
8
use MongoDB \Client ;
9
9
10
+ use function assert ;
10
11
use function fprintf ;
11
12
use function getenv ;
12
- use function hrtime ;
13
+ use function is_object ;
13
14
use function MongoDB \BSON \fromPHP ;
14
15
use function MongoDB \BSON \toRelaxedExtendedJSON ;
15
16
use function printf ;
17
+ use function time ;
16
18
17
19
use const STDERR ;
18
20
@@ -21,7 +23,7 @@ function toJSON(object $document): string
21
23
return toRelaxedExtendedJSON (fromPHP ($ document ));
22
24
}
23
25
24
- $ client = new Client (getenv ('MONGODB_URI ' ) ?? null );
26
+ $ client = new Client (getenv ('MONGODB_URI ' ) ?: ' mongodb://127.0.0.1/ ' );
25
27
26
28
$ collection = $ client ->test ->coll ;
27
29
$ collection ->drop ();
@@ -38,16 +40,18 @@ function toJSON(object $document): string
38
40
39
41
$ changeStream ->rewind ();
40
42
41
- $ startTime = hrtime ( true );
43
+ $ startTime = time ( );
42
44
43
45
while (true ) {
44
46
if ($ changeStream ->valid ()) {
45
- printf ("%s \n" , toJSON ($ changeStream ->current ()));
47
+ $ event = $ changeStream ->current ();
48
+ assert (is_object ($ event ));
49
+ printf ("%s \n" , toJSON ($ event ));
46
50
}
47
51
48
52
$ changeStream ->next ();
49
53
50
- if (hrtime ( true ) - $ startTime > 5e+9 ) {
54
+ if (time ( ) - $ startTime > 5e+9 ) {
51
55
fprintf (STDERR , "Aborting after 5 seconds... \n" );
52
56
break ;
53
57
}
Original file line number Diff line number Diff line change 1
1
<?php
2
2
declare (strict_types=1 );
3
3
4
- // phpcs:disable Squiz.Classes.ClassFileName.NoMatch
5
-
6
4
namespace MongoDB \Examples ;
7
5
8
6
require '../vendor/autoload.php ' ;
13
11
use MongoDB \Driver \Monitoring \CommandSubscriber ;
14
12
use MongoDB \Driver \Monitoring \CommandSucceededEvent ;
15
13
14
+ use function assert ;
16
15
use function fprintf ;
17
16
use function get_class ;
18
17
use function getenv ;
18
+ use function is_object ;
19
19
use function MongoDB \BSON \fromPHP ;
20
20
use function MongoDB \BSON \toRelaxedExtendedJSON ;
21
21
use function printf ;
@@ -27,6 +27,7 @@ function toJSON(object $document): string
27
27
return toRelaxedExtendedJSON (fromPHP ($ document ));
28
28
}
29
29
30
+ // phpcs:disable Squiz.Classes.ClassFileName.NoMatch
30
31
class CommandLogger implements CommandSubscriber
31
32
{
32
33
public function commandStarted (CommandStartedEvent $ event ): void
@@ -56,9 +57,8 @@ public function commandFailed(CommandFailedEvent $event): void
56
57
fprintf (STDERR , "\n" );
57
58
}
58
59
}
59
- // phpcs:enable
60
60
61
- $ client = new Client (getenv ('MONGODB_URI ' ) ?? null );
61
+ $ client = new Client (getenv ('MONGODB_URI ' ) ?: ' mongodb://127.0.0.1/ ' );
62
62
63
63
$ client ->getManager ()->addSubscriber (new CommandLogger ());
64
64
@@ -79,5 +79,6 @@ public function commandFailed(CommandFailedEvent $event): void
79
79
$ cursor = $ collection ->find ([], ['batchSize ' => 2 ]);
80
80
81
81
foreach ($ cursor as $ document ) {
82
+ assert (is_object ($ document ));
82
83
printf ("%s \n" , toJSON ($ document ));
83
84
}
You can’t perform that action at this time.
0 commit comments