|
1 | 1 | # ReactPHP functions
|
2 | 2 |
|
3 |
| -Set of simple PHP functions turned non-blocking on too of |
4 |
| -[ReactPHP](https://reactphp.org/) |
5 |
| - |
6 | 3 | [](https://circleci.com/gh/driftphp/reactphp-functions)
|
7 | 4 |
|
8 |
| -**Table of Contents** |
9 |
| -- [Quickstart example](#quickstart-example) |
10 |
| -- [Usage](#usage) |
11 |
| - - [sleep()](#sleep) |
12 |
| - - [usleep()](#usleep) |
13 |
| - - [mime_content_type()](#mime_content_type) |
14 |
| -- [Install](#install) |
15 |
| -- [Tests](#tests) |
16 |
| -- [License](#license) |
17 |
| - |
18 |
| -## Quickstart example |
19 |
| - |
20 |
| -You can easily add a sleep in your non-blocking code by using these functions. |
21 |
| -In this code you can wait 1 second before continuing with the execution of your |
22 |
| -promise, while the loop is actively switching between active Promises. |
23 |
| - |
24 |
| -```php |
25 |
| - // use a unique event loop instance for all parallel operations |
26 |
| - $loop = React\EventLoop\Factory::create(); |
27 |
| - |
28 |
| - $promise = $this |
29 |
| - ->doWhateverNonBlocking($loop) |
30 |
| - ->then(function() use ($loop) { |
31 |
| - |
32 |
| - // This will be executed on time t=0 |
33 |
| - return React\sleep(1, $loop); |
34 |
| - }) |
35 |
| - ->then(function() { |
36 |
| - |
37 |
| - // This will be executed on time t=1 |
38 |
| - }); |
39 |
| -``` |
40 |
| - |
41 |
| -## Usage |
42 |
| - |
43 |
| -This lightweight library has some small methods with the exact behavior than |
44 |
| -their sibling methods in regular and blocking PHP. |
45 |
| - |
46 |
| -```php |
47 |
| -use Drift\React; |
48 |
| - |
49 |
| -React\sleep(...); |
50 |
| -``` |
51 |
| - |
52 |
| -## EventLoop |
53 |
| - |
54 |
| -Each function is responsible for orchestrating the [EventLoop](https://github.com/reactphp/event-loop#usage) |
55 |
| -in order to make it run (block) until your conditions are fulfilled. |
56 |
| - |
57 |
| -```php |
58 |
| -$loop = React\EventLoop\Factory::create(); |
59 |
| -``` |
60 |
| - |
61 |
| -### sleep |
62 |
| - |
63 |
| -The `sleep($seconds, LoopInterface $loop)` method can be used to sleep for |
64 |
| -$time seconds. |
65 |
| - |
66 |
| -```php |
67 |
| -React\sleep(1.5, $loop) |
68 |
| - ->then(function() { |
69 |
| - // Do whatever you need after 1.5 seconds |
70 |
| - }); |
71 |
| -``` |
72 |
| - |
73 |
| -It is important to understand all the possible sleep implementations you can use |
74 |
| -under this reactive programming environment. |
75 |
| - |
76 |
| -- `\sleep($time)` - Block the PHP thread n seconds, and after this time is |
77 |
| -elapsed, continue from the same point |
78 |
| -- `\Clue\React\Block\sleep($time, $loop` - Don't block the PHP thread, but let |
79 |
| -the loop continue doing cycles. Block the program execution after n seconds, and |
80 |
| -after this time is elapsed, continue from the same point. This is a blocking |
81 |
| -feature. |
82 |
| -- `\Drift\React\sleep($time, $loop)` - Don't block neither the PHP thread nor |
83 |
| -the program execution. This method returns a Promise that will be resolved after |
84 |
| -n seconds. This is a non-blocking feature. |
85 |
| - |
86 |
| -### usleep |
87 |
| - |
88 |
| -The `sleep($seconds, LoopInterface $loop)` method can be used to sleep for |
89 |
| -$time microseconds. |
90 |
| - |
91 |
| -```php |
92 |
| -React\usleep(3000, $loop) |
93 |
| - ->then(function() { |
94 |
| - // Do whatever you need after 3000 microseconds |
95 |
| - }); |
96 |
| -``` |
97 |
| - |
98 |
| -The same rationale than the [`React\sleep`](#sleep) method. This is a |
99 |
| -non-blocking action. |
100 |
| - |
101 |
| -### mime_content_type |
102 |
| - |
103 |
| -The `mime_content_type("/tmp/file.png", LoopInterface $loop)` method can be used |
104 |
| -to guess the mime content type of a file. If failure, then rejects with a |
105 |
| -RuntimeException. |
106 |
| - |
107 |
| -```php |
108 |
| -React\mime_content_type("/tmp/file.png", $loop) |
109 |
| - ->then(function(string $type) { |
110 |
| - // Do whatever you need with the found mime type |
111 |
| - }); |
112 |
| -``` |
113 |
| - |
114 |
| -This is a non-blocking action and equals the regular PHP function |
115 |
| -[mime_content_type](https://www.php.net/manual/en/function.mime-content-type.php). |
116 |
| - |
117 |
| -## Install |
118 |
| - |
119 |
| -The recommended way to install this library is [through Composer](https://getcomposer.org). |
120 |
| -[New to Composer?](https://getcomposer.org/doc/00-intro.md) |
121 |
| - |
122 |
| -This project follows [SemVer](https://semver.org/). |
123 |
| -This will install the latest supported version: |
124 |
| - |
125 |
| -```bash |
126 |
| -$ composer require driftphp/react-functions:dev-master |
127 |
| -``` |
128 |
| - |
129 |
| -This library requires PHP7. |
130 |
| - |
131 |
| -## Tests |
132 |
| - |
133 |
| -To run the test suite, you first need to clone this repo and then install all |
134 |
| -dependencies [through Composer](https://getcomposer.org): |
135 |
| - |
136 |
| -```bash |
137 |
| -$ composer install |
138 |
| -``` |
| 5 | +Set of simple PHP functions turned non-blocking on too of |
| 6 | +[ReactPHP](https://reactphp.org/). |
139 | 7 |
|
140 |
| -To run the test suite, go to the project root and run: |
| 8 | +Some first steps for you! |
141 | 9 |
|
142 |
| -```bash |
143 |
| -$ php vendor/bin/phpunit |
144 |
| -``` |
| 10 | +- [Go to DOCS](https://driftphp.io/#/?id=reactphp-functions) |
145 | 11 |
|
146 |
| -## License |
| 12 | +or |
147 | 13 |
|
148 |
| -This project is released under the permissive [MIT license](LICENSE). |
| 14 | +- [Try a demo](https://github.com/driftphp/demo) |
| 15 | +- [Install the skeleton](https://github.com/driftphp/skeleton) |
0 commit comments