|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dock\Docker\Compose; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Yaml\Parser; |
|
6
|
|
|
|
|
7
|
|
|
class Config |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Project |
|
11
|
|
|
*/ |
|
12
|
|
|
private $project; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param Project $project |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(Project $project) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->project = $project; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* List docker-compose services. |
|
24
|
|
|
* |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getServices() |
|
28
|
|
|
{ |
|
29
|
|
|
return array_keys($this->getConfig()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Return docker-compose service based on current directory. |
|
34
|
|
|
* |
|
35
|
|
|
* @return string Service |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getCurrentService() |
|
38
|
|
|
{ |
|
39
|
|
|
$directory = $this->project->getCurrentRelativePath(); |
|
40
|
|
|
$servicePaths = $this->getServiceBuildPaths(); |
|
41
|
|
|
$dirParts = $this->directoryPathToArray($directory); |
|
42
|
|
|
|
|
43
|
|
|
do { |
|
44
|
|
|
foreach ($servicePaths as $service => $path) { |
|
45
|
|
|
if ($path == $dirParts) { |
|
46
|
|
|
return $service; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} while (array_pop($dirParts)); |
|
50
|
|
|
|
|
51
|
|
|
throw new NotWithinServiceException("Directory $directory is not within any known service"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
private function getServiceBuildPaths() |
|
58
|
|
|
{ |
|
59
|
|
|
return array_map(function ($item) { |
|
60
|
|
|
return $this->directoryPathToArray($item['build']); |
|
61
|
|
|
}, array_filter($this->getConfig(), function ($item) { |
|
62
|
|
|
return array_key_exists('build', $item); |
|
63
|
|
|
})); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
private function directoryPathToArray($path) |
|
70
|
|
|
{ |
|
71
|
|
|
return array_values(array_filter(explode('/', $path), function ($item) { |
|
72
|
|
|
return !in_array($item, ['', '.']); |
|
73
|
|
|
})); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function getConfig() |
|
77
|
|
|
{ |
|
78
|
|
|
$configPath = $this->project->getComposeConfigPath(); |
|
79
|
|
|
|
|
80
|
|
|
return (new Parser())->parse(file_get_contents($configPath)); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|