Skip to content

Commit

Permalink
Revert back to socket-client, add directly used deps, run php-cs-fixer
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Millar <[email protected]>
  • Loading branch information
Rid committed Oct 18, 2023
1 parent 6c0fcd4 commit c1b4d96
Show file tree
Hide file tree
Showing 23 changed files with 142 additions and 150 deletions.
19 changes: 9 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@
],
"require": {
"php": ">=8.1",
"beluga-php/docker-php-api": "7.1.43.x-dev",
"psr/http-client": "^1.0",
"psr/http-client-implementation": "*",
"psr/http-message": "^1.1 || ^2.0",
"psr/http-message-implementation": "*",
"symfony/filesystem": "^6.1",
"symfony/process": "^6.1"
},
"suggest": {
"guzzlehttp/guzzle": "PSR-18 compliant HTTP Client to use the API"
"beluga-php/docker-php-api": "7.1.43.0",
"nyholm/psr7": "^1.8",
"php-http/client-common": "^2.7",
"php-http/discovery": "^1.19",
"plesk/socket-client": "^2.1",
"psr/http-message": "^2.0",
"symfony/filesystem": "^6.3",
"symfony/process": "^6.3",
"symfony/serializer": "^6.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.8",
Expand Down
8 changes: 4 additions & 4 deletions src/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function setDirectory($directory): void
*/
public function getDockerfileContent()
{
return \file_get_contents($this->directory.\DIRECTORY_SEPARATOR.'Dockerfile');
return file_get_contents($this->directory.\DIRECTORY_SEPARATOR.'Dockerfile');
}

/**
Expand Down Expand Up @@ -132,7 +132,7 @@ public function toTar()
public function toStream()
{
if (!\is_resource($this->process)) {
$this->process = \proc_open('/usr/bin/env tar -c .', [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes, $this->directory);
$this->process = proc_open('/usr/bin/env tar -c .', [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes, $this->directory);
$this->stream = $pipes[1];
}

Expand All @@ -142,11 +142,11 @@ public function toStream()
public function __destruct()
{
if (\is_resource($this->stream)) {
\fclose($this->stream);
fclose($this->stream);
}

if (\is_resource($this->process)) {
\proc_close($this->process);
proc_close($this->process);
}

if ($this->cleanup) {
Expand Down
24 changes: 12 additions & 12 deletions src/Context/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function volume($volume)
*/
public function getContext()
{
$directory = \sys_get_temp_dir().'/ctb-'.\microtime();
$directory = sys_get_temp_dir().'/ctb-'.microtime();
$this->fs->mkdir($directory);
$this->write($directory);

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

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

/**
Expand All @@ -338,12 +338,12 @@ private function write($directory): void
*/
private function getFile($directory, $content)
{
$hash = \md5($content);
$hash = md5($content);

if (!\array_key_exists($hash, $this->files)) {
$file = \tempnam($directory, '');
$file = tempnam($directory, '');
$this->fs->dumpFile($file, $content);
$this->files[$hash] = \basename($file);
$this->files[$hash] = basename($file);
}

return $this->files[$hash];
Expand All @@ -359,14 +359,14 @@ private function getFile($directory, $content)
*/
private function getFileFromStream($directory, $stream)
{
$file = \tempnam($directory, '');
$target = \fopen($file, 'w');
if (0 === \stream_copy_to_stream($stream, $target)) {
$file = tempnam($directory, '');
$target = fopen($file, 'w');
if (0 === stream_copy_to_stream($stream, $target)) {
throw new \RuntimeException('Failed to write stream to file');
}
\fclose($target);
fclose($target);

return \basename($file);
return basename($file);
}

/**
Expand All @@ -379,10 +379,10 @@ private function getFileFromStream($directory, $stream)
*/
private function getFileFromDisk($directory, $source)
{
$hash = 'DISK-'.\md5(\realpath($source));
$hash = 'DISK-'.md5(realpath($source));
if (!\array_key_exists($hash, $this->files)) {
// Check if source is a directory or a file.
if (\is_dir($source)) {
if (is_dir($source)) {
$this->fs->mirror($source, $directory.'/'.$hash, null, ['copy_on_windows' => true]);
} else {
$this->fs->copy($source, $directory.'/'.$hash);
Expand Down
5 changes: 1 addition & 4 deletions src/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
*/
class Docker extends Client
{
/**
* {@inheritdoc}
*/
public function imagePush(string $name, array $queryParameters = [], array $headerParameters = [], string $fetch = self::FETCH_OBJECT, array $accept = [])
{
if (isset($headerParameters['X-Registry-Auth']) && $headerParameters['X-Registry-Auth'] instanceof AuthConfig) {
$headerParameters['X-Registry-Auth'] = \base64_encode($this->serializer->serialize($headerParameters['X-Registry-Auth'], 'json'));
$headerParameters['X-Registry-Auth'] = base64_encode($this->serializer->serialize($headerParameters['X-Registry-Auth'], 'json'));
}

return $this->executeEndpoint(new ImagePush($name, $queryParameters, $headerParameters, $accept), $fetch);
Expand Down
32 changes: 16 additions & 16 deletions src/DockerClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
use Http\Client\Common\Plugin\ContentLengthPlugin;
use Http\Client\Common\Plugin\DecoderPlugin;
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\Common\PluginClientFactory;
use Http\Client\Socket\Client;
use Http\Discovery\UriFactoryDiscovery;
use Psr\Http\Client\ClientInterface;
use Http\Discovery\Psr17FactoryDiscovery;

final class DockerClientFactory
{
public static function create(array $config = [], PluginClientFactory $pluginClientFactory = null): ClientInterface
public static function create(array $config = [], PluginClientFactory $pluginClientFactory = null): PluginClient
{
if (!\array_key_exists('remote_socket', $config)) {
$config['remote_socket'] = 'unix:///var/run/docker.sock';
}

$socketClient = new Client($config);

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

$pluginClientFactory = $pluginClientFactory ?? new PluginClientFactory();
$pluginClientFactory ??= new PluginClientFactory();

return $pluginClientFactory->createClient(
$socketClient,
Expand All @@ -37,7 +37,7 @@ public static function create(array $config = [], PluginClientFactory $pluginCli
new AddPathPlugin($uriFactory->createUri('/v1.41')),
new AddHostPlugin($uriFactory->createUri($host)),
new HeaderDefaultsPlugin([
'host' => \parse_url($host, \PHP_URL_HOST),
'host' => parse_url($host, \PHP_URL_HOST),
]),
],
[
Expand All @@ -46,29 +46,29 @@ public static function create(array $config = [], PluginClientFactory $pluginCli
);
}

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

if (\getenv('DOCKER_TLS_VERIFY') && '1' === \getenv('DOCKER_TLS_VERIFY')) {
if (!\getenv('DOCKER_CERT_PATH')) {
if (getenv('DOCKER_TLS_VERIFY') && '1' === getenv('DOCKER_TLS_VERIFY')) {
if (!getenv('DOCKER_CERT_PATH')) {
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');
}

$cafile = \getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'ca.pem';
$certfile = \getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'cert.pem';
$keyfile = \getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'key.pem';
$cafile = getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'ca.pem';
$certfile = getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'cert.pem';
$keyfile = getenv('DOCKER_CERT_PATH').\DIRECTORY_SEPARATOR.'key.pem';

$stream_context = [
'cafile' => $cafile,
'local_cert' => $certfile,
'local_pk' => $keyfile,
];

if (\getenv('DOCKER_PEER_NAME')) {
$stream_context['peer_name'] = \getenv('DOCKER_PEER_NAME');
if (getenv('DOCKER_PEER_NAME')) {
$stream_context['peer_name'] = getenv('DOCKER_PEER_NAME');
}

$options['ssl'] = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoint/ContainerAttach.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class ContainerAttach extends BaseEndpoint
{
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
{
if (200 === $response->getStatusCode() && DockerRawStream::HEADER === $contentType) {
return new DockerRawStream($response->getBody());
Expand Down
6 changes: 3 additions & 3 deletions src/Endpoint/ContainerAttachWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ class ContainerAttachWebsocket extends BaseEndpoint
{
public function getExtraHeaders(): array
{
return \array_merge(
return array_merge(
parent::getExtraHeaders(),
[
'Host' => 'localhost',
'Origin' => 'php://docker-php',
'Upgrade' => 'websocket',
'Connection' => 'Upgrade',
'Sec-WebSocket-Version' => '13',
'Sec-WebSocket-Key' => \base64_encode(\uniqid()),
'Sec-WebSocket-Key' => base64_encode(uniqid()),
]
);
}

protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
{
if (200 === $response->getStatusCode() && DockerRawStream::HEADER === $contentType) {
return new AttachWebsocketStream($response->getBody());
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoint/ContainerLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class ContainerLogs extends BaseEndpoint
{
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
{
if (200 === $response->getStatusCode() && DockerRawStream::HEADER === $contentType) {
return new DockerRawStream($response->getBody());
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoint/ExecStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class ExecStart extends BaseEndpoint
{
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
{
if (200 === $response->getStatusCode() && DockerRawStream::HEADER === $contentType) {
return new DockerRawStream($response->getBody());
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoint/ImageBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getBody(SerializerInterface $serializer, $streamFactory = null):
return [['Content-Type' => ['application/octet-stream']], $body];
}

protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
{
if (200 === $response->getStatusCode()) {
return new BuildStream($response->getBody(), $serializer);
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoint/ImageCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class ImageCreate extends BaseEndpoint
{
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
{
if (200 === $response->getStatusCode()) {
return new CreateImageStream($response->getBody(), $serializer);
Expand Down
4 changes: 2 additions & 2 deletions src/Endpoint/ImagePush.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class ImagePush extends BaseEndpoint
{
public function getUri(): string
{
return \str_replace(['{name}'], [\urlencode($this->name)], '/images/{name}/push');
return str_replace(['{name}'], [urlencode($this->name)], '/images/{name}/push');
}

protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
{
if (200 === $response->getStatusCode()) {
return new PushStream($response->getBody(), $serializer);
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoint/SystemEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class SystemEvents extends BaseEndpoint
{
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null)
protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, string $contentType = null)
{
if (200 === $response->getStatusCode()) {
return new EventStream($response->getBody(), $serializer);
Expand Down
Loading

0 comments on commit c1b4d96

Please sign in to comment.