Skip to content

Commit c1b4d96

Browse files
committed
Revert back to socket-client, add directly used deps, run php-cs-fixer
Signed-off-by: Grant Millar <rid@cylo.io>
1 parent 6c0fcd4 commit c1b4d96

23 files changed

Lines changed: 142 additions & 150 deletions

composer.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,15 @@
3131
],
3232
"require": {
3333
"php": ">=8.1",
34-
"beluga-php/docker-php-api": "7.1.43.x-dev",
35-
"psr/http-client": "^1.0",
36-
"psr/http-client-implementation": "*",
37-
"psr/http-message": "^1.1 || ^2.0",
38-
"psr/http-message-implementation": "*",
39-
"symfony/filesystem": "^6.1",
40-
"symfony/process": "^6.1"
41-
},
42-
"suggest": {
43-
"guzzlehttp/guzzle": "PSR-18 compliant HTTP Client to use the API"
34+
"beluga-php/docker-php-api": "7.1.43.0",
35+
"nyholm/psr7": "^1.8",
36+
"php-http/client-common": "^2.7",
37+
"php-http/discovery": "^1.19",
38+
"plesk/socket-client": "^2.1",
39+
"psr/http-message": "^2.0",
40+
"symfony/filesystem": "^6.3",
41+
"symfony/process": "^6.3",
42+
"symfony/serializer": "^6.3"
4443
},
4544
"require-dev": {
4645
"friendsofphp/php-cs-fixer": "^3.8",

src/Context/Context.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function setDirectory($directory): void
8686
*/
8787
public function getDockerfileContent()
8888
{
89-
return \file_get_contents($this->directory.\DIRECTORY_SEPARATOR.'Dockerfile');
89+
return file_get_contents($this->directory.\DIRECTORY_SEPARATOR.'Dockerfile');
9090
}
9191

9292
/**
@@ -132,7 +132,7 @@ public function toTar()
132132
public function toStream()
133133
{
134134
if (!\is_resource($this->process)) {
135-
$this->process = \proc_open('/usr/bin/env tar -c .', [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes, $this->directory);
135+
$this->process = proc_open('/usr/bin/env tar -c .', [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes, $this->directory);
136136
$this->stream = $pipes[1];
137137
}
138138

@@ -142,11 +142,11 @@ public function toStream()
142142
public function __destruct()
143143
{
144144
if (\is_resource($this->stream)) {
145-
\fclose($this->stream);
145+
fclose($this->stream);
146146
}
147147

148148
if (\is_resource($this->process)) {
149-
\proc_close($this->process);
149+
proc_close($this->process);
150150
}
151151

152152
if ($this->cleanup) {

src/Context/ContextBuilder.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public function volume($volume)
255255
*/
256256
public function getContext()
257257
{
258-
$directory = \sys_get_temp_dir().'/ctb-'.\microtime();
258+
$directory = sys_get_temp_dir().'/ctb-'.microtime();
259259
$this->fs->mkdir($directory);
260260
$this->write($directory);
261261

@@ -325,7 +325,7 @@ private function write($directory): void
325325
$dockerfile[] = 'CMD '.$this->command;
326326
}
327327

328-
$this->fs->dumpFile($directory.\DIRECTORY_SEPARATOR.'Dockerfile', \implode(PHP_EOL, $dockerfile));
328+
$this->fs->dumpFile($directory.\DIRECTORY_SEPARATOR.'Dockerfile', implode(\PHP_EOL, $dockerfile));
329329
}
330330

