Skip to content

Commit 81583d8

Browse files
authored
Merge pull request #1 from ibragim64/develop
Changes for better compatibility
2 parents 60cec14 + af971a4 commit 81583d8

File tree

2 files changed

+76
-85
lines changed

2 files changed

+76
-85
lines changed

composer.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "polkovnik/php-docker-client",
33
"version": "1.41.8",
44
"description": "Docker APIs driver for PHP.",
5+
"homepage": "https://gtihub.com/ibragim64/php-docker-client",
56
"type": "library",
67
"license": "MIT",
78
"authors": [
@@ -18,9 +19,6 @@
1819
"require": {
1920
"ext-curl": "*",
2021
"ext-json": "*",
21-
"symfony/http-client": ">=3.0"
22-
},
23-
"require-dev": {
24-
"symfony/var-dumper": ">=3.0"
22+
"guzzlehttp/guzzle": ">=6.5.0"
2523
}
2624
}

src/DockerClient.php

Lines changed: 74 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
namespace Polkovnik\Component\DockerClient;
44

5+
use GuzzleHttp\Exception\GuzzleException;
56
use Polkovnik\Component\DockerClient\Exception\BadParameterException;
67
use Polkovnik\Component\DockerClient\Exception\DockerSocketNotFound;
78
use Polkovnik\Component\DockerClient\Exception\ResourceBusyException;
89
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;
1111

1212
class DockerClient
1313
{
14-
/** @var CurlHttpClient */
14+
/** @var HttpClient */
1515
private $http;
1616

1717
/** @var array */
@@ -38,7 +38,7 @@ public function __construct($options = [])
3838
$this->dockerApiEndpoint = $options['docker_base_uri'];
3939
}
4040

