1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentDockerCompose\DockerCompose; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Symfony\Component\Finder\Finder; |
7
|
|
|
use TheAentMachine\AentDockerCompose\Aenthill\Enum\PheromoneEnum; |
8
|
|
|
use TheAentMachine\AentDockerCompose\Aenthill\Exception\ContainerProjectDirEnvVariableEmptyException; |
9
|
|
|
|
10
|
|
|
class DockerComposeService |
11
|
|
|
{ |
12
|
|
|
/** @var LoggerInterface */ |
13
|
|
|
private $log; |
14
|
|
|
|
15
|
|
|
/** @var DockerComposeFile[] */ |
16
|
|
|
private $files; |
17
|
|
|
|
18
|
|
|
public function __construct(LoggerInterface $log) |
19
|
|
|
{ |
20
|
|
|
$this->log = $log; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @throws ContainerProjectDirEnvVariableEmptyException |
25
|
|
|
*/ |
26
|
|
|
private function seekFiles(): void |
27
|
|
|
{ |
28
|
|
|
$containerProjectDir = getenv(PheromoneEnum::PHEROMONE_CONTAINER_PROJECT_DIR); |
29
|
|
|
if (empty($containerProjectDir)) { |
30
|
|
|
throw new ContainerProjectDirEnvVariableEmptyException(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$finder = new Finder(); |
34
|
|
|
$dockerComposeFileFilter = function (\SplFileInfo $file) { |
35
|
|
|
return $file->isFile() && preg_match('/^docker-compose(.)*\.(yaml|yml)$/', $file->getFilename()); |
36
|
|
|
}; |
37
|
|
|
$finder->files()->filter($dockerComposeFileFilter)->in($containerProjectDir)->depth('== 0'); |
38
|
|
|
|
39
|
|
|
if (!$finder->hasResults()) { |
40
|
|
|
$this->log->info("no docker-compose file found, let's create it"); |
41
|
|
|
$this->createDockerComposeFile($containerProjectDir . '/docker-compose.yml'); |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** @var \SplFileInfo $file */ |
46
|
|
|
foreach ($finder as $file) { |
47
|
|
|
$this->files[] = new DockerComposeFile($file); |
48
|
|
|
$this->log->info($file->getFilename() . ' has been found'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/*if (count($this->files) === 1) { |
52
|
|
|
$this->log->info($this->files[0]->getFilename() . ' has been found'); |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
throw new NotImplementedException("multiple docker-compose files handling is not yet implemented"); |
57
|
|
|
*/ |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string[] |
62
|
|
|
*/ |
63
|
|
|
public function getDockerComposePathnames(): array |
64
|
|
|
{ |
65
|
|
|
if ($this->files === null) { |
66
|
|
|
$this->seekFiles(); |
67
|
|
|
} |
68
|
|
|
$pathnames = array(); |
69
|
|
|
foreach ($this->files as $file) { |
70
|
|
|
$pathnames[] = $file->getPathname(); |
71
|
|
|
} |
72
|
|
|
return $pathnames; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $path |
77
|
|
|
*/ |
78
|
|
|
private function createDockerComposeFile(string $path): void |
79
|
|
|
{ |
80
|
|
|
// TODO ask questions about version and so on! |
81
|
|
|
$fp = fopen($path, 'wb'); |
82
|
|
|
fclose($fp); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$file = new DockerComposeFile(new \SplFileInfo($path)); |
85
|
|
|
$this->files[] = $file; |
86
|
|
|
$this->log->info($file->getFilename() . ' was successfully created!'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|