331331
/**
@@ -338,12 +338,12 @@ private function write($directory): void
338338
*/
339339
private function getFile($directory, $content)
340340
{
341-
$hash = \md5($content);
341+
$hash = md5($content);
342342

343343
if (!\array_key_exists($hash, $this->files)) {
344-
$file = \tempnam($directory, '');
344+
$file = tempnam($directory, '');
345345
$this->fs->dumpFile($file, $content);
346-
$this->files[$hash] = \basename($file);
346+
$this->files[$hash] = basename($file);
347347
}
348348

349349
return $this->files[$hash];
@@ -359,14 +359,14 @@ private function getFile($directory, $content)
359359
*/
360360
private function getFileFromStream($directory, $stream)
361361
{
362-
$file = \tempnam($directory, '');
363-
$target = \fopen($file, 'w');
364-
if (0 === \stream_copy_to_stream($stream, $target)) {
362+
$file = tempnam($directory, '');
363+
$target = fopen($file, 'w');
364+
if (0 === stream_copy_to_stream($stream, $target)) {
365365
throw new \RuntimeException('Failed to write stream to file');
366366
}
367-
\fclose($target);
367+
fclose($target);
368368

369-
return \basename($file);
369+
return basename($file);
370370
}
371371

372372
/**
@@ -379,10 +379,10 @@ private function getFileFromStream($directory, $stream)
379379
*/
380380
private function getFileFromDisk($directory, $source)
381381
{
382-
$hash = 'DISK-'.\md5(\realpath($source));
382+
$hash = 'DISK-'.md5(realpath($source));
383383
if (!\array_key_exists($hash, $this->files)) {
384384
// Check if source is a directory or a file.
385-
if (\is_dir($source)) {
385+
if (is_dir($source)) {
386386
$this->fs->mirror($source, $directory.'/'.$hash, null, ['copy_on_windows' => true]);
387387
} else {
388388
$this->fs->copy($source, $directory.'/'.$hash);

src/Docker.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
*/
1414
class Docker extends Client
1515
{
16-
/**
17-
* {@inheritdoc}
18-
*/
1916
public function imagePush(string $name, array $queryParameters = [], array $headerParameters = [], string $fetch = self::FETCH_OBJECT, array $accept = [])
2017
{
2118
if (isset($headerParameters['X-Registry-Auth']) && $headerParameters['X-Registry-Auth'] instanceof AuthConfig) {
22-
$headerParameters['X-Registry-Auth'] = \base64_encode($this->serializer->serialize($headerParameters['X-Registry-Auth'], 'json'));
19+
$headerParameters['X-Registry-Auth'] = base64_encode($this->serializer->serialize($headerParameters['X-Registry-Auth'], 'json'));
2320
}
2421

2522
return $this->executeEndpoint(new ImagePush($name, $queryParameters, $headerParameters, $accept), $fetch);

src/DockerClientFactory.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@
99
use Http\Client\Common\Plugin\ContentLengthPlugin;
1010
use Http\Client\Common\Plugin\DecoderPlugin;
1111
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
12+
use Http\Client\Common\PluginClient;
1213
use Http\Client\Common\PluginClientFactory;
1314
use Http\Client\Socket\Client;
14-
use Http\Discovery\UriFactoryDiscovery;
15-
use Psr\Http\Client\ClientInterface;
15+
use Http\Discovery\Psr17FactoryDiscovery;
1616

1717
final class DockerClientFactory
1818
{
19-
public static function create(array $config = [], PluginClientFactory $pluginClientFactory = null): ClientInterface
19+
public static function create(array $config = [], PluginClientFactory $pluginClientFactory = null): PluginClient
2020
{
2121
if (!\array_key_exists('remote_socket', $config)) {
2222
$config['remote_socket'] = 'unix:///var/run/docker.sock';
2323
}
2424

2525
$socketClient = new Client($config);
2626

27-
$uriFactory = UriFactoryDiscovery::find();
28-
$host = \preg_match('/unix:\/\//', $config['remote_socket']) ? 'http://localhost' : $config['remote_socket'];
27+
$uriFactory = Psr17FactoryDiscovery::findUriFactory();
28+
$host = preg_match('/unix:\/\//', $config['remote_socket']) ? 'http://localhost' : $config['remote_socket'];
2929

30-
$pluginClientFactory = $pluginClientFactory ?? new PluginClientFactory();
30+
$pluginClientFactory ??= new PluginClientFactory();
3131

3232
return $pluginClientFactory->createClient(
3333
$socketClient,
@@ -37,7 +37,7 @@ public static function create(array $config = [], PluginClientFactory $pluginCli
3737
new AddPathPlugin($uriFactory->createUri('/v1.41')),
3838
new AddHostPlugin($uriFactory->createUri($host)),
3939
new HeaderDefaultsPlugin([
40-
'host' => \parse_url($host, \PHP_URL_HOST),
40+
'host' => parse_url($host, \PHP_URL_HOST),
4141
]),
4242
],
4343
[
@@ -46,29 +46,29 @@ public static function create(array $config = [], PluginClientFactory $pluginCli
4646
);
4747
}
4848

49-
public static function createFromEnv(PluginClientFactory $pluginClientFactory = null): ClientInterface
49+
public static function createFromEnv(PluginClientFactory $pluginClientFactory = null): PluginClient
5050
{
5151
$options = [
52-
'remote_socket' => \getenv('DOCKER_HOST') ? \getenv('DOCKER_HOST') : 'unix:///var/run/docker.sock',
52+
'remote_socket' => getenv('DOCKER_HOST') ? getenv('DOCKER_HOST') : 'unix:///var/run/docker.sock',
5353
];
5454

55-
if (\getenv('DOCKER_TLS_VERIFY') && '1' === \getenv('DOCKER_TLS_VERIFY')) {
56-
if (!\getenv('DOCKER_CERT_PATH')) {
55+
if (getenv('DOCKER_TLS_VERIFY') && '1' === getenv('DOCKER_TLS_VERIFY')) {
56+
if (!getenv('DOCKER_CERT_PATH')) {
5757
throw new \RuntimeException('Connection to docker has been set to use TLS, but no PATH is defined for certificate in DOCKER_CERT_PATH docker environment variable');
5858
}
5959

60-
$cafile = \getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'ca.pem';
61-
$certfile = \getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'cert.pem';
62-
$keyfile = \getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'key.pem';
60+
$cafile = getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'ca.pem';
61+
$certfile = getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'cert.pem';
62+
$keyfile = getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'key.pem';
6363

6464
$stream_context = [
6565
'cafile' => $cafile,
6666
'local_cert' => $certfile,
6767
'local_pk' => $keyfile,
6868
];
6969

70-
if (\getenv('DOCKER_PEER_NAME')) {
71-
$stream_context['peer_name'] = \getenv('DOCKER_PEER_NAME');
70+
if (getenv('DOCKER_PEER_NAME')) {
71+
$stream_context['peer_name'] = getenv('DOCKER_PEER_NAME');
7272
}
7373

7474
$options['ssl'] = true;

src/Endpoint/ContainerAttach.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class ContainerAttach extends BaseEndpoint
1313
{
14-
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
14+
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
1515
{
1616
if (200 === $response->getStatusCode() && DockerRawStream::HEADER === $contentType) {
1717
return new DockerRawStream($response->getBody());

src/Endpoint/ContainerAttachWebsocket.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ class ContainerAttachWebsocket extends BaseEndpoint
1414
{
1515
public function getExtraHeaders(): array
1616
{
17-
return \array_merge(
17+
return array_merge(
1818
parent::getExtraHeaders(),
1919
[
2020
'Host' => 'localhost',
2121
'Origin' => 'php://docker-php',
2222
'Upgrade' => 'websocket',
2323
'Connection' => 'Upgrade',
2424
'Sec-WebSocket-Version' => '13',
25-
'Sec-WebSocket-Key' => \base64_encode(\uniqid()),
25+
'Sec-WebSocket-Key' => base64_encode(uniqid()),
2626
]
2727
);
2828
}
2929

30-
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
30+
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
3131
{
3232
if (200 === $response->getStatusCode() && DockerRawStream::HEADER === $contentType) {
3333
return new AttachWebsocketStream($response->getBody());

src/Endpoint/ContainerLogs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class ContainerLogs extends BaseEndpoint
1313
{
14-
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
14+
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
1515
{
1616
if (200 === $response->getStatusCode() && DockerRawStream::HEADER === $contentType) {
1717
return new DockerRawStream($response->getBody());

src/Endpoint/ExecStart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class ExecStart extends BaseEndpoint
1313
{
14-
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
14+
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
1515
{
1616
if (200 === $response->getStatusCode() && DockerRawStream::HEADER === $contentType) {
1717
return new DockerRawStream($response->getBody());

src/Endpoint/ImageBuild.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getBody(SerializerInterface $serializer, $streamFactory = null):
2424
return [['Content-Type' => ['application/octet-stream']], $body];
2525
}
2626

27-
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
27+
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
2828
{
2929
if (200 === $response->getStatusCode()) {
3030
return new BuildStream($response->getBody(), $serializer);

0 commit comments

Comments
 (0)