Skip to content

Commit 3c80ff7

Browse files
committed
Remember Docker URL, support TCP/IP connections (closes #2)
1 parent 5812d4f commit 3c80ff7

3 files changed

Lines changed: 33 additions & 18 deletions

File tree

src/Client.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,53 @@
1313
class Client
1414
{
1515
private $browser;
16+
private $url;
1617
private $parser;
1718

18-
public function __construct(Browser $browser, ResponseParser $parser = null)
19+
public function __construct(Browser $browser, $url, ResponseParser $parser = null)
1920
{
2021
if ($parser === null) {
2122
$parser = new ResponseParser();
2223
}
2324

2425
$this->browser = $browser;
26+
$this->url = $url;
2527
$this->parser = $parser;
2628
}
2729

2830
public function ping()
2931
{
30-
return $this->browser->get('/_ping')->then(array($this->parser, 'expectPlain'));
32+
return $this->browser->get($this->url('/_ping'))->then(array($this->parser, 'expectPlain'));
3133
}
3234

3335
public function info()
3436
{
35-
return $this->browser->get('/info')->then(array($this->parser, 'expectJson'));
37+
return $this->browser->get($this->url('/info'))->then(array($this->parser, 'expectJson'));
3638
}
3739

3840
public function version()
3941
{
40-
return $this->browser->get('/version')->then(array($this->parser, 'expectJson'));
42+
return $this->browser->get($this->url('/version'))->then(array($this->parser, 'expectJson'));
4143
}
4244

4345
public function containerList($all = false, $size = false)
4446
{
45-
return $this->browser->get('/containers/json?all=' . $all . '&size=' . $size)->then(array($this->parser, 'expectJson'));
47+
return $this->browser->get($this->url('/containers/json?all=' . $all . '&size=' . $size))->then(array($this->parser, 'expectJson'));
4648
}
4749

4850
public function containerInspect($container)
4951
{
50-
return $this->browser->get('/containers/' . $container . '/json')->then(array($this->parser, 'expectJson'));
52+
return $this->browser->get($this->url('/containers/' . $container . '/json'))->then(array($this->parser, 'expectJson'));
5153
}
5254

5355
public function containerTop($container)
5456
{
55-
return $this->browser->get('/containers/' . $container . '/top')->then(array($this->parser, 'expectJson'));
57+
return $this->browser->get($this->url('/containers/' . $container . '/top'))->then(array($this->parser, 'expectJson'));
5658
}
5759

5860
public function containerWait($container)
5961
{
60-
return $this->browser->post('/containers/' . $container . '/wait')->then(array($this->parser, 'expectJson'));
62+
return $this->browser->post($this->url('/containers/' . $container . '/wait'))->then(array($this->parser, 'expectJson'));
6163
}
6264

6365
/**
@@ -68,7 +70,7 @@ public function containerWait($container)
6870
*/
6971
public function containerStop($container, $t)
7072
{
71-
return $this->browser->post('/containers/' . $container . '/stop?t=' . $t)->then(array($this->parser, 'expectEmpty'));
73+
return $this->browser->post($this->url('/containers/' . $container . '/stop?t=' . $t))->then(array($this->parser, 'expectEmpty'));
7274
}
7375

7476
/**
@@ -79,22 +81,22 @@ public function containerStop($container, $t)
7981
*/
8082
public function containerRestart($container, $t)
8183
{
82-
return $this->browser->post('/containers/' . $container . '/restart?t=' . $t)->then(array($this->parser, 'expectEmpty'));
84+
return $this->browser->post($this->url('/containers/' . $container . '/restart?t=' . $t))->then(array($this->parser, 'expectEmpty'));
8385
}
8486

8587
public function containerKill($container, $signal = null)
8688
{
87-
return $this->browser->post('/containers/' . $container . '/kill?signal=' . $signal)->then(array($this->parser, 'expectEmpty'));
89+
return $this->browser->post($this->url('/containers/' . $container . '/kill?signal=' . $signal))->then(array($this->parser, 'expectEmpty'));
8890
}
8991

9092
public function containerPause($container)
9193
{
92-
return $this->browser->post('/containers/' . $container . '/pause')->then(array($this->parser, 'expectEmpty'));
94+
return $this->browser->post($this->url('/containers/' . $container . '/pause'))->then(array($this->parser, 'expectEmpty'));
9395
}
9496

9597
public function containerUnpause($container)
9698
{
97-
return $this->browser->post('/containers/' . $container . '/unpause')->then(array($this->parser, 'expectEmpty'));
99+
return $this->browser->post($this->url('/containers/' . $container . '/unpause'))->then(array($this->parser, 'expectEmpty'));
98100
}
99101

100102
/**
@@ -106,11 +108,16 @@ public function containerUnpause($container)
106108
*/
107109
public function containerDelete($container, $v = false, $force = false)
108110
{
109-
return $this->browser->delete('/containers/' . $container . '?v=' . (int)$v . '&force=' . (int)$force)->then(array($this->parser, 'expectEmpty'));
111+
return $this->browser->delete($this->url('/containers/' . $container . '?v=' . (int)$v . '&force=' . (int)$force))->then(array($this->parser, 'expectEmpty'));
110112
}
111113

112114
public function containerResize($container, $w, $h)
113115
{
114-
return $this->browser->get('/containers/' . $container . '/resize?w=' . $w . '&h=' . $h)->then(array($this->parser, 'expectEmpty'));
116+
return $this->browser->get($this->url('/containers/' . $container . '/resize?w=' . $w . '&h=' . $h))->then(array($this->parser, 'expectEmpty'));
117+
}
118+
119+
private function url($url)
120+
{
121+
return $this->url . $url;
115122
}
116123
}

src/Factory.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ public function createClient($url = null)
2323
$url = 'unix:///var/run/docker.sock';
2424
}
2525

26-
$sender = Sender::createFromLoopUnix($this->loop, $url);
26+
$sender = null;
27+
28+
if (substr($url, 0, 7) === 'unix://') {
29+
// send everything through a local unix domain socket
30+
$sender = Sender::createFromLoopUnix($this->loop, $url);
31+
32+
// pretend all HTTP URLs to be on localhost
33+
$url = 'http://localhost';
34+
}
2735

2836
$browser = new Browser($this->loop, $sender);
2937

30-
return new Client($browser);
38+
return new Client($browser, $url);
3139
}
3240
}

tests/ClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function setUp()
1515
{
1616
$this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->disableOriginalConstructor()->getMock();
1717
$this->parser = $this->getMock('Clue\React\Docker\Io\ResponseParser');
18-
$this->client = new Client($this->browser, $this->parser);
18+
$this->client = new Client($this->browser, '', $this->parser);
1919
}
2020

2121
public function testPing()

0 commit comments

Comments
 (0)