44
55use Clue \React \Buzz \Browser ;
66use Clue \React \Buzz \Message \Response ;
7+ use Clue \React \Docker \Io \ResponseParser ;
78
89/**
910 *
1213class Client
1314{
1415 private $ browser ;
16+ private $ parser ;
1517
16- public function __construct (Browser $ browser )
18+ public function __construct (Browser $ browser, ResponseParser $ parser = null )
1719 {
20+ if ($ parser === null ) {
21+ $ parser = new ResponseParser ();
22+ }
23+
1824 $ this ->browser = $ browser ;
25+ $ this ->parser = $ parser ;
1926 }
2027
2128 public function ping ()
2229 {
23- return $ this ->browser ->get ('/_ping ' )->then (array ($ this , 'expectPlain ' ));
30+ return $ this ->browser ->get ('/_ping ' )->then (array ($ this -> parser , 'expectPlain ' ));
2431 }
2532
2633 public function info ()
2734 {
28- return $ this ->browser ->get ('/info ' )->then (array ($ this , 'expectJson ' ));
35+ return $ this ->browser ->get ('/info ' )->then (array ($ this -> parser , 'expectJson ' ));
2936 }
3037
3138 public function version ()
3239 {
33- return $ this ->browser ->get ('/version ' )->then (array ($ this , 'expectJson ' ));
40+ return $ this ->browser ->get ('/version ' )->then (array ($ this -> parser , 'expectJson ' ));
3441 }
3542
3643 public function containerList ($ all = false , $ size = false )
3744 {
38- return $ this ->browser ->get ('/containers/json?all= ' . $ all . '&size= ' . $ size )->then (array ($ this , 'expectJson ' ));
45+ return $ this ->browser ->get ('/containers/json?all= ' . $ all . '&size= ' . $ size )->then (array ($ this -> parser , 'expectJson ' ));
3946 }
4047
4148 public function containerInspect ($ container )
4249 {
43- return $ this ->browser ->get ('/containers/ ' . $ container . '/json ' )->then (array ($ this , 'expectJson ' ));
50+ return $ this ->browser ->get ('/containers/ ' . $ container . '/json ' )->then (array ($ this -> parser , 'expectJson ' ));
4451 }
4552
4653 public function containerTop ($ container )
4754 {
48- return $ this ->browser ->get ('/containers/ ' . $ container . '/top ' )->then (array ($ this , 'expectJson ' ));
55+ return $ this ->browser ->get ('/containers/ ' . $ container . '/top ' )->then (array ($ this -> parser , 'expectJson ' ));
4956 }
5057
5158 public function containerWait ($ container )
5259 {
53- return $ this ->browser ->post ('/containers/ ' . $ container . '/wait ' )->then (array ($ this , 'expectJson ' ));
60+ return $ this ->browser ->post ('/containers/ ' . $ container . '/wait ' )->then (array ($ this -> parser , 'expectJson ' ));
5461 }
5562
5663 /**
@@ -61,7 +68,7 @@ public function containerWait($container)
6168 */
6269 public function containerStop ($ container , $ t )
6370 {
64- return $ this ->browser ->post ('/containers/ ' . $ container . '/stop?t= ' . $ t )->then (array ($ this , 'expectEmpty ' ));
71+ return $ this ->browser ->post ('/containers/ ' . $ container . '/stop?t= ' . $ t )->then (array ($ this -> parser , 'expectEmpty ' ));
6572 }
6673
6774 /**
@@ -72,22 +79,22 @@ public function containerStop($container, $t)
7279 */
7380 public function containerRestart ($ container , $ t )
7481 {
75- return $ this ->browser ->post ('/containers/ ' . $ container . '/restart?t= ' . $ t )->then (array ($ this , 'expectEmpty ' ));
82+ return $ this ->browser ->post ('/containers/ ' . $ container . '/restart?t= ' . $ t )->then (array ($ this -> parser , 'expectEmpty ' ));
7683 }
7784
7885 public function containerKill ($ container , $ signal = null )
7986 {
80- return $ this ->browser ->post ('/containers/ ' . $ container . '/kill?signal= ' . $ signal )->then (array ($ this , 'expectEmpty ' ));
87+ return $ this ->browser ->post ('/containers/ ' . $ container . '/kill?signal= ' . $ signal )->then (array ($ this -> parser , 'expectEmpty ' ));
8188 }
8289
8390 public function containerPause ($ container )
8491 {
85- return $ this ->browser ->post ('/containers/ ' . $ container . '/pause ' )->then (array ($ this , 'expectEmpty ' ));
92+ return $ this ->browser ->post ('/containers/ ' . $ container . '/pause ' )->then (array ($ this -> parser , 'expectEmpty ' ));
8693 }
8794
8895 public function containerUnpause ($ container )
8996 {
90- return $ this ->browser ->post ('/containers/ ' . $ container . '/unpause ' )->then (array ($ this , 'expectEmpty ' ));
97+ return $ this ->browser ->post ('/containers/ ' . $ container . '/unpause ' )->then (array ($ this -> parser , 'expectEmpty ' ));
9198 }
9299
93100 /**
@@ -99,33 +106,11 @@ public function containerUnpause($container)
99106 */
100107 public function containerDelete ($ container , $ v = false , $ force = false )
101108 {
102- return $ this ->browser ->delete ('/containers/ ' . $ container . '?v= ' . (int )$ v . '&force= ' . (int )$ force )->then (array ($ this , 'expectEmpty ' ));
109+ return $ this ->browser ->delete ('/containers/ ' . $ container . '?v= ' . (int )$ v . '&force= ' . (int )$ force )->then (array ($ this -> parser , 'expectEmpty ' ));
103110 }
104111
105112 public function containerResize ($ container , $ w , $ h )
106113 {
107- return $ this ->browser ->get ('/containers/ ' . $ container . '/resize?w= ' . $ w . '&h= ' . $ h )->then (array ($ this , 'expectEmpty ' ));
108- }
109-
110- public function expectPlain (Response $ response )
111- {
112- // text/plain
113-
114- return (string )$ response ->getBody ();
115- }
116-
117- public function expectJson (Response $ response )
118- {
119- // application/json
120-
121- return json_decode ((string )$ response ->getBody (), true );
122- }
123-
124- public function expectEmpty (Response $ response )
125- {
126- // 204 No Content
127- // no content-type
128-
129- return $ this ->expectPlain ($ response );
114+ return $ this ->browser ->get ('/containers/ ' . $ container . '/resize?w= ' . $ w . '&h= ' . $ h )->then (array ($ this ->parser , 'expectEmpty ' ));
130115 }
131116}
0 commit comments