|
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 Docker\Stream\TarStream; |
|
14
|
|
|
use Joli\Jane\OpenApi\Client\QueryParam; |
|
15
|
|
|
use Psr\Http\Message\StreamInterface; |
|
16
|
|
|
|
|
17
|
|
|
class ImageManager extends ImageResource |
|
18
|
|
|
{ |
|
19
|
|
|
const FETCH_STREAM = 'stream'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
* |
|
24
|
|
|
* @param resource|StreamInterface|string $inputStream The input stream (encoded with tar) containing the Dockerfile |
|
25
|
|
|
* and other files for the image. |
|
26
|
|
|
* |
|
27
|
|
|
* @return \Psr\Http\Message\ResponseInterface|BuildInfo[]|BuildStream |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function build($inputStream, $parameters = [], $fetch = self::FETCH_OBJECT) |
|
30
|
|
|
{ |
|
31
|
4 |
|
if (is_resource($inputStream)) { |
|
32
|
4 |
|
$inputStream = new TarStream($inputStream); |
|
33
|
4 |
|
} |
|
34
|
|
|
|
|
35
|
4 |
|
$queryParam = new QueryParam(); |
|
36
|
4 |
|
$queryParam->setDefault('dockerfile', null); |
|
37
|
4 |
|
$queryParam->setDefault('t', null); |
|
38
|
4 |
|
$queryParam->setDefault('remote', null); |
|
39
|
4 |
|
$queryParam->setDefault('q', false); |
|
40
|
4 |
|
$queryParam->setDefault('nocache', false); |
|
41
|
4 |
|
$queryParam->setDefault('pull', null); |
|
42
|
4 |
|
$queryParam->setDefault('rm', true); |
|
43
|
4 |
|
$queryParam->setDefault('forcerm', false); |
|
44
|
4 |
|
$queryParam->setDefault('memory', null); |
|
45
|
4 |
|
$queryParam->setDefault('memswap', null); |
|
46
|
4 |
|
$queryParam->setDefault('cpushares', null); |
|
47
|
4 |
|
$queryParam->setDefault('cpusetcpus', null); |
|
48
|
4 |
|
$queryParam->setDefault('cpuperiod', null); |
|
49
|
4 |
|
$queryParam->setDefault('cpuquota', null); |
|
50
|
4 |
|
$queryParam->setDefault('buildargs', null); |
|
51
|
4 |
|
$queryParam->setDefault('Content-type', 'application/tar'); |
|
52
|
4 |
|
$queryParam->setHeaderParameters(['Content-type']); |
|
53
|
4 |
|
$queryParam->setDefault('X-Registry-Config', null); |
|
54
|
4 |
|
$queryParam->setHeaderParameters(['X-Registry-Config']); |
|
55
|
|
|
|
|
56
|
4 |
|
$url = '/build'; |
|
57
|
4 |
|
$url = $url . ('?' . $queryParam->buildQueryString($parameters)); |
|
58
|
|
|
|
|
59
|
4 |
|
$headers = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters)); |
|
60
|
|
|
|
|
61
|
4 |
|
$body = $inputStream; |
|
62
|
|
|
|
|
63
|
4 |
|
$request = $this->messageFactory->createRequest('POST', $url, $headers, $body); |
|
64
|
|
|
|
|
65
|
4 |
|
if ($inputStream instanceof StreamInterface) { |
|
66
|
4 |
|
$request = $request->withHeader('Transfer-Encoding', 'chunked'); |
|
67
|
4 |
|
} |
|
68
|
|
|
|
|
69
|
4 |
|
$response = $this->httpClient->sendRequest($request); |
|
70
|
|
|
|
|
71
|
4 |
|
if (200 === $response->getStatusCode()) { |
|
72
|
4 |
|
if (self::FETCH_STREAM === $fetch) { |
|
73
|
1 |
|
return new BuildStream($response->getBody(), $this->serializer); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
if (self::FETCH_OBJECT === $fetch) { |
|
77
|
3 |
|
$buildInfoList = []; |
|
78
|
|
|
|
|
79
|
3 |
|
$stream = new BuildStream($response->getBody(), $this->serializer); |
|
80
|
|
|
$stream->onFrame(function (BuildInfo $buildInfo) use (&$buildInfoList) { |
|
81
|
3 |
|
$buildInfoList[] = $buildInfo; |
|
82
|
3 |
|
}); |
|
83
|
3 |
|
$stream->wait(); |
|
84
|
|
|
|
|
85
|
3 |
|
return $buildInfoList; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $response; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
* |
|
95
|
|
|
* @return \Psr\Http\Message\ResponseInterface|CreateImageInfo[]|CreateImageStream |
|
96
|
|
|
*/ |
|
97
|
3 |
View Code Duplication |
public function create($parameters = [], $fetch = self::FETCH_OBJECT) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
3 |
|
$response = parent::create($parameters, self::FETCH_RESPONSE); |
|
100
|
|
|
|
|
101
|
3 |
|
if (200 === $response->getStatusCode()) { |
|
102
|
3 |
|
if (self::FETCH_STREAM === $fetch) { |
|
103
|
1 |
|
return new CreateImageStream($response->getBody(), $this->serializer); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
if (self::FETCH_OBJECT === $fetch) { |
|
107
|
2 |
|
$createImageInfoList = []; |
|
108
|
|
|
|
|
109
|
2 |
|
$stream = new CreateImageStream($response->getBody(), $this->serializer); |
|
110
|
|
|
$stream->onFrame(function (CreateImageInfo $createImageInfo) use (&$createImageInfoList) { |
|
111
|
2 |
|
$createImageInfoList[] = $createImageInfo; |
|
112
|
2 |
|
}); |
|
113
|
2 |
|
$stream->wait(); |
|
114
|
|
|
|
|
115
|
2 |
|
return $createImageInfoList; |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $response; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
* |
|
125
|
|
|
* @return \Psr\Http\Message\ResponseInterface|PushImageInfo[]|CreateImageStream |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function push($name, $parameters = [], $fetch = self::FETCH_OBJECT) |
|
128
|
|
|
{ |
|
129
|
2 |
|
if (isset($parameters['X-Registry-Auth']) && $parameters['X-Registry-Auth'] instanceof AuthConfig) { |
|
|
|
|
|
|
130
|
2 |
|
$parameters['X-Registry-Auth'] = base64_encode($this->serializer->serialize($parameters['X-Registry-Auth'], 'json')); |
|
131
|
2 |
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
$queryParam = new QueryParam(); |
|
134
|
2 |
|
$queryParam->setDefault('tag', null); |
|
135
|
2 |
|
$queryParam->setDefault('X-Registry-Auth', null); |
|
136
|
2 |
|
$queryParam->setHeaderParameters(['X-Registry-Auth']); |
|
137
|
|
|
|
|
138
|
2 |
|
$url = 'http://localhost/images/{name}/push'; |
|
139
|
2 |
|
$url = str_replace('{name}', $name, $url); |
|
140
|
2 |
|
$url = $url . ('?' . $queryParam->buildQueryString($parameters)); |
|
141
|
|
|
|
|
142
|
2 |
|
$headers = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters)); |
|
143
|
|
|
|
|
144
|
2 |
|
$body = $queryParam->buildFormDataString($parameters); |
|
145
|
|
|
|
|
146
|
2 |
|
$request = $this->messageFactory->createRequest('POST', $url, $headers, $body); |
|
147
|
2 |
|
$response = $this->httpClient->sendRequest($request); |
|
148
|
|
|
|
|
149
|
2 |
|
if (200 === $response->getStatusCode()) { |
|
150
|
2 |
|
if (self::FETCH_STREAM === $fetch) { |
|
151
|
1 |
|
return new PushStream($response->getBody(), $this->serializer); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
if (self::FETCH_OBJECT === $fetch) { |
|
155
|
1 |
|
$pushImageInfoList = []; |
|
156
|
|
|
|
|
157
|
1 |
|
$stream = new PushStream($response->getBody(), $this->serializer); |
|
158
|
1 |
|
$stream->onFrame(function (PushImageInfo $pushImageInfo) use (&$pushImageInfoList) { |
|
159
|
1 |
|
$pushImageInfoList[] = $pushImageInfo; |
|
160
|
1 |
|
}); |
|
161
|
1 |
|
$stream->wait(); |
|
162
|
|
|
|
|
163
|
1 |
|
return $pushImageInfoList; |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $response; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.