Skip to content

Commit 92c7efb

Browse files
authored
Merge pull request #71 from clue-labs/default-loop
Simplify usage by supporting new default loop
2 parents 7da1256 + 6a5eafe commit 92c7efb

19 files changed

+85
-96
lines changed

README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,11 @@ Once [installed](#install), you can use the following code to access the
5858
Docker API of your local docker daemon:
5959

6060
```php
61-
$loop = React\EventLoop\Factory::create();
62-
$client = new Clue\React\Docker\Client($loop);
61+
$client = new Clue\React\Docker\Client();
6362

6463
$client->imageSearch('clue')->then(function (array $images) {
6564
var_dump($images);
6665
});
67-
68-
$loop->run();
6966
```
7067

7168
See also the [examples](examples).
@@ -75,23 +72,26 @@ See also the [examples](examples).
7572
### Client
7673

7774
The `Client` is responsible for assembling and sending HTTP requests to the Docker Engine API.
78-
It uses an HTTP client bound to the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
79-
in order to handle async requests:
8075

8176
```php
82-
$loop = React\EventLoop\Factory::create();
83-
$client = new Clue\React\Docker\Client($loop);
77+
$client = new Clue\React\Docker\Client();
8478
```
8579

80+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
81+
pass the event loop instance to use for this object. You can use a `null` value
82+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
83+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
84+
given event loop instance.
85+
8686
If your Docker Engine API is not accessible using the default `unix:///var/run/docker.sock`
8787
Unix domain socket path, you may optionally pass an explicit URL like this:
8888

89-
```
89+
```php
9090
// explicitly use given UNIX socket path
91-
$client = new Clue\React\Docker\Client($loop, 'unix:///var/run/docker.sock');
91+
$client = new Clue\React\Docker\Client(null, 'unix:///var/run/docker.sock');
9292

9393
// or connect via TCP/IP to a remote Docker Engine API
94-
$client = new Clue\React\Docker\Client($loop, 'http://10.0.0.2:8000/');
94+
$client = new Clue\React\Docker\Client(null, 'http://10.0.0.2:8000/');
9595
```
9696

9797
#### Commands
@@ -154,13 +154,12 @@ The resulting blocking code could look something like this:
154154
```php
155155
use Clue\React\Block;
156156

157-
$loop = React\EventLoop\Factory::create();
158-
$client = new Clue\React\Docker\Client($loop);
157+
$client = new Clue\React\Docker\Client();
159158

160159
$promise = $client->imageInspect('busybox');
161160

162161
try {
163-
$results = Block\await($promise, $loop);
162+
$results = Block\await($promise, Loop::get());
164163
// resporesults successfully received
165164
} catch (Exception $e) {
166165
// an error occured while performing the request
@@ -175,7 +174,7 @@ $promises = array(
175174
$client->imageInspect('ubuntu'),
176175
);
177176

178-
$inspections = Block\awaitAll($promises, $loop);
177+
$inspections = Block\awaitAll($promises, Loop::get());
179178
```
180179

181180
Please refer to [clue/reactphp-block](https://github.com/clue/reactphp-block#readme) for more details.

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
"require": {
2020
"php": ">=5.3",
2121
"clue/json-stream": "^0.1",
22-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
23-
"react/http": "^1.0",
22+
"react/event-loop": "^1.2",
23+
"react/http": "^1.4",
2424
"react/promise": "^2.0 || ^1.1",
2525
"react/promise-stream": "^1.0",
26-
"react/socket": "^1.0",
27-
"react/stream": "^1.0",
26+
"react/socket": "^1.8",
27+
"react/stream": "^1.2",
2828
"rize/uri-template": "^0.3"
2929
},
3030
"require-dev": {

examples/archive.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
$path = isset($argv[2]) ? $argv[2] : '/etc/passwd';
1616
echo 'Container "' . $container . '" dumping "' . $path . '" (pass as arguments to this example)' . PHP_EOL;
1717

18-
$loop = React\EventLoop\Factory::create();
19-
$client = new Client($loop);
18+
$client = new Client();
2019

2120
$stream = $client->containerArchiveStream($container, $path);
2221

@@ -43,5 +42,3 @@
4342
});
4443

4544
$stream->pipe($tar);
46-
47-
$loop->run();

examples/attach-stream.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
$container = isset($argv[1]) ? $argv[1] : 'foo';
1515
echo 'Dumping output of container "' . $container . '" (pass as argument to this example)' . PHP_EOL;
1616

17-
$loop = React\EventLoop\Factory::create();
18-
$client = new Client($loop);
17+
$client = new Client();
1918

2019
// use caret notation for any control characters except \t, \r and \n
2120
$caret = new Encoder("\t\r\n");
@@ -33,5 +32,3 @@
3332
$stream->on('close', function () {
3433
echo 'CLOSED' . PHP_EOL;
3534
});
36-
37-
$loop->run();

examples/benchmark-attach.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// $ docker run -i --rm --log-driver=none busybox dd if=/dev/zero bs=1M count=1000 status=none | dd of=/dev/null
1212

1313
use Clue\React\Docker\Client;
14+
use React\EventLoop\Loop;
1415

1516
require __DIR__ . '/../vendor/autoload.php';
1617

@@ -26,8 +27,7 @@
2627
$cmd = array_slice($argv, 2);
2728
}
2829

29-
$loop = React\EventLoop\Factory::create();
30-
$client = new Client($loop);
30+
$client = new Client();
3131

3232
$client->containerCreate(array(
3333
'Image' => $image,
@@ -38,11 +38,11 @@
3838
'Type' => 'none'
3939
)
4040
)
41-
))->then(function ($container) use ($client, $loop) {
41+
))->then(function ($container) use ($client) {
4242
$stream = $client->containerAttachStream($container['Id'], false, true);
4343

4444
// we're creating the container without a log, so first wait for attach stream before starting
45-
$loop->addTimer(0.1, function () use ($client, $container) {
45+
Loop::addTimer(0.1, function () use ($client, $container) {
4646
$client->containerStart($container['Id'])->then(null, 'printf');
4747
});
4848

@@ -62,5 +62,3 @@
6262
echo 'Received ' . $bytes . ' bytes in ' . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
6363
});
6464
}, 'printf');
65-
66-
$loop->run();

examples/benchmark-exec.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
$cmd = array_slice($argv, 2);
3030
}
3131

32-
$loop = React\EventLoop\Factory::create();
33-
$client = new Client($loop);
32+
$client = new Client();
3433

3534
$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
3635
$stream = $client->execStartStream($info['Id'], true);
@@ -50,5 +49,3 @@
5049
echo 'Received ' . $bytes . ' bytes in ' . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
5150
});
5251
}, 'printf');
53-
54-
$loop->run();

examples/events.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
require __DIR__ . '/../vendor/autoload.php';
99

10-
$loop = React\EventLoop\Factory::create();
11-
$client = new Client($loop);
10+
$client = new Client();
1211

1312
// get a list of all events that happened up until this point
1413
// expect this list to be limited to the last 64 (or so) events
@@ -25,5 +24,3 @@
2524
});
2625

2726
$stream->on('error', 'printf');
28-
29-
$loop->run();

examples/exec-inspect.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
$cmd = array_slice($argv, 2);
1919
}
2020

21-
$loop = React\EventLoop\Factory::create();
22-
$client = new Client($loop);
21+
$client = new Client();
2322

2423
$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
2524
echo 'Created with info: ' . json_encode($info) . PHP_EOL;
@@ -43,5 +42,3 @@
4342
echo 'Response: ' . $e->getResponse()->getBody() . PHP_EOL;
4443
}
4544
});
46-
47-
$loop->run();

examples/exec-stream.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// displays the streaming output as it happens.
55

66
use Clue\React\Docker\Client;
7+
use React\EventLoop\Loop;
78
use React\Stream\WritableResourceStream;
89

910
require __DIR__ . '/../vendor/autoload.php';
@@ -23,11 +24,10 @@
2324
$cmd = array_slice($argv, 2);
2425
}
2526

26-
$loop = React\EventLoop\Factory::create();
27-
$client = new Client($loop);
27+
$client = new Client();
2828

29-
$out = new WritableResourceStream(STDOUT, $loop);
30-
$stderr = new WritableResourceStream(STDERR, $loop);
29+
$out = new WritableResourceStream(STDOUT);
30+
$stderr = new WritableResourceStream(STDERR);
3131

3232
// unkown exit code by default
3333
$exit = 1;
@@ -56,6 +56,6 @@
5656
});
5757
}, 'printf');
5858

59-
$loop->run();
59+
Loop::run();
6060

6161
exit($exit);

examples/export.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
$target = isset($argv[2]) ? $argv[2] : ($container . '.tar');
1717
echo 'Exporting whole container "' . $container . '" to "' . $target .'" (pass as arguments to this example)' . PHP_EOL;
1818

19-
$loop = React\EventLoop\Factory::create();
20-
$client = new Client($loop);
19+
$client = new Client();
2120

2221
$stream = $client->containerExportStream($container);
2322

@@ -26,7 +25,5 @@
2625
echo 'ERROR requesting stream' . PHP_EOL . $e;
2726
});
2827

29-
$out = new WritableResourceStream(fopen($target, 'w'), $loop);
28+
$out = new WritableResourceStream(fopen($target, 'w'));
3029
$stream->pipe($out);
31-
32-
$loop->run();

examples/info.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
require __DIR__ . '/../vendor/autoload.php';
88

9-
$loop = React\EventLoop\Factory::create();
10-
$client = new Client($loop);
9+
$client = new Client();
1110

1211
$client->info()->then(function ($info) {
1312
echo json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
1413
}, 'printf');
15-
16-
$loop->run();

examples/logs-stream.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
$container = isset($argv[1]) ? $argv[1] : 'asd';
1212
echo 'Dumping logs (last 100 lines) of container "' . $container . '" (pass as argument to this example)' . PHP_EOL;
1313

14-
$loop = React\EventLoop\Factory::create();
15-
$client = new Client($loop);
14+
$client = new Client();
1615

1716
// use caret notation for any control characters except \t, \r and \n
1817
$caret = new Encoder("\t\r\n");
@@ -30,5 +29,3 @@
3029
$stream->on('close', function ($e = null) {
3130
echo 'CLOSED' . PHP_EOL . $e;
3231
});
33-
34-
$loop->run();

examples/logs.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
$container = isset($argv[1]) ? $argv[1] : 'foo';
1616
echo 'Dumping logs (last 100 lines) of container "' . $container . '" (pass as argument to this example)' . PHP_EOL;
1717

18-
$loop = React\EventLoop\Factory::create();
19-
$client = new Client($loop);
18+
$client = new Client();
2019

2120
$client->containerLogs($container, false, true, true, 0, false, 100)->then(
2221
function ($logs) {
@@ -40,5 +39,3 @@ function ($error) use ($container) {
4039
EOT;
4140
}
4241
);
43-
44-
$loop->run();

examples/pull.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
$image = isset($argv[1]) ? $argv[1] : 'clue/redis-benchmark';
1111
echo 'Pulling image "' . $image . '" (pass as argument to this example)' . PHP_EOL;
1212

13-
$loop = React\EventLoop\Factory::create();
14-
$client = new Client($loop);
13+
$client = new Client();
1514

1615
$stream = $client->imageCreateStream($image);
1716

@@ -22,5 +21,3 @@
2221
$stream->on('close', function () {
2322
echo 'stream closed' . PHP_EOL;
2423
});
25-
26-
$loop->run();

examples/push.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
$auth = json_decode('{"username": "string", "password": "string", "email": "string", "serveraddress" : "string", "auth": ""}');
1212
echo 'Pushing image "' . $image . '" (pass as argument to this example)' . PHP_EOL;
1313

14-
$loop = React\EventLoop\Factory::create();
15-
$client = new Client($loop);
14+
$client = new Client();
1615

1716
$client->imagePush($image, null, null, $auth)->then(function ($result) {
1817
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
1918
}, 'printf');
20-
21-
$loop->run();

examples/resize.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
$container = isset($argv[1]) ? $argv[1] : 'asd';
1111

12-
$loop = React\EventLoop\Factory::create();
13-
$client = new Client($loop);
12+
$client = new Client();
1413

1514
$client->containerInspect($container)->then(function ($info) use ($client, $container) {
1615
$size = $info['HostConfig']['ConsoleSize'];
@@ -21,6 +20,3 @@
2120
})->then(function () use ($client) {
2221
echo 'Successfully set' . PHP_EOL;
2322
}, 'printf');
24-
25-
26-
$loop->run();

examples/stats.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
$container = isset($argv[1]) ? $argv[1] : 'asd';
1111
echo 'Monitoring "' . $container . '" (pass as argument to this example)' . PHP_EOL;
1212

13-
$loop = React\EventLoop\Factory::create();
14-
$client = new Client($loop);
13+
$client = new Client();
1514

1615
$stream = $client->containerStatsStream($container);
1716

@@ -31,5 +30,3 @@
3130
$stream->on('close', function () {
3231
echo 'stream closed' . PHP_EOL;
3332
});
34-
35-
$loop->run();

0 commit comments

Comments
 (0)