1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Docker\Manager; |
4
|
|
|
|
5
|
|
|
use Docker\API\Model\AuthConfig; |
6
|
|
|
use Docker\API\Model\BuildInfo; |
7
|
|
|
use Docker\API\Model\CreateImageInfo; |
8
|
|
|
use Docker\API\Model\PushImageInfo; |
9
|
|
|
use Docker\API\Resource\ImageResource; |
10
|
|
|
use Docker\Stream\BuildStream; |
11
|
|
|
use Docker\Stream\CreateImageStream; |
12
|
|
|
use Docker\Stream\PushStream; |
13
|
|
|
use Joli\Jane\Swagger\Client\QueryParam; |
14
|
|
|
|
15
|
|
|
class ImageManager extends ImageResource |
16
|
|
|
{ |
17
|
|
|
const FETCH_STREAM = 'stream'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
* |
22
|
|
|
* @return \Psr\Http\Message\ResponseInterface|BuildInfo[]|BuildStream |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
public function build($inputStream, $parameters = [], $fetch = self::FETCH_OBJECT) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$response = parent::build($inputStream, $parameters, self::FETCH_RESPONSE); |
27
|
|
|
|
28
|
|
|
if (200 === $response->getStatusCode()) { |
29
|
|
|
if (self::FETCH_STREAM === $fetch) { |
30
|
|
|
return new BuildStream($response->getBody(), $this->serializer); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (self::FETCH_OBJECT === $fetch) { |
34
|
|
|
$buildInfoList = []; |
35
|
|
|
|
36
|
|
|
$stream = new BuildStream($response->getBody(), $this->serializer); |
37
|
|
|
$stream->onFrame(function (BuildInfo $buildInfo) use (&$buildInfoList) { |
38
|
|
|
$buildInfoList[] = $buildInfo; |
39
|
|
|
}); |
40
|
|
|
$stream->wait(); |
41
|
|
|
|
42
|
|
|
return $buildInfoList; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $response; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
* |
52
|
|
|
* @return \Psr\Http\Message\ResponseInterface|CreateImageInfo[]|CreateImageStream |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function create($parameters = [], $fetch = self::FETCH_OBJECT) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$response = parent::create($parameters, self::FETCH_RESPONSE); |
57
|
|
|
|
58
|
|
|
if (200 === $response->getStatusCode()) { |
59
|
|
|
if (self::FETCH_STREAM === $fetch) { |
60
|
|
|
return new CreateImageStream($response->getBody(), $this->serializer); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (self::FETCH_OBJECT === $fetch) { |
64
|
|
|
$createImageInfoList = []; |
65
|
|
|
|
66
|
|
|
$stream = new CreateImageStream($response->getBody(), $this->serializer); |
67
|
|
|
$stream->onFrame(function (CreateImageInfo $createImageInfo) use (&$createImageInfoList) { |
68
|
|
|
$createImageInfoList[] = $createImageInfo; |
69
|
|
|
}); |
70
|
|
|
$stream->wait(); |
71
|
|
|
|
72
|
|
|
return $createImageInfoList; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $response; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
* |
82
|
|
|
* @return \Psr\Http\Message\ResponseInterface|PushImageInfo[]|CreateImageStream |
83
|
|
|
*/ |
84
|
|
|
public function push($name, $parameters = [], $fetch = self::FETCH_OBJECT) |
85
|
|
|
{ |
86
|
|
|
if (isset($parameters['X-Registry-Auth']) && $parameters['X-Registry-Auth'] instanceof AuthConfig) { |
87
|
|
|
$parameters['X-Registry-Auth'] = base64_encode($this->serializer->serialize($parameters['X-Registry-Auth'], 'json')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$queryParam = new QueryParam(); |
91
|
|
|
$queryParam->setDefault('tag', null); |
92
|
|
|
$queryParam->setDefault('X-Registry-Auth', null); |
93
|
|
|
$queryParam->setHeaderParameters(['X-Registry-Auth']); |
94
|
|
|
|
95
|
|
|
$url = 'http://localhost/images/{name}/push'; |
96
|
|
|
$url = str_replace('{name}', $name, $url); |
97
|
|
|
$url = $url . ('?' . $queryParam->buildQueryString($parameters)); |
98
|
|
|
|
99
|
|
|
$headers = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters)); |
100
|
|
|
|
101
|
|
|
$body = $queryParam->buildFormDataString($parameters); |
102
|
|
|
|
103
|
|
|
$request = $this->messageFactory->createRequest('POST', $url, $headers, $body); |
104
|
|
|
$response = $this->httpClient->sendRequest($request); |
105
|
|
|
|
106
|
|
|
if (200 === $response->getStatusCode()) { |
107
|
|
|
if (self::FETCH_STREAM === $fetch) { |
108
|
|
|
return new PushStream($response->getBody(), $this->serializer); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (self::FETCH_OBJECT === $fetch) { |
112
|
|
|
$pushImageInfoList = []; |
113
|
|
|
|
114
|
|
|
$stream = new PushStream($response->getBody(), $this->serializer); |
115
|
|
|
$stream->onFrame(function (PushImageInfo $pushImageInfo) use (&$pushImageInfoList) { |
116
|
|
|
$pushImageInfoList[] = $pushImageInfo; |
117
|
|
|
}); |
118
|
|
|
$stream->wait(); |
119
|
|
|
|
120
|
|
|
return $pushImageInfoList; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $response; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.