-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerLookup.php
More file actions
46 lines (37 loc) · 1.59 KB
/
ContainerLookup.php
File metadata and controls
46 lines (37 loc) · 1.59 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
<?php
declare(strict_types=1);
namespace TinyBlocks\DockerContainer\Internal\Containers;
use TinyBlocks\DockerContainer\Contracts\ContainerStarted;
use TinyBlocks\DockerContainer\Internal\Client\Client;
use TinyBlocks\DockerContainer\Internal\CommandHandler\CommandHandler;
use TinyBlocks\DockerContainer\Internal\Commands\DockerInspect;
use TinyBlocks\DockerContainer\Internal\Containers\Definitions\ContainerDefinition;
use TinyBlocks\DockerContainer\Internal\Containers\Models\ContainerId;
use TinyBlocks\DockerContainer\Internal\Exceptions\DockerContainerNotFound;
final readonly class ContainerLookup
{
public function __construct(private Client $client, private ShutdownHook $shutdownHook)
{
}
public function byId(
ContainerId $id,
ContainerDefinition $definition,
CommandHandler $commandHandler
): ContainerStarted {
$dockerInspect = DockerInspect::from(id: $id);
$executionCompleted = $this->client->execute(command: $dockerInspect);
$inspectPayload = (array)json_decode($executionCompleted->getOutput(), true);
if (empty(array_filter($inspectPayload))) {
throw new DockerContainerNotFound(name: $definition->name);
}
$inspection = ContainerInspection::from(inspectResult: $inspectPayload[0]);
return new Started(
id: $id,
name: $definition->name,
address: $inspection->toAddress(),
shutdownHook: $this->shutdownHook,
commandHandler: $commandHandler,
environmentVariables: $inspection->toEnvironmentVariables()
);
}
}