Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"require": {
"ext-curl": "*",
"php": ">= 8.1",
"beluga-php/docker-php": "^1.45"
"beluga-php/docker-php": "^1.45",
"php-http/client-common": "^2.7"
},
"require-dev": {
"ext-pdo": "*",
Expand Down
22 changes: 21 additions & 1 deletion src/ContainerClient/DockerContainerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace Testcontainers\ContainerClient;

use Composer\InstalledVersions;
use Docker\Docker as DockerClient;
use Docker\DockerClientFactory;
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
use Http\Client\Common\PluginClient;

class DockerContainerClient
{
Expand All @@ -26,14 +30,30 @@ private function __construct()
public static function getDockerClient(): DockerClient
{
if (self::$dockerClient === null) {
self::$dockerClient = DockerClient::create();
try {
$version = InstalledVersions::getPrettyVersion('testcontainers/testcontainers') ?? 'unknown';
} catch (\OutOfBoundsException) {
$version = 'unknown';
}

$version = str_replace('+no-version-set', '', $version);

$baseHttpClient = DockerClientFactory::createFromEnv();

$httpClient = new PluginClient(
$baseHttpClient,
[new HeaderDefaultsPlugin(['User-Agent' => 'tc-php/' . $version])]
);

self::$dockerClient = DockerClient::create($httpClient);
}

return self::$dockerClient;
}

/**
* Injects a DockerClient instance for testing or special use cases.
* Note: clients injected via this method will not have the tc-php User-Agent header applied automatically.
*
* @param DockerClient $client The DockerClient instance to set.
*/
Expand Down
Loading