41-
$this->http = new CurlHttpClient([
41+
$this->http = new HttpClient([
4242
'base_uri' => $this->dockerApiEndpoint,
4343
]);
4444

@@ -55,7 +55,7 @@ private function testConnection()
5555
return $this->info();
5656
} catch (\Exception $e) {
5757
$search = 'failed binding local connection end';
58-
if (str_contains(strtolower($e->getMessage()), $search)) {
58+
if (strpos(strtolower($e->getMessage()), $search) !== false) {
5959
$text = sprintf('Could not bind to docker socket at %s', $this->unixSocket);
6060
throw new DockerSocketNotFound($text);
6161
}
@@ -70,51 +70,38 @@ private function testConnection()
7070
* @param array $options
7171
* @param bool $resolveResponse
7272
*
73-
* @return mixed|\Symfony\Contracts\HttpClient\ResponseInterface
73+
* @return mixed|\Psr\Http\Message\ResponseInterface
7474
*
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
7976
*/
8077
private function request($method, $url, $options = [], $resolveResponse = true)
8178
{
82-
$options = array_replace_recursive(['bindto' => $this->unixSocket], $options);
79+
$options = array_replace_recursive(['curl' => [CURLOPT_UNIX_SOCKET_PATH => $this->unixSocket]], $options);
8380
$response = $this->http->request($method, $url, $options);
8481
if ($resolveResponse) {
85-
return json_decode($response->getContent(), true);
82+
return json_decode($response->getBody()->getContents(), true);
8683
} else {
8784
return $response;
8885
}
8986
}
9087

9188
/**
92-
* Retrieves information about the Docker engine.
89+
* @return mixed|\Psr\Http\Message\ResponseInterface
9390
*
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
10092
*/
10193
public function info()
10294
{
10395
return $this->request('GET', '/info');
10496
}
10597

10698
/**
107-
* Returns a list of containers
108-
*
10999
* @param array $options
110100
*
111-
* @return array|null
101+
* @return mixed|\Psr\Http\Message\ResponseInterface
112102
*
113103
* @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
118105
*/
119106
public function listContainers($options = [])
120107
{
@@ -138,7 +125,7 @@ public function listContainers($options = [])
138125
$endpoint = urldecode($endpoint);
139126
try {
140127
return $this->request('GET', $endpoint);
141-
} catch (ClientException $e) {
128+
} catch (GuzzleException $e) {
142129
$code = $e->getCode();
143130
if ($code === 400) {
144131
throw new BadParameterException($e->getMessage(), $e->getCode());
@@ -153,10 +140,7 @@ public function listContainers($options = [])
153140
*
154141
* @return bool
155142
*
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
160144
* @throws ResourceNotFound
161145
*/
162146
public function stopContainer($id)
@@ -166,7 +150,7 @@ public function stopContainer($id)
166150
if ($response->getStatusCode() === 204) {
167151
return true;
168152
}
169-
} catch (ClientException $e) {
153+
} catch (GuzzleException $e) {
170154
$code = $e->getCode();
171155
if ($code === 404) {
172156
$text = sprintf('No such container: %s', $id);
@@ -182,21 +166,19 @@ public function stopContainer($id)
182166
/**
183167
* @param $id
184168
*
185-
* @return mixed
169+
* @return mixed|\Psr\Http\Message\ResponseInterface
186170
*
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
191173
*/
192174
public function startContainer($id)
193175
{
194176
try {
195177
return $this->request('POST', sprintf('/containers/%s/start', $id));
196-
} catch (ClientException $e) {
178+
} catch (GuzzleException $e) {
197179
if ($e->getCode() === 404) {
198180
$text = sprintf('No such container: %s', $id);
199-
throw new ResourceNotFound($text, $e->getCode(), $e);
181+
throw new ResourceNotFound($text, $e->getCode());
200182
}
201183

202184
throw $e;
@@ -207,12 +189,10 @@ public function startContainer($id)
207189
* @param $name
208190
* @param $payload
209191
*
210-
* @return string|false
192+
* @return false|mixed
211193
*
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
216196
*/
217197
public function runContainer($name, $payload)
218198
{
@@ -232,21 +212,19 @@ public function runContainer($name, $payload)
232212
/**
233213
* @param $id
234214
*
235-
* @return array
215+
* @return mixed|\Psr\Http\Message\ResponseInterface
236216
*
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
241219
*/
242220
public function inspectContainer($id)
243221
{
244222
try {
245223
return $this->request('GET', sprintf('/containers/%s/json', $id));
246-
} catch (ClientException $e) {
224+
} catch (GuzzleException $e) {
247225
if ($e->getCode() === 404) {
248226
$text = sprintf('No such container: %s', $id);
249-
throw new ResourceNotFound($text, 404, $e);
227+
throw new ResourceNotFound($text, 404);
250228
}
251229

252230
throw $e;
@@ -255,23 +233,21 @@ public function inspectContainer($id)
255233

256234
/**
257235
* @param $id
258-
* @param bool $oneShot
236+
* @param false $oneShot
259237
*
260-
* @return mixed|\Symfony\Contracts\HttpClient\ResponseInterface
238+
* @return mixed|\Psr\Http\Message\ResponseInterface
261239
*
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
266242
*/
267243
public function getContainerStats($id, $oneShot = false)
268244
{
269245
try {
270246
return $this->request('GET', sprintf('/containers/%s/stats?stream=false&one-shot=%s', $id, $oneShot));
271-
} catch (ClientException $e) {
247+
} catch (GuzzleException $e) {
272248
if ($e->getCode() === 404) {
273249
$text = sprintf('No such container: %s', $id);
274-
throw new ResourceNotFound($text, 404, $e);
250+
throw new ResourceNotFound($text, 404);
275251
}
276252

277253
throw $e;
@@ -282,12 +258,10 @@ public function getContainerStats($id, $oneShot = false)
282258
* @param $id
283259
* @param string $level
284260
*
285-
* @return string
261+
* @return string|string[]|null
286262
*
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
291265
*/
292266
public function getContainerLogs($id, $level = 'all')
293267
{
@@ -312,42 +286,42 @@ public function getContainerLogs($id, $level = 'all')
312286
$text = preg_replace('/(?!\n)[\p{Cc}]/', '', $text);
313287

314288
return $text;
315-
} catch (ClientException $e) {
289+
} catch (GuzzleException $e) {
316290
if ($e->getCode() === 404) {
317291
$text = sprintf('No such container: %s', $id);
318-
throw new ResourceNotFound($text, 404, $e);
292+
throw new ResourceNotFound($text, 404);
319293
}
320294

321295
throw $e;
322296
}
323297
}
324298

325299
/**
326-
* @param string $id
300+
* @param $id
327301
*
328302
* @return bool
329303
*
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
334308
*/
335309
public function deleteContainer($id)
336310
{
337311
try {
338312
$response = $this->request('DELETE', sprintf('/containers/%s', $id), [], false);
339313

340314
return $response->getStatusCode() === 204;
341-
} catch (ClientException $e) {
315+
} catch (GuzzleException $e) {
342316
$code = $e->getCode();
343317
if ($code === 400) {
344-
throw new BadParameterException($e->getMessage(), 400, $e);
318+
throw new BadParameterException($e->getMessage(), 400);
345319
} else if ($code === 404) {
346320
$text = sprintf('No such container: %s', $id);
347-
throw new ResourceNotFound($text, 404, $e);
321+
throw new ResourceNotFound($text, 404);
348322
} else if ($code === 409) {
349323
$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);
351325
}
352326

353327
throw $e;
@@ -359,10 +333,7 @@ public function deleteContainer($id)
359333
*
360334
* @return array
361335
*
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
366337
*/
367338
public function pruneContainers($label = null)
368339
{
@@ -381,6 +352,13 @@ public function pruneContainers($label = null)
381352

382353
// DOCKER IMAGES
383354

355+
/**
356+
* @param $name
357+
*
358+
* @return bool
359+
*
360+
* @throws GuzzleException
361+
*/
384362
public function imageExists($name)
385363
{
386364
try {
@@ -392,6 +370,13 @@ public function imageExists($name)
392370
return false;
393371
}
394372

373+
/**
374+
* @param null $label
375+
*
376+
* @return mixed|\Psr\Http\Message\ResponseInterface
377+
*
378+
* @throws GuzzleException
379+
*/
395380
public function listImages($label = null)
396381
{
397382
$endpoint = '/images/json';
@@ -406,14 +391,22 @@ public function listImages($label = null)
406391
return $this->request('GET', $endpoint);
407392
}
408393

394+
/**
395+
* @param $nameOrId
396+
*
397+
* @return mixed|\Psr\Http\Message\ResponseInterface
398+
*
399+
* @throws GuzzleException
400+
* @throws ResourceNotFound
401+
*/
409402
public function inspectImage($nameOrId)
410403
{
411404
try {
412405
return $this->request('GET', sprintf('/images/%s/json', $nameOrId));
413-
} catch (ClientException $e) {
406+
} catch (GuzzleException $e) {
414407
if ($e->getCode() === 404) {
415408
$text = sprintf('No such image: %s', $nameOrId);
416-
throw new ResourceNotFound($text, 404, $e);
409+
throw new ResourceNotFound($text, 404);
417410
}
418411

419412
throw $e;

0 commit comments

Comments
 (0)