@@ -13,6 +13,8 @@ single [`run()`](#run) call that is controlled by the user.
13
13
14
14
* [ Quickstart example] ( #quickstart-example )
15
15
* [ Usage] ( #usage )
16
+ * [ Loop] ( #loop )
17
+ * [ get()] ( #get )
16
18
* [ Factory] ( #factory )
17
19
* [ create()] ( #create )
18
20
* [ Loop implementations] ( #loop-implementations )
@@ -45,32 +47,32 @@ single [`run()`](#run) call that is controlled by the user.
45
47
Here is an async HTTP server built with just the event loop.
46
48
47
49
``` php
48
- $loop = React\EventLoop\Factory::create() ;
50
+ use React\EventLoop\Loop ;
49
51
50
52
$server = stream_socket_server('tcp://127.0.0.1:8080');
51
53
stream_set_blocking($server, false);
52
54
53
- $loop ->addReadStream($server, function ($server) use ($loop ) {
55
+ Loop::get() ->addReadStream($server, function ($server) {
54
56
$conn = stream_socket_accept($server);
55
57
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
56
- $loop ->addWriteStream($conn, function ($conn) use (& $data, $loop ) {
58
+ Loop::get() ->addWriteStream($conn, function ($conn) use (& $data) {
57
59
$written = fwrite($conn, $data);
58
60
if ($written === strlen($data)) {
59
61
fclose($conn);
60
- $loop ->removeWriteStream($conn);
62
+ Loop::get() ->removeWriteStream($conn);
61
63
} else {
62
64
$data = substr($data, $written);
63
65
}
64
66
});
65
67
});
66
68
67
- $loop ->addPeriodicTimer(5, function () {
69
+ Loop::get() ->addPeriodicTimer(5, function () {
68
70
$memory = memory_get_usage() / 1024;
69
71
$formatted = number_format($memory, 3).'K';
70
72
echo "Current memory usage: {$formatted}\n";
71
73
});
72
74
73
- $loop ->run();
75
+ Loop::get() ->run();
74
76
```
75
77
76
78
See also the [ examples] ( examples ) .
@@ -110,6 +112,28 @@ $loop->run();
110
112
purposes.
111
113
3 . The loop is run with a single [ ` $loop->run() ` ] ( #run ) call at the end of the program.
112
114
115
+ ### Loop
116
+
117
+ The ` Loop ` class exists as a convenient global accessor for the event loop.
118
+
119
+ #### get()
120
+
121
+ The ` get(): LoopInterface ` method is the preferred way to get and use the event loop. With
122
+ it there is no need to always pass the loop around anymore.
123
+
124
+ ``` php
125
+ use React\EventLoop\Loop;
126
+
127
+ Loop::get()->addTimer(0.02, function () {
128
+ echo 'World!';
129
+ });
130
+ Loop::get()->addTimer(0.01, function () {
131
+ echo 'Hello ';
132
+ });
133
+
134
+ Loop::get()->run();
135
+ ```
136
+
113
137
### Factory
114
138
115
139
The ` Factory ` class exists as a convenient way to pick the best available
0 commit comments