forked from docker-php/docker-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerClientFactory.php
More file actions
75 lines (61 loc) · 2.61 KB
/
Copy pathDockerClientFactory.php
File metadata and controls
75 lines (61 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Docker;
use GuzzleHttp\Psr7\Uri;
use Http\Client\Common\Plugin\AddHostPlugin;
use Http\Client\Common\Plugin\ContentLengthPlugin;
use Http\Client\Common\Plugin\DecoderPlugin;
use Http\Client\Common\PluginClientFactory;
use Http\Client\HttpClient;
use Http\Client\Socket\Client as SocketHttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory;
final class DockerClientFactory
{
/**
* ( .
*/
public static function create(array $config = [], PluginClientFactory $pluginClientFactory = null): HttpClient
{
if (!\array_key_exists('remote_socket', $config)) {
$config['remote_socket'] = 'unix:///var/run/docker.sock';
}
$messageFactory = new GuzzleMessageFactory();
$socketClient = new SocketHttpClient($messageFactory, $config);
$host = \preg_match('/unix:\/\//', $config['remote_socket']) ? 'http://localhost' : $config['remote_socket'];
$pluginClientFactory = $pluginClientFactory ?? new PluginClientFactory();
return $pluginClientFactory->createClient($socketClient, [
new ContentLengthPlugin(),
new DecoderPlugin(),
new AddHostPlugin(new Uri($host)),
], [
'client_name' => 'docker-client',
]);
}
public static function createFromEnv(PluginClientFactory $pluginClientFactory = null): HttpClient
{
$options = [
'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')) {
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';
$stream_context = [
'cafile' => $cafile,
'local_cert' => $certfile,
'local_pk' => $keyfile,
];
if (\getenv('DOCKER_PEER_NAME')) {
$stream_context['peer_name'] = \getenv('DOCKER_PEER_NAME');
}
$options['ssl'] = true;
$options['stream_context_options'] = [
'ssl' => $stream_context,
];
}
return self::create($options, $pluginClientFactory);
}
}