Skip to content

Commit b9b09be

Browse files
authored
Merge pull request #49 from clue-labs/no-factory
Replace `Factory` with simplified `Client` constructor
2 parents 5a1b473 + 61103e9 commit b9b09be

16 files changed

Lines changed: 158 additions & 254 deletions

README.md

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ its event-driven model to react to changes and events happening.
2727

2828
* [Quickstart example](#quickstart-example)
2929
* [Usage](#usage)
30-
* [Factory](#factory)
31-
* [createClient()](#createclient)
3230
* [Client](#client)
3331
* [Commands](#commands)
3432
* [Promises](#promises)
@@ -48,10 +46,9 @@ Docker API of your local docker daemon:
4846

4947
```php
5048
$loop = React\EventLoop\Factory::create();
51-
$factory = new Factory($loop);
52-
$client = $factory->createClient();
49+
$client = new Clue\React\Docker\Client($loop);
5350

54-
$client->imageSearch('clue')->then(function ($images) {
51+
$client->imageSearch('clue')->then(function (array $images) {
5552
var_dump($images);
5653
});
5754

@@ -62,45 +59,28 @@ See also the [examples](examples).
6259

6360
## Usage
6461

65-
### Factory
62+
### Client
6663

67-
The `Factory` is responsible for creating your `Client` instance.
68-
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
64+
The `Client` is responsible for assembling and sending HTTP requests to the Docker Engine API.
65+
It uses an HTTP client bound to the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
66+
in order to handle async requests:
6967

7068
```php
7169
$loop = React\EventLoop\Factory::create();
72-
$factory = new Factory($loop);
70+
$client = new Clue\React\Docker\Client($loop);
7371
```
7472

75-
If you need custom DNS, SSL/TLS or proxy settings, you can explicitly pass a
76-
custom [`Browser`](https://github.com/clue/php-buzz-react#browser) instance:
73+
If your Docker Engine API is not accessible using the default `unix:///var/run/docker.sock`
74+
Unix domain socket path, you may optionally pass an explicit URL like this:
7775

78-
```php
79-
$factory = new Factory($loop, $browser);
8076
```
81-
82-
#### createClient()
83-
84-
The `createClient($url = null)` method can be used to create a new `Client`.
85-
It helps with constructing a `Browser` object for the given remote URL.
86-
87-
```php
88-
// create client with default URL (unix:///var/run/docker.sock)
89-
$client = $factory->createClient();
90-
9177
// explicitly use given UNIX socket path
92-
$client = $factory->createClient('unix:///var/run/docker.sock');
78+
$client = new Clue\React\Docker\Client($loop, 'unix:///var/run/docker.sock');
9379
94-
// connect via TCP/IP
95-
$client = $factory->createClient('http://10.0.0.2:8000/');
80+
// or connect via TCP/IP to a remote Docker Engine API
81+
$client = new Clue\React\Docker\Client($loop, 'http://10.0.0.2:8000/');
9682
```
9783

98-
### Client
99-
100-
The `Client` is responsible for assembling and sending HTTP requests to the Docker API.
101-
It requires a `Browser` object bound to the main `EventLoop` in order to handle async requests and a base URL.
102-
The recommended way to create a `Client` is using the `Factory` (see above).
103-
10484
#### Commands
10585

10686
All public methods on the `Client` resemble the API described in the [Docker Engine API documentation](https://docs.docker.com/develop/sdk/) like this:
@@ -162,8 +142,7 @@ The resulting blocking code could look something like this:
162142
use Clue\React\Block;
163143

164144
$loop = React\EventLoop\Factory::create();
165-
$factory = new Factory($loop);
166-
$client = $factory->createClient();
145+
$client = new Clue\React\Docker\Client($loop);
167146

168147
$promise = $client->imageInspect('busybox');
169148

examples/archive.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<?php
2+
23
// this example shows how the containerArchiveStream() method returns a TAR stream,
34
// how it can be passed to a TAR decoder and how we can then pipe each
45
// individual file to the console output.
56

6-
require __DIR__ . '/../vendor/autoload.php';
7-
8-
use React\EventLoop\Factory as LoopFactory;
9-
use Clue\React\Docker\Factory;
7+
use Clue\CaretNotation\Encoder;
8+
use Clue\React\Docker\Client;
109
use Clue\React\Tar\Decoder;
1110
use React\Stream\ReadableStreamInterface;
12-
use Clue\CaretNotation\Encoder;
11+
12+
require __DIR__ . '/../vendor/autoload.php';
1313

1414
$container = isset($argv[1]) ? $argv[1] : 'asd';
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 = LoopFactory::create();
19-
20-
$factory = new Factory($loop);
21-
$client = $factory->createClient();
18+
$loop = React\EventLoop\Factory::create();
19+
$client = new Client($loop);
2220

2321
$stream = $client->containerArchiveStream($container, $path);
2422

examples/benchmark-exec.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<?php
2+
23
// this simple example executes a command within the given running container and
34
// displays how fast it can receive its output.
45
// expect this to be significantly faster than the (totally unfair) equivalent:
56
// $ docker exec asd dd if=/dev/zero bs=1M count=1000 | dd of=/dev/null
67

7-
require __DIR__ . '/../vendor/autoload.php';
8+
use Clue\React\Docker\Client;
89

9-
use React\EventLoop\Factory as LoopFactory;
10-
use Clue\React\Docker\Factory;
11-
use React\Stream\Stream;
10+
require __DIR__ . '/../vendor/autoload.php';
1211

1312
$container = 'asd';
1413
$cmd = array('dd', 'if=/dev/zero', 'bs=1M', 'count=1000');
@@ -18,10 +17,8 @@
1817
$cmd = array_slice($argv, 2);
1918
}
2019

21-
$loop = LoopFactory::create();
22-
23-
$factory = new Factory($loop);
24-
$client = $factory->createClient();
20+
$loop = React\EventLoop\Factory::create();
21+
$client = new Client($loop);
2522

2623
$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
2724
$stream = $client->execStartStream($info['Id'], true);

examples/events.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
<?php
2+
23
// this simple example displays all docker events that happen in the next 10s.
34
// try starting / removing a container in the meantime to see some output.
45

5-
require __DIR__ . '/../vendor/autoload.php';
6-
7-
use React\EventLoop\Factory as LoopFactory;
8-
use Clue\React\Docker\Factory;
6+
use Clue\React\Docker\Client;
97

10-
$loop = LoopFactory::create();
8+
require __DIR__ . '/../vendor/autoload.php';
119

12-
$factory = new Factory($loop);
13-
$client = $factory->createClient();
10+
$loop = React\EventLoop\Factory::create();
11+
$client = new Client($loop);
1412

1513
// get a list of all events that happened up until this point
1614
// expect this list to be limited to the last 64 (or so) events

examples/exec-inspect.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
2-
// this simple example executes a "sleep 2" within the given running container
32

4-
require __DIR__ . '/../vendor/autoload.php';
3+
// this simple example executes a "sleep 2" within the given running container
54

6-
use React\EventLoop\Factory as LoopFactory;
7-
use Clue\React\Docker\Factory;
85
use Clue\React\Buzz\Message\ResponseException;
6+
use Clue\React\Docker\Client;
7+
8+
require __DIR__ . '/../vendor/autoload.php';
99

1010
$container = 'asd';
1111
//$cmd = array('echo', 'hello world');
@@ -18,10 +18,8 @@
1818
$cmd = array_slice($argv, 2);
1919
}
2020

21-
$loop = LoopFactory::create();
22-
23-
$factory = new Factory($loop);
24-
$client = $factory->createClient();
21+
$loop = React\EventLoop\Factory::create();
22+
$client = new Client($loop);
2523

2624
$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
2725
echo 'Created with info: ' . json_encode($info) . PHP_EOL;

examples/exec-stream.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
2+
23
// this example executes some commands within the given running container and
34
// displays the streaming output as it happens.
45

5-
require __DIR__ . '/../vendor/autoload.php';
6-
7-
use React\EventLoop\Factory as LoopFactory;
8-
use Clue\React\Docker\Factory;
6+
use Clue\React\Docker\Client;
97
use React\Stream\Stream;
108

9+
require __DIR__ . '/../vendor/autoload.php';
10+
1111
$container = 'asd';
1212
//$cmd = array('echo', 'hello world');
1313
//$cmd = array('sleep', '2');
@@ -19,10 +19,8 @@
1919
$cmd = array_slice($argv, 2);
2020
}
2121

22-
$loop = LoopFactory::create();
23-
24-
$factory = new Factory($loop);
25-
$client = $factory->createClient();
22+
$loop = React\EventLoop\Factory::create();
23+
$client = new Client($loop);
2624

2725
$out = new Stream(STDOUT, $loop);
2826
$out->pause();

examples/export.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
<?php
2+
23
// this example shows how the containerExport() call returns a TAR stream
34
// and how we it can be piped into a output tar file.
45

5-
require __DIR__ . '/../vendor/autoload.php';
6-
7-
use React\EventLoop\StreamSelectLoop;
8-
use Clue\React\Docker\Factory;
6+
use Clue\React\Docker\Client;
97
use React\Stream\Stream;
108

9+
require __DIR__ . '/../vendor/autoload.php';
10+
1111
$container = isset($argv[1]) ? $argv[1] : 'asd';
1212
$target = isset($argv[2]) ? $argv[2] : ($container . '.tar');
1313
echo 'Exporting whole container "' . $container . '" to "' . $target .'" (pass as arguments to this example)' . PHP_EOL;
1414

15-
$loop = new StreamSelectLoop();
16-
17-
$factory = new Factory($loop);
18-
$client = $factory->createClient();
15+
$loop = React\EventLoop\Factory::create();
16+
$client = new Client($loop);
1917

2018
$stream = $client->containerExportStream($container);
2119

examples/info.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?php
2-
// this simple example displays system wide information from Docker as a simple JSON
32

4-
require __DIR__ . '/../vendor/autoload.php';
3+
// this simple example displays system wide information from Docker as a simple JSON
54

6-
use React\EventLoop\Factory as LoopFactory;
7-
use Clue\React\Docker\Factory;
5+
use Clue\React\Docker\Client;
86

9-
$loop = LoopFactory::create();
7+
require __DIR__ . '/../vendor/autoload.php';
108

11-
$factory = new Factory($loop);
12-
$client = $factory->createClient();
9+
$loop = React\EventLoop\Factory::create();
10+
$client = new Client($loop);
1311

1412
$client->info()->then(function ($info) {
1513
echo json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;

examples/pull.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
<?php
2+
23
// this example shows how the imageCreateStream() call can be used to pull a given image.
34
// demonstrates the JSON streaming API, individual progress events will be printed as they happen.
45

5-
require __DIR__ . '/../vendor/autoload.php';
6+
use Clue\React\Docker\Client;
67

7-
use React\EventLoop\Factory as LoopFactory;
8-
use Clue\React\Docker\Factory;
8+
require __DIR__ . '/../vendor/autoload.php';
99

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 = LoopFactory::create();
14-
15-
$factory = new Factory($loop);
16-
$client = $factory->createClient();
13+
$loop = React\EventLoop\Factory::create();
14+
$client = new Client($loop);
1715

1816
$stream = $client->imageCreateStream($image);
1917

examples/push.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
<?php
2+
23
// this example shows how the imagePush() call can be used to publish a given image.
34
// this requires authorization and this example includes some invalid defaults.
45

5-
require __DIR__ . '/../vendor/autoload.php';
6+
use Clue\React\Docker\Client;
67

7-
use React\EventLoop\Factory as LoopFactory;
8-
use Clue\React\Docker\Factory;
8+
require __DIR__ . '/../vendor/autoload.php';
99

1010
$image = isset($argv[1]) ? $argv[1] : 'asd';
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 = LoopFactory::create();
15-
16-
$factory = new Factory($loop);
17-
$client = $factory->createClient();
14+
$loop = React\EventLoop\Factory::create();
15+
$client = new Client($loop);
1816

1917
$client->imagePush($image, null, null, $auth)->then(function ($result) {
2018
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;

0 commit comments

Comments
 (0)