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