2
2
3
3
namespace Polkovnik \Component \DockerClient ;
4
4
5
+ use GuzzleHttp \Exception \GuzzleException ;
5
6
use Polkovnik \Component \DockerClient \Exception \BadParameterException ;
6
7
use Polkovnik \Component \DockerClient \Exception \DockerSocketNotFound ;
7
8
use Polkovnik \Component \DockerClient \Exception \ResourceBusyException ;
8
9
use Polkovnik \Component \DockerClient \Exception \ResourceNotFound ;
9
- use Symfony \Component \HttpClient \CurlHttpClient ;
10
- use Symfony \Component \HttpClient \Exception \ClientException ;
10
+ use GuzzleHttp \Client as HttpClient ;
11
11
12
12
class DockerClient
13
13
{
14
- /** @var CurlHttpClient */
14
+ /** @var HttpClient */
15
15
private $ http ;
16
16
17
17
/** @var array */
@@ -38,7 +38,7 @@ public function __construct($options = [])
38
38
$ this ->dockerApiEndpoint = $ options ['docker_base_uri ' ];
39
39
}
40
40
41
- $ this ->http = new CurlHttpClient ([
41
+ $ this ->http = new HttpClient ([
42
42
'base_uri ' => $ this ->dockerApiEndpoint ,
43
43
]);
44
44
@@ -55,7 +55,7 @@ private function testConnection()
55
55
return $ this ->info ();
56
56
} catch (\Exception $ e ) {
57
57
$ search = 'failed binding local connection end ' ;
58
- if (str_contains (strtolower ($ e ->getMessage ()), $ search )) {
58
+ if (strpos (strtolower ($ e ->getMessage ()), $ search ) !== false ) {
59
59
$ text = sprintf ('Could not bind to docker socket at %s ' , $ this ->unixSocket );
60
60
throw new DockerSocketNotFound ($ text );
61
61
}
@@ -70,51 +70,38 @@ private function testConnection()
70
70
* @param array $options
71
71
* @param bool $resolveResponse
72
72
*
73
- * @return mixed|\Symfony\Contracts\HttpClient \ResponseInterface
73
+ * @return mixed|\Psr\Http\Message \ResponseInterface
74
74
*
75
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
76
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
77
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
78
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
75
+ * @throws GuzzleException
79
76
*/
80
77
private function request ($ method , $ url , $ options = [], $ resolveResponse = true )
81
78
{
82
- $ options = array_replace_recursive (['bindto ' => $ this ->unixSocket ], $ options );
79
+ $ options = array_replace_recursive (['curl ' => [ CURLOPT_UNIX_SOCKET_PATH => $ this ->unixSocket ] ], $ options );
83
80
$ response = $ this ->http ->request ($ method , $ url , $ options );
84
81
if ($ resolveResponse ) {
85
- return json_decode ($ response ->getContent (), true );
82
+ return json_decode ($ response ->getBody ()-> getContents (), true );
86
83
} else {
87
84
return $ response ;
88
85
}
89
86
}
90
87
91
88
/**
92
- * Retrieves information about the Docker engine.
89
+ * @return mixed|\Psr\Http\Message\ResponseInterface
93
90
*
94
- * @return mixed|\Symfony\Contracts\HttpClient\ResponseInterface
95
- *
96
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
97
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
98
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
99
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
91
+ * @throws GuzzleException
100
92
*/
101
93
public function info ()
102
94
{
103
95
return $ this ->request ('GET ' , '/info ' );
104
96
}
105
97
106
98
/**
107
- * Returns a list of containers
108
- *
109
99
* @param array $options
110
100
*
111
- * @return array|null
101
+ * @return mixed|\Psr\Http\Message\ResponseInterface
112
102
*
113
103
* @throws BadParameterException
114
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
115
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
116
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
117
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
104
+ * @throws GuzzleException
118
105
*/
119
106
public function listContainers ($ options = [])
120
107
{
@@ -138,7 +125,7 @@ public function listContainers($options = [])
138
125
$ endpoint = urldecode ($ endpoint );
139
126
try {
140
127
return $ this ->request ('GET ' , $ endpoint );
141
- } catch (ClientException $ e ) {
128
+ } catch (GuzzleException $ e ) {
142
129
$ code = $ e ->getCode ();
143
130
if ($ code === 400 ) {
144
131
throw new BadParameterException ($ e ->getMessage (), $ e ->getCode ());
@@ -153,10 +140,7 @@ public function listContainers($options = [])
153
140
*
154
141
* @return bool
155
142
*
156
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
157
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
158
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
159
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
143
+ * @throws GuzzleException
160
144
* @throws ResourceNotFound
161
145
*/
162
146
public function stopContainer ($ id )
@@ -166,7 +150,7 @@ public function stopContainer($id)
166
150
if ($ response ->getStatusCode () === 204 ) {
167
151
return true ;
168
152
}
169
- } catch (ClientException $ e ) {
153
+ } catch (GuzzleException $ e ) {
170
154
$ code = $ e ->getCode ();
171
155
if ($ code === 404 ) {
172
156
$ text = sprintf ('No such container: %s ' , $ id );
@@ -182,21 +166,19 @@ public function stopContainer($id)
182
166
/**
183
167
* @param $id
184
168
*
185
- * @return mixed
169
+ * @return mixed|\Psr\Http\Message\ResponseInterface
186
170
*
187
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
188
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
189
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
190
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
171
+ * @throws GuzzleException
172
+ * @throws ResourceNotFound
191
173
*/
192
174
public function startContainer ($ id )
193
175
{
194
176
try {
195
177
return $ this ->request ('POST ' , sprintf ('/containers/%s/start ' , $ id ));
196
- } catch (ClientException $ e ) {
178
+ } catch (GuzzleException $ e ) {
197
179
if ($ e ->getCode () === 404 ) {
198
180
$ text = sprintf ('No such container: %s ' , $ id );
199
- throw new ResourceNotFound ($ text , $ e ->getCode (), $ e );
181
+ throw new ResourceNotFound ($ text , $ e ->getCode ());
200
182
}
201
183
202
184
throw $ e ;
@@ -207,12 +189,10 @@ public function startContainer($id)
207
189
* @param $name
208
190
* @param $payload
209
191
*
210
- * @return string| false
192
+ * @return false|mixed
211
193
*
212
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
213
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
214
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
215
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
194
+ * @throws GuzzleException
195
+ * @throws ResourceNotFound
216
196
*/
217
197
public function runContainer ($ name , $ payload )
218
198
{
@@ -232,21 +212,19 @@ public function runContainer($name, $payload)
232
212
/**
233
213
* @param $id
234
214
*
235
- * @return array
215
+ * @return mixed|\Psr\Http\Message\ResponseInterface
236
216
*
237
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
238
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
239
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
240
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
217
+ * @throws GuzzleException
218
+ * @throws ResourceNotFound
241
219
*/
242
220
public function inspectContainer ($ id )
243
221
{
244
222
try {
245
223
return $ this ->request ('GET ' , sprintf ('/containers/%s/json ' , $ id ));
246
- } catch (ClientException $ e ) {
224
+ } catch (GuzzleException $ e ) {
247
225
if ($ e ->getCode () === 404 ) {
248
226
$ text = sprintf ('No such container: %s ' , $ id );
249
- throw new ResourceNotFound ($ text , 404 , $ e );
227
+ throw new ResourceNotFound ($ text , 404 );
250
228
}
251
229
252
230
throw $ e ;
@@ -255,23 +233,21 @@ public function inspectContainer($id)
255
233
256
234
/**
257
235
* @param $id
258
- * @param bool $oneShot
236
+ * @param false $oneShot
259
237
*
260
- * @return mixed|\Symfony\Contracts\HttpClient \ResponseInterface
238
+ * @return mixed|\Psr\Http\Message \ResponseInterface
261
239
*
262
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
263
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
264
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
265
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
240
+ * @throws GuzzleException
241
+ * @throws ResourceNotFound
266
242
*/
267
243
public function getContainerStats ($ id , $ oneShot = false )
268
244
{
269
245
try {
270
246
return $ this ->request ('GET ' , sprintf ('/containers/%s/stats?stream=false&one-shot=%s ' , $ id , $ oneShot ));
271
- } catch (ClientException $ e ) {
247
+ } catch (GuzzleException $ e ) {
272
248
if ($ e ->getCode () === 404 ) {
273
249
$ text = sprintf ('No such container: %s ' , $ id );
274
- throw new ResourceNotFound ($ text , 404 , $ e );
250
+ throw new ResourceNotFound ($ text , 404 );
275
251
}
276
252
277
253
throw $ e ;
@@ -282,12 +258,10 @@ public function getContainerStats($id, $oneShot = false)
282
258
* @param $id
283
259
* @param string $level
284
260
*
285
- * @return string
261
+ * @return string|string[]|null
286
262
*
287
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
288
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
289
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
290
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
263
+ * @throws GuzzleException
264
+ * @throws ResourceNotFound
291
265
*/
292
266
public function getContainerLogs ($ id , $ level = 'all ' )
293
267
{
@@ -312,42 +286,42 @@ public function getContainerLogs($id, $level = 'all')
312
286
$ text = preg_replace ('/(?!\n)[\p{Cc}]/ ' , '' , $ text );
313
287
314
288
return $ text ;
315
- } catch (ClientException $ e ) {
289
+ } catch (GuzzleException $ e ) {
316
290
if ($ e ->getCode () === 404 ) {
317
291
$ text = sprintf ('No such container: %s ' , $ id );
318
- throw new ResourceNotFound ($ text , 404 , $ e );
292
+ throw new ResourceNotFound ($ text , 404 );
319
293
}
320
294
321
295
throw $ e ;
322
296
}
323
297
}
324
298
325
299
/**
326
- * @param string $id
300
+ * @param $id
327
301
*
328
302
* @return bool
329
303
*
330
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
331
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
332
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
333
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
304
+ * @throws BadParameterException
305
+ * @throws GuzzleException
306
+ * @throws ResourceBusyException
307
+ * @throws ResourceNotFound
334
308
*/
335
309
public function deleteContainer ($ id )
336
310
{
337
311
try {
338
312
$ response = $ this ->request ('DELETE ' , sprintf ('/containers/%s ' , $ id ), [], false );
339
313
340
314
return $ response ->getStatusCode () === 204 ;
341
- } catch (ClientException $ e ) {
315
+ } catch (GuzzleException $ e ) {
342
316
$ code = $ e ->getCode ();
343
317
if ($ code === 400 ) {
344
- throw new BadParameterException ($ e ->getMessage (), 400 , $ e );
318
+ throw new BadParameterException ($ e ->getMessage (), 400 );
345
319
} else if ($ code === 404 ) {
346
320
$ text = sprintf ('No such container: %s ' , $ id );
347
- throw new ResourceNotFound ($ text , 404 , $ e );
321
+ throw new ResourceNotFound ($ text , 404 );
348
322
} else if ($ code === 409 ) {
349
323
$ text = sprintf ('You cannot remove a running container: %s. Stop the container before attempting removal or force remove ' , $ id );
350
- throw new ResourceBusyException ($ text , 409 , $ e );
324
+ throw new ResourceBusyException ($ text , 409 );
351
325
}
352
326
353
327
throw $ e ;
@@ -359,10 +333,7 @@ public function deleteContainer($id)
359
333
*
360
334
* @return array
361
335
*
362
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
363
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
364
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
365
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
336
+ * @throws GuzzleException
366
337
*/
367
338
public function pruneContainers ($ label = null )
368
339
{
@@ -381,6 +352,13 @@ public function pruneContainers($label = null)
381
352
382
353
// DOCKER IMAGES
383
354
355
+ /**
356
+ * @param $name
357
+ *
358
+ * @return bool
359
+ *
360
+ * @throws GuzzleException
361
+ */
384
362
public function imageExists ($ name )
385
363
{
386
364
try {
@@ -392,6 +370,13 @@ public function imageExists($name)
392
370
return false ;
393
371
}
394
372
373
+ /**
374
+ * @param null $label
375
+ *
376
+ * @return mixed|\Psr\Http\Message\ResponseInterface
377
+ *
378
+ * @throws GuzzleException
379
+ */
395
380
public function listImages ($ label = null )
396
381
{
397
382
$ endpoint = '/images/json ' ;
@@ -406,14 +391,22 @@ public function listImages($label = null)
406
391
return $ this ->request ('GET ' , $ endpoint );
407
392
}
408
393
394
+ /**
395
+ * @param $nameOrId
396
+ *
397
+ * @return mixed|\Psr\Http\Message\ResponseInterface
398
+ *
399
+ * @throws GuzzleException
400
+ * @throws ResourceNotFound
401
+ */
409
402
public function inspectImage ($ nameOrId )
410
403
{
411
404
try {
412
405
return $ this ->request ('GET ' , sprintf ('/images/%s/json ' , $ nameOrId ));
413
- } catch (ClientException $ e ) {
406
+ } catch (GuzzleException $ e ) {
414
407
if ($ e ->getCode () === 404 ) {
415
408
$ text = sprintf ('No such image: %s ' , $ nameOrId );
416
- throw new ResourceNotFound ($ text , 404 , $ e );
409
+ throw new ResourceNotFound ($ text , 404 );
417
410
}
418
411
419
412
throw $ e ;
0 commit comments