|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Support\Images; |
|
4
|
|
|
|
|
5
|
|
|
use App\Support\Contracts\ImageSetRepository as ImageSetRepositoryContract; |
|
6
|
|
|
use Illuminate\Foundation\Application; |
|
7
|
|
|
use Symfony\Component\Finder\Finder; |
|
8
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
9
|
|
|
|
|
10
|
|
|
class ImageSetRepository implements ImageSetRepositoryContract |
|
11
|
|
|
{ |
|
12
|
|
|
protected $locations = []; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* ImageSetRepository constructor. |
|
16
|
|
|
* Accepts a set of locations, or a singular one. |
|
17
|
|
|
* |
|
18
|
|
|
* @param $location |
|
19
|
|
|
*/ |
|
20
|
90 |
|
public function __construct($location) |
|
21
|
|
|
{ |
|
22
|
90 |
|
$this->addLocation($location); |
|
23
|
90 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Add a location for the where we may find docker files. |
|
27
|
|
|
* |
|
28
|
|
|
* @param $location |
|
29
|
|
|
* |
|
30
|
|
|
* @return ImageSetRepository |
|
31
|
|
|
*/ |
|
32
|
90 |
|
public function addLocation($location) |
|
33
|
|
|
{ |
|
34
|
90 |
|
if (!is_array($location)) { |
|
35
|
3 |
|
$location = [$location]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
90 |
|
$this->locations = array_unique(array_merge($this->locations, $location)); |
|
39
|
|
|
|
|
40
|
90 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get an image repository using the most recently added locations first. |
|
45
|
|
|
* |
|
46
|
|
|
* @param $imageSetName |
|
47
|
|
|
* |
|
48
|
|
|
* @throws \Exception |
|
49
|
|
|
* |
|
50
|
|
|
* @return ImageRepository |
|
51
|
|
|
*/ |
|
52
|
7 |
|
public function getImageRepository($imageSetName) |
|
53
|
|
|
{ |
|
54
|
7 |
|
foreach (array_reverse($this->locations) as $location) { |
|
55
|
7 |
|
$path = $location.'/'.$imageSetName; |
|
56
|
7 |
|
if (is_dir($path)) { |
|
57
|
7 |
|
return new ImageRepository($path); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
throw new \Exception("Image Set {$imageSetName} not located."); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Return a list of the available ImageSets. |
|
66
|
|
|
* |
|
67
|
|
|
* @return \Illuminate\Support\Collection |
|
68
|
|
|
*/ |
|
69
|
90 |
|
public function availableImageSets() |
|
70
|
|
|
{ |
|
71
|
90 |
|
return collect($this->locations) |
|
72
|
|
|
->flatMap(function ($location) { |
|
73
|
|
|
try { |
|
74
|
90 |
|
return iterator_to_array( |
|
75
|
90 |
|
Finder::create()->in($location)->depth(1)->directories() |
|
76
|
|
|
); |
|
77
|
90 |
|
} catch (\InvalidArgumentException $e) { |
|
78
|
90 |
|
return; |
|
79
|
|
|
} |
|
80
|
90 |
|
})->filter() |
|
81
|
|
|
->map(function (SplFileInfo $directory) { |
|
82
|
90 |
|
return $directory->getRelativePathname(); |
|
83
|
90 |
|
})->unique(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Register the view namespaces for the image sets. |
|
88
|
|
|
* |
|
89
|
|
|
* @param $app |
|
90
|
|
|
*/ |
|
91
|
90 |
|
public function registerViewNamespaces(Application $app) |
|
92
|
|
|
{ |
|
93
|
90 |
|
foreach ($this->availableImageSets() as $path => $namespace) { |
|
94
|
90 |
|
$app['view']->addNamespace($namespace, $path.'/docker_compose'); |
|
95
|
|
|
} |
|
96
|
90 |
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|