From 6b145753efd267e7c5e349d69b6c970723e6dfe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rube=CC=81n=20Robles?= Date: Sat, 24 Feb 2024 00:52:03 +0100 Subject: [PATCH] wip --- composer.json | 9 +- phpstan.neon | 6 +- pint.json | 4 +- src/ApiClient.php | 58 +++++++----- src/Client.php | 2 +- src/Compose/Builder.php | 6 +- src/Compose/Service.php | 4 +- src/Compose/ServiceBuilder.php | 2 +- src/Compose/ServicePort.php | 2 +- src/Endpoints/Configs.php | 18 ++-- src/Endpoints/Containers.php | 88 +++++++++---------- src/Endpoints/Delegates/HandlesConfigs.php | 10 +-- src/Endpoints/Delegates/HandlesContainers.php | 44 +++++----- src/Endpoints/Delegates/HandlesImages.php | 30 +++---- src/Endpoints/Delegates/HandlesNetworks.php | 14 +-- src/Endpoints/Delegates/HandlesNodes.php | 8 +- src/Endpoints/Delegates/HandlesSecrets.php | 10 +-- src/Endpoints/Delegates/HandlesServices.php | 12 +-- src/Endpoints/Delegates/HandlesSwarm.php | 14 +-- src/Endpoints/Delegates/HandlesSystem.php | 10 +-- src/Endpoints/Delegates/HandlesTasks.php | 6 +- src/Endpoints/Delegates/HandlesVolumes.php | 12 +-- src/Endpoints/Images.php | 54 ++++++------ src/Endpoints/Networks.php | 26 +++--- src/Endpoints/Nodes.php | 14 +-- src/Endpoints/Secrets.php | 18 ++-- src/Endpoints/Services.php | 22 ++--- src/Endpoints/Swarm.php | 27 +++--- src/Endpoints/System.php | 14 +-- src/Endpoints/Tasks.php | 10 +-- src/Endpoints/Volumes.php | 22 ++--- src/Exceptions/DockerApiException.php | 4 +- .../UnacceptedResponseFormatting.php | 4 +- src/Queries/Swarm/SwarmLeaveQuery.php | 18 ++++ src/RequestQuery.php | 2 +- src/Serialize/DockerStream.php | 38 ++++---- 36 files changed, 336 insertions(+), 306 deletions(-) create mode 100644 src/Queries/Swarm/SwarmLeaveQuery.php diff --git a/composer.json b/composer.json index 672f8cc..b1527dd 100644 --- a/composer.json +++ b/composer.json @@ -38,8 +38,8 @@ "require-dev": { "laravel/pint": "^1.13", "guzzlehttp/guzzle": "^7.1", - "phpstan/phpstan": "^1.0", - "phpunit/phpunit": "^10.0" + "pestphp/pest": "^2.34", + "phpstan/phpstan": "^1.0" }, "minimum-stability": "dev", "autoload": { @@ -55,7 +55,8 @@ "config": { "sort-packages": true, "allow-plugins": { - "php-http/discovery": true + "php-http/discovery": true, + "pestphp/pest-plugin": true } } -} \ No newline at end of file +} diff --git a/phpstan.neon b/phpstan.neon index c8a7d64..bf4a1d5 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,12 +1,8 @@ -includes: - - ./vendor/nunomaduro/larastan/extension.neon - parameters: paths: - src - # The level 8 is the highest level - level: 5 + level: 'max' checkMissingIterableValueType: false diff --git a/pint.json b/pint.json index 93061b6..58e569c 100644 --- a/pint.json +++ b/pint.json @@ -1,3 +1,3 @@ { - "preset": "laravel" -} + "preset": "per" +} \ No newline at end of file diff --git a/src/ApiClient.php b/src/ApiClient.php index 34a25ff..936f0a2 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -33,90 +33,106 @@ class ApiClient private HttpClient $client; + private RequestFactoryInterface $requestFactory; + + private StreamFactoryInterface $streamFactory; + public function __construct( private string $baseUrl, - private ?SocketClient $socket = null, - private ?RequestFactoryInterface $requestFactory = null, - private ?StreamFactoryInterface $streamFactory = null, - private ?PluginClientFactory $pluginFactory = null, + ?SocketClient $socket = null, + ?RequestFactoryInterface $requestFactory = null, + ?StreamFactoryInterface $streamFactory = null, + ?PluginClientFactory $pluginFactory = null, private array $headers = [] ) { - $this->requestFactory ??= Psr17FactoryDiscovery::findRequestFactory(); - $this->streamFactory ??= Psr17FactoryDiscovery::findStreamFactory(); + $requestFactory ??= Psr17FactoryDiscovery::findRequestFactory(); + $streamFactory ??= Psr17FactoryDiscovery::findStreamFactory(); + + $this->requestFactory = $requestFactory; + $this->streamFactory = $streamFactory; $socket ??= new SocketClient($this->requestFactory, [ 'remote_socket' => 'unix:///var/run/docker.sock', ]); - $this->pluginFactory ??= new PluginClientFactory; + $pluginFactory ??= new PluginClientFactory(); - $this->client = $this->pluginFactory->createClient($socket, [ + $this->client = $pluginFactory->createClient($socket, [ new ContentLengthPlugin(), new DecoderPlugin(), ]); } - public function get(string $path, array $params = []) + private function queryToString(array|RequestQuery $params = []): string + { + return build_http_query( + $params instanceof RequestQuery + ? $params->toArray() + : $params + ); + } + + public function get(string $path, array|RequestQuery $params = []): mixed { $request = $this->requestFactory->createRequest( 'GET', - $this->baseUrl.$path.build_http_query($params) + $this->baseUrl . $path . $this->queryToString($params) ); return $this->executeRequest($request); } - public function head(string $path) + public function head(string $path, array|RequestQuery $params = []): mixed { $request = $this->requestFactory->createRequest( 'HEAD', - $this->baseUrl.$path + $this->baseUrl . $path ); return $this->executeRequest($request); } - public function post(string $path, $body = null, array $params = [], string $contentType = null) + public function post(string $path, $body = null, array|RequestQuery $params = [], ?string $contentType = null): mixed { $this->contentType($contentType ?? static::JSON_CONTENT_TYPE); $request = $this->requestFactory->createRequest( 'POST', - $this->baseUrl.$path.build_http_query($params) + $this->baseUrl . $path . $this->queryToString($params) )->withBody($this->applySentBodyParsing($body)); return $this->executeRequest($request); } - public function put(string $path, $body = null, array $params = [], string $contentType = null) + public function put(string $path, $body = null, array|RequestQuery $params = [], ?string $contentType = null): mixed { $this->contentType($contentType ?? static::JSON_CONTENT_TYPE); $request = $this->requestFactory->createRequest( 'PUT', - $this->baseUrl.$path.build_http_query($params) + $this->baseUrl . $path . $this->queryToString($params) )->withBody($this->applySentBodyParsing($body)); return $this->executeRequest($request); } - public function patch(string $path, $body = null, array $params = [], string $contentType = null) + public function patch(string $path, $body = null, array|RequestQuery $params = [], ?string $contentType = null): mixed { $this->contentType($contentType ?? static::JSON_CONTENT_TYPE); $request = $this->requestFactory->createRequest( 'PATCH', - $this->baseUrl.$path.build_http_query($params) + $this->baseUrl . $path . $this->queryToString($params) )->withBody($this->applySentBodyParsing($body)); return $this->executeRequest($request); } - public function delete(string $path, array $params = []) + public function delete(string $path, array|RequestQuery $params = []): mixed { $request = $this->requestFactory->createRequest( 'DELETE', - $this->baseUrl.$path.build_http_query($params) + $this->baseUrl . $path . $this->queryToString($params) ); return $this->executeRequest($request); @@ -134,7 +150,7 @@ public function usingHeader(string $header, string $value): static return $this; } - private function executeRequest(RequestInterface $request) + private function executeRequest(RequestInterface $request): mixed { foreach ($this->headers as $header => $value) { $request = $request->withAddedHeader($header, $value); diff --git a/src/Client.php b/src/Client.php index de2f34f..3c27e8c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -54,7 +54,7 @@ public function __construct( StreamFactoryInterface $streamFactory = null ) { $this->apiClient = new ApiClient( - $url.'/'.static::DOCKER_API_VERSION, + $url . '/' . static::DOCKER_API_VERSION, $socketClient, $requestFactory, $streamFactory diff --git a/src/Compose/Builder.php b/src/Compose/Builder.php index 2528171..1d112b5 100644 --- a/src/Compose/Builder.php +++ b/src/Compose/Builder.php @@ -31,14 +31,14 @@ protected function loadFromFile(): void public function toFile(string $filePath = null): void { - // + // } public function toArray(): array { return [ 'version' => $this->version, - 'services' => array_map(fn (Service $service) => $service->toArray(), $this->services), + 'services' => array_map(fn(Service $service) => $service->toArray(), $this->services), ]; } @@ -49,7 +49,7 @@ public function toYaml(int $indentation = 2) public function newServiceBuilder(): ServiceBuilder { - return new ServiceBuilder; + return new ServiceBuilder(); } public function addService(string $id, Service $service): self diff --git a/src/Compose/Service.php b/src/Compose/Service.php index d67d9d6..264f874 100644 --- a/src/Compose/Service.php +++ b/src/Compose/Service.php @@ -18,7 +18,7 @@ public function __construct( public array|null $ports = [], public array|null $labels = [], ) { - // + // } public function toArray() @@ -27,7 +27,7 @@ public function toArray() 'name' => $this->name, 'image' => $this->image, 'build' => $this->build, - 'ports' => array_map(fn (ServicePort $port) => $port->mode ? $port->toArray() : (string) $port, $this->ports), + 'ports' => array_map(fn(ServicePort $port) => $port->mode ? $port->toArray() : (string) $port, $this->ports), 'labels' => $this->labels, ]); } diff --git a/src/Compose/ServiceBuilder.php b/src/Compose/ServiceBuilder.php index 47e345e..66b6820 100644 --- a/src/Compose/ServiceBuilder.php +++ b/src/Compose/ServiceBuilder.php @@ -7,7 +7,7 @@ class ServiceBuilder public function __construct( protected ?Service $service = null ) { - $this->service ??= new Service; + $this->service ??= new Service(); } public function fromArray(array $data): self diff --git a/src/Compose/ServicePort.php b/src/Compose/ServicePort.php index b64a380..0a599cb 100644 --- a/src/Compose/ServicePort.php +++ b/src/Compose/ServicePort.php @@ -13,7 +13,7 @@ public function __construct( public string|null $protocol = null, public string|null $mode = null ) { - // + // } public function getPublished(): int|string diff --git a/src/Endpoints/Configs.php b/src/Endpoints/Configs.php index cd03812..9ebf517 100644 --- a/src/Endpoints/Configs.php +++ b/src/Endpoints/Configs.php @@ -8,28 +8,28 @@ class Configs extends Endpoint { protected const PATH = '/configs'; - public function list() + public function list(): mixed { return $this->client->get(self::PATH); } - public function create() + public function create(): mixed { - return $this->client->post(self::PATH.'/create'); + return $this->client->post(self::PATH . '/create'); } - public function inspect(string $id) + public function inspect(string $id): mixed { - return $this->client->get(self::PATH."/{$id}"); + return $this->client->get(self::PATH . "/{$id}"); } - public function remove(string $id) + public function remove(string $id): mixed { - return $this->client->delete(self::PATH."/{$id}"); + return $this->client->delete(self::PATH . "/{$id}"); } - public function update(string $id) + public function update(string $id): mixed { - return $this->client->post(self::PATH."/{$id}/update"); + return $this->client->post(self::PATH . "/{$id}/update"); } } diff --git a/src/Endpoints/Containers.php b/src/Endpoints/Containers.php index 96831a2..da8edbb 100644 --- a/src/Endpoints/Containers.php +++ b/src/Endpoints/Containers.php @@ -20,117 +20,117 @@ class Containers extends Endpoint { protected const PATH = '/containers'; - public function list(ContainersListQuery $query = null) + public function list(?ContainersListQuery $query = null): mixed { - return $this->client->get(self::PATH.'/json', $query ? $query->toArray() : []); + return $this->client->get(self::PATH . '/json', $query ?? []); } - public function create(array $body) + public function create(array $body): mixed { - return $this->client->post(self::PATH.'/create', $body); + return $this->client->post(self::PATH . '/create', $body); } - public function inspect(string $id, ContainersInspectQuery $query = null) + public function inspect(string $id, ?ContainersInspectQuery $query): mixed { - return $this->client->get(self::PATH."/{$id}/json", $query ? $query->toArray() : []); + return $this->client->get(self::PATH . "/{$id}/json", $query ?? []); } - public function top(string $id, ContainersTopQuery $query = null) + public function top(string $id, ?ContainersTopQuery $query): mixed { - return $this->client->get(self::PATH."/{$id}/top", $query ? $query->toArray() : []); + return $this->client->get(self::PATH . "/{$id}/top", $query ?? []); } - public function logs(string $id, ContainersLogsQuery $query): DockerStream + public function logs(string $id, ?ContainersLogsQuery $query): DockerStream { return $this->client->contentType(ApiClient::MULTIPLEXED_DOCKER_STREAM_CONTENT_TYPE) - ->get(self::PATH."/{$id}/logs", $query->toArray()); + ->get(self::PATH . "/{$id}/logs", $query ?? []); } - public function changes(string $id) + public function changes(string $id): mixed { - return $this->client->get(self::PATH."/{$id}/changes"); + return $this->client->get(self::PATH . "/{$id}/changes"); } - public function export(string $id) + public function export(string $id): mixed { - return $this->client->get(self::PATH."/{$id}/export"); + return $this->client->get(self::PATH . "/{$id}/export"); } - public function stats(string $id, ContainersStatsQuery $query = null) + public function stats(string $id, ?ContainersStatsQuery $query = null): mixed { - return $this->client->get(self::PATH."/{$id}/stats", $query ? $query->toArray() : []); + return $this->client->get(self::PATH . "/{$id}/stats", $query ?? []); } - public function start(string $id, ContainersStartQuery $query = null) + public function start(string $id, ?ContainersStartQuery $query = null): mixed { - return $this->client->post(self::PATH."/{$id}/start", $query ? $query->toArray() : []); + return $this->client->post(self::PATH . "/{$id}/start", $query ?? []); } - public function stop(string $id, ContainersStopQuery $query = null) + public function stop(string $id, ?ContainersStopQuery $query = null): mixed { - return $this->client->post(self::PATH."/{$id}/stop", $query ? $query->toArray() : []); + return $this->client->post(self::PATH . "/{$id}/stop", $query ?? []); } - public function restart(string $id, ContainersRestartQuery $query = null) + public function restart(string $id, ?ContainersRestartQuery $query = null): mixed { - return $this->client->post(self::PATH."/{$id}/restart", $query ? $query->toArray() : []); + return $this->client->post(self::PATH . "/{$id}/restart", $query ?? []); } - public function kill(string $id, ContainersKillQuery $query = null) + public function kill(string $id, ?ContainersKillQuery $query = null): mixed { - return $this->client->post(self::PATH."/{$id}/kill", $query ? $query->toArray() : []); + return $this->client->post(self::PATH . "/{$id}/kill", $query ?? []); } - public function update(string $id, array $body) + public function update(string $id, array $body): mixed { - return $this->client->post(self::PATH."/{$id}/update", $body); + return $this->client->post(self::PATH . "/{$id}/update", $body); } - public function rename(string $id, ContainersRenameQuery $query) + public function rename(string $id, ContainersRenameQuery $query): mixed { - return $this->client->post(self::PATH."/{$id}/rename", null, $query->toArray()); + return $this->client->post(self::PATH . "/{$id}/rename", null, $query->toArray()); } - public function pause(string $id) + public function pause(string $id): mixed { - return $this->client->post(self::PATH."/{$id}/pause"); + return $this->client->post(self::PATH . "/{$id}/pause"); } - public function unpause(string $id) + public function unpause(string $id): mixed { - return $this->client->post(self::PATH."/{$id}/unpause"); + return $this->client->post(self::PATH . "/{$id}/unpause"); } // TODO: Attach // @see https://docs.docker.com/engine/api/v1.42/#tag/Container/operation/ContainerAttach - public function wait(string $id) + public function wait(string $id): mixed { - return $this->client->post(self::PATH."/{$id}/wait"); + return $this->client->post(self::PATH . "/{$id}/wait"); } - public function remove(string $id): null + public function remove(string $id): mixed { - return $this->client->delete(self::PATH."/{$id}"); + return $this->client->delete(self::PATH . "/{$id}"); } - public function filesystem(string $id) + public function filesystem(string $id): mixed { - return $this->client->head(self::PATH."/{$id}/archive"); + return $this->client->head(self::PATH . "/{$id}/archive"); } - public function backupFiles(string $id) + public function backupFiles(string $id): mixed { - return $this->client->get(self::PATH."/{$id}/archive"); + return $this->client->get(self::PATH . "/{$id}/archive"); } - public function uploadFiles(string $id) + public function uploadFiles(string $id): mixed { - return $this->client->put(self::PATH."/{$id}/archive"); + return $this->client->put(self::PATH . "/{$id}/archive"); } - public function prune() + public function prune(): mixed { - return $this->client->post(self::PATH.'/prune'); + return $this->client->post(self::PATH . '/prune'); } } diff --git a/src/Endpoints/Delegates/HandlesConfigs.php b/src/Endpoints/Delegates/HandlesConfigs.php index 23da24c..fdf11a2 100644 --- a/src/Endpoints/Delegates/HandlesConfigs.php +++ b/src/Endpoints/Delegates/HandlesConfigs.php @@ -11,27 +11,27 @@ trait HandlesConfigs { protected Configs $configs; - public function getConfigs() + public function getConfigs(): mixed { return $this->configs->list(); } - public function createConfig() + public function createConfig(): mixed { return $this->configs->create(); } - public function getConfig(string $id) + public function getConfig(string $id): mixed { return $this->configs->inspect($id); } - public function removeConfig(string $id) + public function removeConfig(string $id): mixed { return $this->configs->remove($id); } - public function updateConfig(string $id) + public function updateConfig(string $id): mixed { return $this->configs->update($id); } diff --git a/src/Endpoints/Delegates/HandlesContainers.php b/src/Endpoints/Delegates/HandlesContainers.php index d36c618..eec311a 100644 --- a/src/Endpoints/Delegates/HandlesContainers.php +++ b/src/Endpoints/Delegates/HandlesContainers.php @@ -22,112 +22,112 @@ trait HandlesContainers { protected Containers $containers; - public function getContainers(ContainersListQuery $query = null) + public function getContainers(?ContainersListQuery $query = null): mixed { return $this->containers->list($query); } - public function createContainer(array $body) + public function createContainer(array $body): mixed { return $this->containers->create($body); } - public function getContainer(string $id, ContainersInspectQuery $query = null) + public function getContainer(string $id, ?ContainersInspectQuery $query = null): mixed { return $this->containers->inspect($id, $query); } - public function getContainerProcesses(string $id, ContainersTopQuery $query = null) + public function getContainerProcesses(string $id, ?ContainersTopQuery $query = null): mixed { return $this->containers->top($id, $query); } - public function getContainerLogs(string $id, ContainersLogsQuery $query): DockerStream + public function getContainerLogs(string $id, ?ContainersLogsQuery $query = null): DockerStream { return $this->containers->logs($id, $query); } - public function getContainerChanges(string $id) + public function getContainerChanges(string $id): mixed { return $this->containers->changes($id); } - public function exportContainer(string $id) + public function exportContainer(string $id): mixed { return $this->containers->export($id); } - public function getContainerStats(string $id, ContainersStatsQuery $query = null) + public function getContainerStats(string $id, ?ContainersStatsQuery $query = null): mixed { return $this->containers->stats($id, $query); } - public function startContainer(string $id, ContainersStartQuery $query = null) + public function startContainer(string $id, ?ContainersStartQuery $query = null): mixed { return $this->containers->start($id, $query); } - public function stopContainer(string $id, ContainersStopQuery $query = null) + public function stopContainer(string $id, ?ContainersStopQuery $query = null): mixed { return $this->containers->stop($id, $query); } - public function restartContainer(string $id, ContainersRestartQuery $query = null) + public function restartContainer(string $id, ?ContainersRestartQuery $query = null): mixed { return $this->containers->restart($id, $query); } - public function killContainer(string $id, ContainersKillQuery $query = null) + public function killContainer(string $id, ?ContainersKillQuery $query = null): mixed { return $this->containers->kill($id, $query); } - public function updateContainer(string $id, array $body) + public function updateContainer(string $id, array $body): mixed { return $this->containers->update($id, $body); } - public function renameContainer(string $id, ContainersRenameQuery $query) + public function renameContainer(string $id, ContainersRenameQuery $query): mixed { return $this->containers->rename($id, $query); } - public function pauseContainer(string $id) + public function pauseContainer(string $id): mixed { return $this->containers->pause($id); } - public function unpauseContainer(string $id) + public function unpauseContainer(string $id): mixed { return $this->containers->unpause($id); } - public function waitForContainer(string $id) + public function waitForContainer(string $id): mixed { return $this->containers->wait($id); } - public function removeContainer(string $id): null + public function removeContainer(string $id): mixed { return $this->containers->remove($id); } - public function getContainerFilesystemInfo(string $id) + public function getContainerFilesystemInfo(string $id): mixed { return $this->containers->filesystem($id); } - public function backupFilesFromContainer(string $id) + public function backupFilesFromContainer(string $id): mixed { return $this->containers->backupFiles($id); } - public function uploadFilesToContainer(string $id) + public function uploadFilesToContainer(string $id): mixed { return $this->containers->uploadFiles($id); } - public function pruneContainers() + public function pruneContainers(): mixed { return $this->containers->prune(); } diff --git a/src/Endpoints/Delegates/HandlesImages.php b/src/Endpoints/Delegates/HandlesImages.php index 4168f5c..29bbf4d 100644 --- a/src/Endpoints/Delegates/HandlesImages.php +++ b/src/Endpoints/Delegates/HandlesImages.php @@ -11,77 +11,77 @@ trait HandlesImages { protected Images $images; - public function getImages() + public function getImages(): mixed { return $this->images->list(); } - public function buildImage() + public function buildImage(): mixed { return $this->images->build(); } - public function pruneImageBuildCache() + public function pruneImageBuildCache(): mixed { return $this->images->pruneBuildCache(); } - public function createImage() + public function createImage(): mixed { return $this->images->create(); } - public function getImage(string $name) + public function getImage(string $name): mixed { return $this->images->inspect($name); } - public function getImageLayers($name) + public function getImageLayers(string $name): mixed { return $this->images->history($name); } - public function pushImage(string $name) + public function pushImage(string $name): mixed { return $this->images->push($name); } - public function tagImage(string $name) + public function tagImage(string $name): mixed { return $this->images->tag($name); } - public function removeImage(string $name) + public function removeImage(string $name): mixed { return $this->images->remove($name); } - public function searchImages() + public function searchImages(): mixed { return $this->images->search(); } - public function pruneUnusedImages() + public function pruneUnusedImages(): mixed { return $this->images->prune(); } - public function createImageFromContainer() + public function createImageFromContainer(): mixed { return $this->images->commit(); } - public function exportImage(string $name) + public function exportImage(string $name): mixed { return $this->images->export($name); } - public function exportImages() + public function exportImages(): mixed { return $this->images->exportMany(); } - public function importImages() + public function importImages(): mixed { return $this->images->import(); } diff --git a/src/Endpoints/Delegates/HandlesNetworks.php b/src/Endpoints/Delegates/HandlesNetworks.php index 32576f0..167ccfe 100644 --- a/src/Endpoints/Delegates/HandlesNetworks.php +++ b/src/Endpoints/Delegates/HandlesNetworks.php @@ -11,37 +11,37 @@ trait HandlesNetworks { protected Networks $networks; - public function getNetworks() + public function getNetworks(): mixed { return $this->networks->list(); } - public function getNetwork(string $id) + public function getNetwork(string $id): mixed { return $this->networks->inspect($id); } - public function removeNetwork(string $id) + public function removeNetwork(string $id): mixed { return $this->networks->remove($id); } - public function create(string $id) + public function create(string $id): mixed { return $this->networks->create($id); } - public function attachNetworkToContainer(string $id) + public function attachNetworkToContainer(string $id): mixed { return $this->networks->attachToContainer($id); } - public function removeNetworkFromContainer(string $id) + public function removeNetworkFromContainer(string $id): mixed { return $this->networks->removeFromContainer($id); } - public function pruneUnusedNetworks() + public function pruneUnusedNetworks(): mixed { return $this->networks->prune(); } diff --git a/src/Endpoints/Delegates/HandlesNodes.php b/src/Endpoints/Delegates/HandlesNodes.php index d39646e..eece970 100644 --- a/src/Endpoints/Delegates/HandlesNodes.php +++ b/src/Endpoints/Delegates/HandlesNodes.php @@ -8,22 +8,22 @@ trait HandlesNodes { protected Nodes $nodes; - public function getNodes() + public function getNodes(): mixed { return $this->nodes->list(); } - public function getNode(string $id) + public function getNode(string $id): mixed { return $this->nodes->inspect($id); } - public function removeNode(string $id) + public function removeNode(string $id): mixed { return $this->nodes->remove($id); } - public function updateNode(string $id) + public function updateNode(string $id): mixed { return $this->nodes->update($id); } diff --git a/src/Endpoints/Delegates/HandlesSecrets.php b/src/Endpoints/Delegates/HandlesSecrets.php index 0a8e339..3a91a83 100644 --- a/src/Endpoints/Delegates/HandlesSecrets.php +++ b/src/Endpoints/Delegates/HandlesSecrets.php @@ -11,27 +11,27 @@ trait HandlesSecrets { protected Secrets $secrets; - public function getSecrets() + public function getSecrets(): mixed { return $this->secrets->list(); } - public function createSecret() + public function createSecret(): mixed { return $this->secrets->create(); } - public function getSecret(string $id) + public function getSecret(string $id): mixed { return $this->secrets->inspect($id); } - public function removeSecret(string $id) + public function removeSecret(string $id): mixed { return $this->secrets->remove($id); } - public function updateSecret(string $id) + public function updateSecret(string $id): mixed { return $this->secrets->update($id); } diff --git a/src/Endpoints/Delegates/HandlesServices.php b/src/Endpoints/Delegates/HandlesServices.php index 745900c..eee64e9 100644 --- a/src/Endpoints/Delegates/HandlesServices.php +++ b/src/Endpoints/Delegates/HandlesServices.php @@ -11,32 +11,32 @@ trait HandlesServices { protected Services $services; - public function getServices() + public function getServices(): mixed { return $this->services->list(); } - public function createService() + public function createService(): mixed { return $this->services->create(); } - public function getService(string $id) + public function getService(string $id): mixed { return $this->services->inspect($id); } - public function removeService(string $id) + public function removeService(string $id): mixed { return $this->services->remove($id); } - public function updateService(string $id) + public function updateService(string $id): mixed { return $this->services->update($id); } - public function getServiceLogs(string $id) + public function getServiceLogs(string $id): mixed { return $this->services->logs($id); } diff --git a/src/Endpoints/Delegates/HandlesSwarm.php b/src/Endpoints/Delegates/HandlesSwarm.php index 59b629f..bf31b50 100644 --- a/src/Endpoints/Delegates/HandlesSwarm.php +++ b/src/Endpoints/Delegates/HandlesSwarm.php @@ -11,37 +11,37 @@ trait HandlesSwarm { protected Swarm $swarm; - public function getSwarm() + public function getSwarm(): mixed { return $this->swarm->inspect(); } - public function initSwarm() + public function initSwarm(): mixed { return $this->swarm->init(); } - public function joinSwarm() + public function joinSwarm(): mixed { return $this->swarm->join(); } - public function leaveSwarm() + public function leaveSwarm(): mixed { return $this->swarm->leave(); } - public function updateSwarm() + public function updateSwarm(): mixed { return $this->swarm->update(); } - public function getKeyToUnlockSwarm() + public function getKeyToUnlockSwarm(): mixed { return $this->swarm->keyToUnlock(); } - public function unlockSwarm() + public function unlockSwarm(): mixed { return $this->swarm->unlock(); } diff --git a/src/Endpoints/Delegates/HandlesSystem.php b/src/Endpoints/Delegates/HandlesSystem.php index d3cb05a..6233a88 100644 --- a/src/Endpoints/Delegates/HandlesSystem.php +++ b/src/Endpoints/Delegates/HandlesSystem.php @@ -13,27 +13,27 @@ trait HandlesSystem { protected System $system; - public function getSystemInfo() + public function getSystemInfo(): mixed { return $this->system->info(); } - public function getVersion() + public function getVersion(): mixed { return $this->system->version(); } - public function ping(bool $usingHead = true) + public function ping(bool $usingHead = true): mixed { return $this->system->ping($usingHead); } - public function getSystemEvents(SystemEventsQuery $query = null) + public function getSystemEvents(SystemEventsQuery $query = null): mixed { return $this->system->events($query); } - public function getStorageUsage(SystemStorageUsageQuery $query = null) + public function getStorageUsage(SystemStorageUsageQuery $query = null): mixed { return $this->system->dataUsage($query); } diff --git a/src/Endpoints/Delegates/HandlesTasks.php b/src/Endpoints/Delegates/HandlesTasks.php index d24ff26..6631a06 100644 --- a/src/Endpoints/Delegates/HandlesTasks.php +++ b/src/Endpoints/Delegates/HandlesTasks.php @@ -11,17 +11,17 @@ trait HandlesTasks { protected Tasks $tasks; - public function getTasks() + public function getTasks(): mixed { return $this->tasks->list(); } - public function getTask(string $id) + public function getTask(string $id): mixed { return $this->tasks->inspect($id); } - public function getTaskLogs(string $id) + public function getTaskLogs(string $id): mixed { return $this->tasks->logs($id); } diff --git a/src/Endpoints/Delegates/HandlesVolumes.php b/src/Endpoints/Delegates/HandlesVolumes.php index 0d25127..b86f1cf 100644 --- a/src/Endpoints/Delegates/HandlesVolumes.php +++ b/src/Endpoints/Delegates/HandlesVolumes.php @@ -8,32 +8,32 @@ trait HandlesVolumes { protected Volumes $volumes; - public function getVolumes() + public function getVolumes(): mixed { return $this->volumes->list(); } - public function createVolume() + public function createVolume(): mixed { return $this->volumes->create(); } - public function getVolume(string $name) + public function getVolume(string $name): mixed { return $this->volumes->inspect($name); } - public function updateVolume(string $name) + public function updateVolume(string $name): mixed { return $this->volumes->update($name); } - public function removeVolume(string $name) + public function removeVolume(string $name): mixed { return $this->volumes->remove($name); } - public function pruneUnusedVolumes() + public function pruneUnusedVolumes(): mixed { return $this->volumes->prune(); } diff --git a/src/Endpoints/Images.php b/src/Endpoints/Images.php index fa56088..fb7febb 100644 --- a/src/Endpoints/Images.php +++ b/src/Endpoints/Images.php @@ -8,78 +8,78 @@ class Images extends Endpoint { protected const PATH = '/images'; - public function list() + public function list(): mixed { - return $this->client->get(self::PATH.'/json'); + return $this->client->get(self::PATH . '/json'); } - public function build() + public function build(): mixed { return $this->client->post('/build'); } - public function pruneBuildCache() + public function pruneBuildCache(): mixed { return $this->client->post('/build/prune'); } - public function create() + public function create(): mixed { - return $this->client->post(self::PATH.'/images/create'); + return $this->client->post(self::PATH . '/images/create'); } - public function inspect(string $name) + public function inspect(string $name): mixed { - return $this->client->post(self::PATH."/{$name}/json"); + return $this->client->post(self::PATH . "/{$name}/json"); } - public function history(string $name) + public function history(string $name): mixed { - return $this->client->get(self::PATH."/{$name}/history"); + return $this->client->get(self::PATH . "/{$name}/history"); } - public function push(string $name) + public function push(string $name): mixed { - return $this->client->post(self::PATH."/{$name}/push"); + return $this->client->post(self::PATH . "/{$name}/push"); } - public function tag(string $name) + public function tag(string $name): mixed { - return $this->client->post(self::PATH."/{$name}/tag"); + return $this->client->post(self::PATH . "/{$name}/tag"); } - public function remove(string $name) + public function remove(string $name): mixed { - return $this->client->delete(self::PATH."/{$name}"); + return $this->client->delete(self::PATH . "/{$name}"); } - public function search() + public function search(): mixed { - return $this->client->get(self::PATH.'/search'); + return $this->client->get(self::PATH . '/search'); } - public function prune() + public function prune(): mixed { - return $this->client->post(self::PATH.'/prune'); + return $this->client->post(self::PATH . '/prune'); } - public function commit() + public function commit(): mixed { return $this->client->post('/commit'); } - public function export(string $name) + public function export(string $name): mixed { - return $this->client->get(self::PATH."/{$name}/get"); + return $this->client->get(self::PATH . "/{$name}/get"); } - public function exportMany() + public function exportMany(): mixed { - return $this->client->get(self::PATH.'/get'); + return $this->client->get(self::PATH . '/get'); } - public function import() + public function import(): mixed { - return $this->client->post(self::PATH.'/load'); + return $this->client->post(self::PATH . '/load'); } } diff --git a/src/Endpoints/Networks.php b/src/Endpoints/Networks.php index 1c9c2b9..fee3b2e 100644 --- a/src/Endpoints/Networks.php +++ b/src/Endpoints/Networks.php @@ -8,38 +8,38 @@ class Networks extends Endpoint { protected const PATH = '/networks'; - public function list() + public function list(): mixed { return $this->client->get(self::PATH); } - public function inspect(string $id) + public function inspect(string $id): mixed { - return $this->client->get(self::PATH."/{$id}"); + return $this->client->get(self::PATH . "/{$id}"); } - public function remove(string $id) + public function remove(string $id): mixed { - return $this->client->delete(self::PATH."/{$id}"); + return $this->client->delete(self::PATH . "/{$id}"); } - public function create(string $id) + public function create(string $id): mixed { - return $this->client->post(self::PATH.'/create'); + return $this->client->post(self::PATH . '/create'); } - public function attachToContainer(string $id) + public function attachToContainer(string $id): mixed { - return $this->client->post(self::PATH."/{$id}/connect"); + return $this->client->post(self::PATH . "/{$id}/connect"); } - public function removeFromContainer(string $id) + public function removeFromContainer(string $id): mixed { - return $this->client->post(self::PATH."/{$id}/disconnect"); + return $this->client->post(self::PATH . "/{$id}/disconnect"); } - public function prune() + public function prune(): mixed { - return $this->client->post(self::PATH.'/prune'); + return $this->client->post(self::PATH . '/prune'); } } diff --git a/src/Endpoints/Nodes.php b/src/Endpoints/Nodes.php index f219265..1ad7d66 100644 --- a/src/Endpoints/Nodes.php +++ b/src/Endpoints/Nodes.php @@ -8,23 +8,23 @@ class Nodes extends Endpoint { protected const PATH = '/nodes'; - public function list() + public function list(): mixed { return $this->client->get(self::PATH); } - public function inspect(string $id) + public function inspect(string $id): mixed { - return $this->client->get(self::PATH."/{$id}"); + return $this->client->get(self::PATH . "/{$id}"); } - public function remove(string $id) + public function remove(string $id): mixed { - return $this->client->delete(self::PATH."/{$id}"); + return $this->client->delete(self::PATH . "/{$id}"); } - public function update(string $id) + public function update(string $id): mixed { - return $this->client->post(self::PATH."/{$id}/update"); + return $this->client->post(self::PATH . "/{$id}/update"); } } diff --git a/src/Endpoints/Secrets.php b/src/Endpoints/Secrets.php index 89536ec..91fbee4 100644 --- a/src/Endpoints/Secrets.php +++ b/src/Endpoints/Secrets.php @@ -8,28 +8,28 @@ class Secrets extends Endpoint { protected const PATH = '/secrets'; - public function list() + public function list(): mixed { return $this->client->get(self::PATH); } - public function create() + public function create(): mixed { - return $this->client->post(self::PATH.'/create'); + return $this->client->post(self::PATH . '/create'); } - public function inspect(string $id) + public function inspect(string $id): mixed { - return $this->client->get(self::PATH."/{$id}"); + return $this->client->get(self::PATH . "/{$id}"); } - public function remove(string $id) + public function remove(string $id): mixed { - return $this->client->delete(self::PATH."/{$id}"); + return $this->client->delete(self::PATH . "/{$id}"); } - public function update(string $id) + public function update(string $id): mixed { - return $this->client->post(self::PATH."/{$id}/update"); + return $this->client->post(self::PATH . "/{$id}/update"); } } diff --git a/src/Endpoints/Services.php b/src/Endpoints/Services.php index f06b44a..5d562b6 100644 --- a/src/Endpoints/Services.php +++ b/src/Endpoints/Services.php @@ -8,33 +8,33 @@ class Services extends Endpoint { protected const PATH = '/services'; - public function list() + public function list(): mixed { return $this->client->get(self::PATH); } - public function create() + public function create(): mixed { - return $this->client->post(self::PATH.'/create'); + return $this->client->post(self::PATH . '/create'); } - public function inspect(string $id) + public function inspect(string $id): mixed { - return $this->client->get(self::PATH."/{$id}"); + return $this->client->get(self::PATH . "/{$id}"); } - public function remove(string $id) + public function remove(string $id): mixed { - return $this->client->delete(self::PATH."/{$id}"); + return $this->client->delete(self::PATH . "/{$id}"); } - public function update(string $id) + public function update(string $id): mixed { - return $this->client->post(self::PATH."/{$id}/updates"); + return $this->client->post(self::PATH . "/{$id}/updates"); } - public function logs(string $id) + public function logs(string $id): mixed { - return $this->client->get(self::PATH."/{$id}/logs"); + return $this->client->get(self::PATH . "/{$id}/logs"); } } diff --git a/src/Endpoints/Swarm.php b/src/Endpoints/Swarm.php index 4e69cbf..564cc46 100644 --- a/src/Endpoints/Swarm.php +++ b/src/Endpoints/Swarm.php @@ -3,43 +3,44 @@ namespace OpenSoutheners\Docker\Endpoints; use OpenSoutheners\Docker\Endpoint; +use OpenSoutheners\Docker\Queries\Swarm\SwarmLeaveQuery; class Swarm extends Endpoint { protected const PATH = '/swarm'; - public function inspect() + public function inspect(): mixed { return $this->client->get(self::PATH); } - public function init() + public function init(): mixed { - return $this->client->post(self::PATH.'/init'); + return $this->client->post(self::PATH . '/init'); } - public function join() + public function join(): mixed { - return $this->client->post(self::PATH.'/join'); + return $this->client->post(self::PATH . '/join'); } - public function leave() + public function leave(?SwarmLeaveQuery $query = null): mixed { - return $this->client->post(self::PATH.'/leave'); + return $this->client->post(self::PATH . '/leave'); } - public function update() + public function update(): mixed { - return $this->client->post(self::PATH.'/update'); + return $this->client->post(self::PATH . '/update'); } - public function keyToUnlock() + public function keyToUnlock(): mixed { - return $this->client->get(self::PATH.'/unlockkey'); + return $this->client->get(self::PATH . '/unlockkey'); } - public function unlock() + public function unlock(): mixed { - return $this->client->post(self::PATH.'/unlock'); + return $this->client->post(self::PATH . '/unlock'); } } diff --git a/src/Endpoints/System.php b/src/Endpoints/System.php index 4791e12..a80b9df 100644 --- a/src/Endpoints/System.php +++ b/src/Endpoints/System.php @@ -8,28 +8,28 @@ class System extends Endpoint { - public function info() + public function info(): mixed { return $this->client->get('/info'); } - public function version() + public function version(): mixed { return $this->client->get('/version'); } - public function ping(bool $usingHead = true) + public function ping(bool $usingHead = true): mixed { return $usingHead ? $this->client->head('/_ping') : $this->client->get('/_ping'); } - public function events(SystemEventsQuery $query = null) + public function events(?SystemEventsQuery $query = null): mixed { - return $this->client->get('/events', $query ? $query->toArray() : []); + return $this->client->get('/events', $query ?? []); } - public function dataUsage(SystemStorageUsageQuery $query = null) + public function dataUsage(?SystemStorageUsageQuery $query = null): mixed { - return $this->client->get('/system/df', $query ? $query->toArray() : []); + return $this->client->get('/system/df', $query ?? []); } } diff --git a/src/Endpoints/Tasks.php b/src/Endpoints/Tasks.php index ba1504d..9af1822 100644 --- a/src/Endpoints/Tasks.php +++ b/src/Endpoints/Tasks.php @@ -8,18 +8,18 @@ class Tasks extends Endpoint { protected const PATH = '/tasks'; - public function list() + public function list(): mixed { return $this->client->get(self::PATH); } - public function inspect(string $id) + public function inspect(string $id): mixed { - return $this->client->get(self::PATH."/{$id}"); + return $this->client->get(self::PATH . "/{$id}"); } - public function logs(string $id) + public function logs(string $id): mixed { - return $this->client->get(self::PATH."/{$id}/logs"); + return $this->client->get(self::PATH . "/{$id}/logs"); } } diff --git a/src/Endpoints/Volumes.php b/src/Endpoints/Volumes.php index 5e655dd..4bd9696 100644 --- a/src/Endpoints/Volumes.php +++ b/src/Endpoints/Volumes.php @@ -8,33 +8,33 @@ class Volumes extends Endpoint { protected const PATH = '/volumes'; - public function list() + public function list(): mixed { return $this->client->get(self::PATH); } - public function create() + public function create(): mixed { - return $this->client->post(self::PATH.'/create'); + return $this->client->post(self::PATH . '/create'); } - public function inspect(string $name) + public function inspect(string $name): mixed { - return $this->client->get(self::PATH."/{$name}"); + return $this->client->get(self::PATH . "/{$name}"); } - public function update(string $name) + public function update(string $name): mixed { - return $this->client->put(self::PATH."/{$name}"); + return $this->client->put(self::PATH . "/{$name}"); } - public function remove(string $name) + public function remove(string $name): mixed { - return $this->client->delete(self::PATH."/{$name}"); + return $this->client->delete(self::PATH . "/{$name}"); } - public function prune() + public function prune(): mixed { - return $this->client->post(self::PATH.'/prune'); + return $this->client->post(self::PATH . '/prune'); } } diff --git a/src/Exceptions/DockerApiException.php b/src/Exceptions/DockerApiException.php index a556215..5c9b8ff 100644 --- a/src/Exceptions/DockerApiException.php +++ b/src/Exceptions/DockerApiException.php @@ -7,7 +7,7 @@ class DockerApiException extends \Exception implements ClientExceptionInterface { - public static function fromResponse(ResponseInterface $response, array|string $responseBody) + public static function fromResponse(ResponseInterface $response, array|string $responseBody): self { $errorMessage = $response->getReasonPhrase(); @@ -18,6 +18,6 @@ public static function fromResponse(ResponseInterface $response, array|string $r : ($responseBody['message'] ?? ''); } - return new static($errorMessage, $response->getStatusCode()); + return new self($errorMessage, $response->getStatusCode()); } } diff --git a/src/Exceptions/UnacceptedResponseFormatting.php b/src/Exceptions/UnacceptedResponseFormatting.php index 580849a..b3d65fd 100644 --- a/src/Exceptions/UnacceptedResponseFormatting.php +++ b/src/Exceptions/UnacceptedResponseFormatting.php @@ -9,9 +9,9 @@ class UnacceptedResponseFormatting extends \Exception implements ClientException { public const EXCEPTION_MESSAGE = 'The Docker API response formatted as "%s" did not match return format "%s".'; - public static function fromAccepted(string $contentType, ResponseInterface $response, \Throwable $previous = null) + public static function fromAccepted(string $contentType, ResponseInterface $response, \Throwable $previous = null): self { - return new static(sprintf( + return new self(sprintf( self::EXCEPTION_MESSAGE, implode(', ', $response->getHeader('Content-Type')), $contentType diff --git a/src/Queries/Swarm/SwarmLeaveQuery.php b/src/Queries/Swarm/SwarmLeaveQuery.php new file mode 100644 index 0000000..f08f950 --- /dev/null +++ b/src/Queries/Swarm/SwarmLeaveQuery.php @@ -0,0 +1,18 @@ +getValue($this); - + if ($propertyValue === null) { continue; } diff --git a/src/Serialize/DockerStream.php b/src/Serialize/DockerStream.php index 4b9abf8..a00baa0 100644 --- a/src/Serialize/DockerStream.php +++ b/src/Serialize/DockerStream.php @@ -11,9 +11,7 @@ public function __construct( protected array $onStdinCallables = [], protected array $onStdoutCallables = [], protected array $onStderrCallables = [] - ) { - - } + ) {} /** * Add a callable to read stdin. @@ -45,24 +43,28 @@ public function onStderr(callable $callback): self return $this; } - public function readFrame() + public function readFrame(): void { $header = $this->stream->read(8); + if (! $header) { + return; + } + $decoded = unpack('C1type/C3/N1size', $header); - $output = $this->forceRead($decoded['size']); - if (0 === $decoded['type']) { - $callbackList = $this->onStdinCallables; + if (! $decoded) { + return; } - if (1 === $decoded['type']) { - $callbackList = $this->onStdoutCallables; - } + $output = $this->forceRead($decoded['size']); - if (2 === $decoded['type']) { - $callbackList = $this->onStderrCallables; - } + $callbackList = match ($decoded['type']) { + 0 => $this->onStdinCallables, + 1 => $this->onStdoutCallables, + 2 => $this->onStderrCallables, + default => [], + }; foreach ($callbackList as $callback) { $callback($output); @@ -71,18 +73,14 @@ public function readFrame() /** * Force to have something of the expected size (block). - * - * @param $length - * - * @return string */ - private function forceRead($length) + private function forceRead(int $length): string { $read = ''; do { $read .= $this->stream->read($length - \strlen($read)); - } while (\strlen($read) < $length && !$this->stream->eof()); + } while (\strlen($read) < $length && ! $this->stream->eof()); return $read; } @@ -92,7 +90,7 @@ private function forceRead($length) */ public function wait(): void { - while (!$this->stream->eof()) { + while (! $this->stream->eof()) { $this->readFrame(); } }