1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Support\Images; |
4
|
|
|
|
5
|
|
|
use App\Support\Contracts\ImageSetRepository as ImageSetRepositoryContract; |
6
|
|
|
use Symfony\Component\Finder\Finder; |
7
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
8
|
|
|
|
9
|
|
|
class ImageSetRepository implements ImageSetRepositoryContract |
10
|
|
|
{ |
11
|
|
|
protected $locations; |
12
|
|
|
|
13
|
50 |
|
public function __construct() |
14
|
|
|
{ |
15
|
50 |
|
$this->addLocation(base_path('docker')); |
16
|
50 |
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Add a location for the where we may find docker files |
20
|
|
|
* |
21
|
|
|
* @param $location |
22
|
|
|
* @return ImageSetRepository |
23
|
|
|
*/ |
24
|
50 |
|
public function addLocation($location) |
25
|
|
|
{ |
26
|
50 |
|
$this->locations[] = $location; |
27
|
|
|
|
28
|
50 |
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get an image repository using the most recently added locations first |
33
|
|
|
* |
34
|
|
|
* @param $imageSetName |
35
|
|
|
* @return string |
36
|
|
|
* @throws \Exception |
37
|
|
|
*/ |
38
|
|
|
public function getImageRepository($imageSetName) |
39
|
|
|
{ |
40
|
|
|
foreach (array_reverse($this->locations) as $location) { |
41
|
|
|
$path = $location.'/'.$imageSetName; |
42
|
|
|
if (is_dir($path)) { |
43
|
|
|
return new ImageRepository($path, $imageSetName); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
throw new \Exception("Image Set {$imageSetName} not located."); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return a list of the available ImageSets |
52
|
|
|
* |
53
|
|
|
* @return \Illuminate\Support\Collection |
54
|
|
|
*/ |
55
|
|
|
public function availableImageSets() |
56
|
|
|
{ |
57
|
|
|
return collect($this->locations) |
58
|
|
|
->flatMap(function ($location) { |
59
|
|
|
try { |
60
|
|
|
return iterator_to_array( |
61
|
|
|
Finder::create()->in($location)->depth(1)->directories() |
62
|
|
|
); |
63
|
|
|
} catch (\InvalidArgumentException $e) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
})->filter() |
67
|
|
|
->map(function (SplFileInfo $directory) { |
68
|
|
|
return $directory->getRelativePathname(); |
69
|
|
|
})->unique(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|