@@ -8,32 +8,18 @@ Event loop abstraction layer that libraries can use for evented I/O.
88In order for async based libraries to be interoperable, they need to use the
99same event loop. This component provides a common ` LoopInterface ` that any
1010library can target. This allows them to be used in the same loop, with one
11- single ` run ` call that is controlled by the user.
11+ single ` run() ` call that is controlled by the user.
1212
13- In addition to the interface there are some implementations provided:
13+ ** Table of Contents **
1414
15- * ` StreamSelectLoop ` : This is the only implementation which works out of the
16- box with PHP. It does a simple ` select ` system call. It's not the most
17- performant of loops, but still does the job quite well.
18-
19- * ` LibEventLoop ` : This uses the ` libevent ` pecl extension. ` libevent ` itself
20- supports a number of system-specific backends (epoll, kqueue).
21-
22- * ` LibEvLoop ` : This uses the ` libev ` pecl extension
23- ([ github] ( https://github.com/m4rw3r/php-libev ) ). It supports the same
24- backends as libevent.
25-
26- * ` ExtEventLoop ` : This uses the ` event ` pecl extension. It supports the same
27- backends as libevent.
28-
29- All of the loops support these features:
30-
31- * File descriptor polling
32- * One-off timers
33- * Periodic timers
34- * Deferred execution of callbacks
15+ * [ Quickstart example] ( #quickstart-example )
16+ * [ Usage] ( #usage )
17+ * [ Loop implementations] ( #loop-implementations )
18+ * [ Install] ( #install )
19+ * [ Tests] ( #tests )
20+ * [ License] ( #license )
3521
36- ## Usage
22+ ## Quickstart example
3723
3824Here is an async HTTP server built with just the event loop.
3925
@@ -42,6 +28,7 @@ $loop = React\EventLoop\Factory::create();
4228
4329$server = stream_socket_server('tcp://127.0.0.1:8080');
4430stream_set_blocking($server, 0);
31+
4532$loop->addReadStream($server, function ($server) use ($loop) {
4633 $conn = stream_socket_accept($server);
4734 $data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
@@ -65,7 +52,92 @@ $loop->addPeriodicTimer(5, function () {
6552$loop->run();
6653```
6754
68- ** Note:** The factory is just for convenience. It tries to pick the best
69- available implementation. Libraries ` SHOULD ` allow the user to inject an
70- instance of the loop. They ` MAY ` use the factory when the user did not supply
71- a loop.
55+ ## Usage
56+
57+ Typical applications use a single event loop which is created at the beginning
58+ and run at the end of the program.
59+
60+ ``` php
61+ // [1]
62+ $loop = React\EventLoop\Factory::create();
63+
64+ // [2]
65+ $loop->addPeriodicTimer(1, function () {
66+ echo "Tick\n";
67+ });
68+
69+ $stream = new React\Stream\ReadableResourceStream(
70+ fopen('file.txt', 'r'),
71+ $loop
72+ );
73+
74+ // [3]
75+ $loop->run();
76+ ```
77+
78+ 1 . The loop instance is created at the beginning of the program. A convenience
79+ factory ` React\EventLoop\Factory::create() ` is provided by this library which
80+ picks the best available [ loop implementation] ( #loop-implementations ) .
81+ 2 . The loop instance is used directly or passed to library and application code.
82+ In this example, a periodic timer is registered with the event loop which
83+ simply outputs ` Tick ` every second and a
84+ [ readable stream] ( https://github.com/reactphp/stream#readableresourcestream )
85+ is created by using ReactPHP's
86+ [ stream component] ( https://github.com/reactphp/stream ) for demonstration
87+ purposes.
88+ 3 . The loop is run with a single ` $loop->run() ` call at the end of the program.
89+
90+ ## Loop implementations
91+
92+ In addition to the interface there are the following implementations provided:
93+
94+ * ` StreamSelectLoop ` : This is the only implementation which works out of the
95+ box with PHP. It does a simple ` select ` system call. It's not the most
96+ performant of loops, but still does the job quite well.
97+
98+ * ` LibEventLoop ` : This uses the ` libevent ` pecl extension. ` libevent ` itself
99+ supports a number of system-specific backends (epoll, kqueue).
100+
101+ * ` LibEvLoop ` : This uses the ` libev ` pecl extension
102+ ([ github] ( https://github.com/m4rw3r/php-libev ) ). It supports the same
103+ backends as libevent.
104+
105+ * ` ExtEventLoop ` : This uses the ` event ` pecl extension. It supports the same
106+ backends as libevent.
107+
108+ All of the loops support these features:
109+
110+ * File descriptor polling
111+ * One-off timers
112+ * Periodic timers
113+ * Deferred execution of callbacks
114+
115+ ## Install
116+
117+ The recommended way to install this library is [ through Composer] ( http://getcomposer.org ) .
118+ [ New to Composer?] ( http://getcomposer.org/doc/00-intro.md )
119+
120+ This will install the latest supported version:
121+
122+ ``` bash
123+ $ composer require react/event-loop
124+ ```
125+
126+ ## Tests
127+
128+ To run the test suite, you first need to clone this repo and then install all
129+ dependencies [ through Composer] ( http://getcomposer.org ) :
130+
131+ ``` bash
132+ $ composer install
133+ ```
134+
135+ To run the test suite, go to the project root and run:
136+
137+ ``` bash
138+ $ php vendor/bin/phpunit
139+ ```
140+
141+ ## License
142+
143+ MIT, see [ LICENSE file] ( LICENSE ) .
0 commit comments