-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerCommandHandler.php
More file actions
76 lines (58 loc) · 2.89 KB
/
ContainerCommandHandler.php
File metadata and controls
76 lines (58 loc) · 2.89 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
<?php
declare(strict_types=1);
namespace TinyBlocks\DockerContainer\Internal\CommandHandler;
use TinyBlocks\DockerContainer\Contracts\ContainerStarted;
use TinyBlocks\DockerContainer\Contracts\ExecutionCompleted;
use TinyBlocks\DockerContainer\Internal\Client\Client;
use TinyBlocks\DockerContainer\Internal\Commands\Command;
use TinyBlocks\DockerContainer\Internal\Commands\DockerCopy;
use TinyBlocks\DockerContainer\Internal\Commands\DockerList;
use TinyBlocks\DockerContainer\Internal\Commands\DockerNetworkCreate;
use TinyBlocks\DockerContainer\Internal\Commands\DockerRun;
use TinyBlocks\DockerContainer\Internal\Containers\ContainerLookup;
use TinyBlocks\DockerContainer\Internal\Containers\Definitions\ContainerDefinition;
use TinyBlocks\DockerContainer\Internal\Containers\Definitions\CopyInstruction;
use TinyBlocks\DockerContainer\Internal\Containers\Models\ContainerId;
use TinyBlocks\DockerContainer\Internal\Containers\ShutdownHook;
use TinyBlocks\DockerContainer\Internal\Exceptions\DockerCommandExecutionFailed;
final readonly class ContainerCommandHandler implements CommandHandler
{
private ContainerLookup $lookup;
public function __construct(private Client $client, ShutdownHook $shutdownHook)
{
$this->lookup = new ContainerLookup(client: $client, shutdownHook: $shutdownHook);
}
public function execute(Command $command): ExecutionCompleted
{
return $this->client->execute(command: $command);
}
public function findBy(ContainerDefinition $definition): ?ContainerStarted
{
$dockerList = DockerList::from(name: $definition->name);
$executionCompleted = $this->client->execute(command: $dockerList);
$output = trim($executionCompleted->getOutput());
if (empty($output)) {
return null;
}
$id = ContainerId::from(value: $output);
return $this->lookup->byId(id: $id, definition: $definition, commandHandler: $this);
}
public function run(DockerRun $dockerRun): ContainerStarted
{
if (!is_null($dockerRun->definition->network)) {
$this->client->execute(command: DockerNetworkCreate::from(network: $dockerRun->definition->network));
}
$executionCompleted = $this->client->execute(command: $dockerRun);
if (!$executionCompleted->isSuccessful()) {
throw DockerCommandExecutionFailed::fromCommand(command: $dockerRun, execution: $executionCompleted);
}
$id = ContainerId::from(value: $executionCompleted->getOutput());
$started = $this->lookup->byId(id: $id, definition: $dockerRun->definition, commandHandler: $this);
$dockerRun->definition->copyInstructions->each(
actions: function (CopyInstruction $instruction) use ($id): void {
$this->client->execute(command: DockerCopy::from(id: $id, instruction: $instruction));
}
);
return $started;
}
}