1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Support\Images; |
4
|
|
|
|
5
|
|
|
use App\Support\Contracts\ImageRepository as ImageRepositoryContract; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
class ImageRepository implements ImageRepositoryContract |
9
|
|
|
{ |
10
|
|
|
/** @var string */ |
11
|
|
|
protected $path; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $name; |
15
|
|
|
|
16
|
|
|
protected $firstPartyImages = []; |
17
|
|
|
|
18
|
|
|
protected $thirdPartyImages = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ImageRepository constructor. |
22
|
|
|
* |
23
|
|
|
* @param $path |
24
|
|
|
* |
25
|
|
|
* @throws Exception |
26
|
|
|
*/ |
27
|
40 |
|
public function __construct($path) |
28
|
|
|
{ |
29
|
40 |
|
$this->path = $path; |
30
|
|
|
|
31
|
40 |
|
$this->loadConfig(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Load the configuration file for the image set. |
36
|
|
|
* |
37
|
|
|
* @throws Exception |
38
|
|
|
*/ |
39
|
40 |
|
protected function loadConfig() |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
40 |
|
$config = json_decode(file_get_contents($this->path.'/config.json')); |
43
|
|
|
|
44
|
40 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
45
|
|
|
throw new \Exception(json_last_error_msg()); |
46
|
|
|
} |
47
|
|
|
|
48
|
40 |
|
if (!property_exists($config, 'name') || !$config->name) { |
49
|
1 |
|
throw new \Exception('There is no name specified.'); |
50
|
|
|
} |
51
|
|
|
|
52
|
40 |
|
$this->name = $config->name; |
53
|
40 |
|
$this->firstPartyImages = (array) $config->firstParty ?? []; |
54
|
40 |
|
$this->thirdPartyImages = (array) $config->thirdParty ?? []; |
55
|
2 |
|
} catch (\Exception $e) { |
56
|
2 |
|
throw new \Exception("Failed loading config for image set '{$this->path}'. {$e->getMessage()}"); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the docker images that are pulled on install. A custom image set name may be specified. |
62
|
|
|
* |
63
|
|
|
* @throws Exception |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
28 |
|
public function firstParty() |
68
|
|
|
{ |
69
|
28 |
|
return collect($this->firstPartyImages) |
70
|
28 |
|
->map(function ($version, $name) { |
71
|
28 |
|
return new Image($this->name.'-'.$name.':'.$version, $this->getDockerContext().$name); |
72
|
28 |
|
})->values()->toArray(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* The third party docker images. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
19 |
|
public function thirdParty() |
81
|
|
|
{ |
82
|
19 |
|
return collect($this->thirdPartyImages) |
83
|
19 |
|
->map(function ($image) { |
84
|
19 |
|
return new Image($image); |
85
|
19 |
|
})->toArray(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return a full listing of images. |
90
|
|
|
* |
91
|
|
|
* @throws Exception |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
18 |
|
public function all() |
96
|
|
|
{ |
97
|
18 |
|
return array_merge($this->firstParty(), $this->thirdParty()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return Docker context path. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
30 |
|
public function getDockerContext() |
106
|
|
|
{ |
107
|
30 |
|
return $this->path.'/docker/'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the path. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
5 |
|
public function getPath() |
116
|
|
|
{ |
117
|
5 |
|
return $this->path; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return the name. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
15 |
|
public function getName() |
126
|
|
|
{ |
127
|
15 |
|
return $this->name; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Find the image for a given service. |
132
|
|
|
* |
133
|
|
|
* @param $service |
134
|
|
|
* @param bool $firstPartyOnly |
135
|
|
|
* |
136
|
|
|
* @throws Exception |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
26 |
|
public function findByServiceName($service, $firstPartyOnly = false) |
141
|
|
|
{ |
142
|
26 |
|
$service = preg_replace('/[^a-zA-Z0-9\-\_]/', '-', $service); |
143
|
26 |
|
$images = $firstPartyOnly ? $this->firstParty() : $this->all(); |
144
|
|
|
|
145
|
26 |
|
if (!$service) { |
146
|
4 |
|
return $images; |
147
|
|
|
} |
148
|
|
|
|
149
|
22 |
|
return array_values(array_filter($images, function (Image $image) use ($service) { |
150
|
22 |
|
return strpos($image->getName(), $service) !== false; |
151
|
|
|
})); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Find the first image for a given service. |
156
|
|
|
* |
157
|
|
|
* @param $service |
158
|
|
|
* @param bool $firstPartyOnly |
159
|
|
|
* |
160
|
|
|
* @throws Exception |
161
|
|
|
* |
162
|
|
|
* @return Image |
163
|
|
|
*/ |
164
|
15 |
|
public function firstByServiceName($service, $firstPartyOnly = false) |
165
|
|
|
{ |
166
|
15 |
|
if (!$service) { |
167
|
1 |
|
throw new \Exception('A service name must be provided.'); |
168
|
|
|
} |
169
|
|
|
|
170
|
14 |
|
$found = $this->findByServiceName($service, $firstPartyOnly); |
171
|
|
|
|
172
|
14 |
|
if (empty($found)) { |
173
|
1 |
|
throw new \Exception("Service not found '{$service}'"); |
174
|
|
|
} |
175
|
|
|
|
176
|
14 |
|
return $found[0]; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|