forked from beluga-php/docker-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerClientFactory.php
More file actions
82 lines (67 loc) · 2.88 KB
/
Copy pathDockerClientFactory.php
File metadata and controls
82 lines (67 loc) · 2.88 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
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
namespace Docker;
use Http\Client\Common\Plugin\AddHostPlugin;
use Http\Client\Common\Plugin\AddPathPlugin;
use Http\Client\Common\Plugin\ContentLengthPlugin;
use Http\Client\Common\Plugin\DecoderPlugin;
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
use Http\Client\Common\PluginClientFactory;
use Http\Client\Socket\Client;
use Http\Discovery\UriFactoryDiscovery;
use Psr\Http\Client\ClientInterface;
final class DockerClientFactory
{
public static function create(array $config = [], PluginClientFactory $pluginClientFactory = null): ClientInterface
{
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'];
$pluginClientFactory = $pluginClientFactory ?? new PluginClientFactory();
return $pluginClientFactory->createClient(
$socketClient,
[
new ContentLengthPlugin(),
new DecoderPlugin(),
new AddPathPlugin($uriFactory->createUri('/v1.41')),
new AddHostPlugin($uriFactory->createUri($host)),
new HeaderDefaultsPlugin([
'host' => \parse_url($host, \PHP_URL_HOST),
]),
],
[
'client_name' => 'docker-client',
]
);
}
public static function createFromEnv(PluginClientFactory $pluginClientFactory = null): ClientInterface
{
$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);
}
}