|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\Conversion; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
8
|
|
|
use Spatie\MediaLibrary\Exceptions\InvalidConversion; |
|
9
|
|
|
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions; |
|
10
|
|
|
use Spatie\MediaLibrary\Media; |
|
11
|
|
|
|
|
12
|
|
|
class ConversionCollection extends Collection |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
|
16
|
|
|
* |
|
17
|
|
|
* @return static |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function createForMedia(Media $media) |
|
20
|
|
|
{ |
|
21
|
|
|
return (new static())->setMedia($media); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
|
26
|
|
|
* |
|
27
|
|
|
* @return $this |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setMedia(Media $media) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->items = []; |
|
32
|
|
|
|
|
33
|
|
|
$this->addConversionsFromRelatedModel($media); |
|
34
|
|
|
|
|
35
|
|
|
$this->addManipulationsFromDb($media); |
|
36
|
|
|
|
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get a conversion by it's name. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $name |
|
44
|
|
|
* |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
* |
|
47
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getByName(string $name) |
|
50
|
|
|
{ |
|
51
|
|
|
$conversion = $this->first(function ($key, Conversion $conversion) use ($name) { |
|
52
|
|
|
return $conversion->getName() === $name; |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
if (!$conversion) { |
|
56
|
|
|
throw InvalidConversion::unknownName($name); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $conversion; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Add the conversion that are defined on the related model of |
|
64
|
|
|
* the given media. |
|
65
|
|
|
* |
|
66
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function addConversionsFromRelatedModel(Media $media) |
|
69
|
|
|
{ |
|
70
|
|
|
$modelName = Arr::get(Relation::morphMap(), $media->model_type, $media->model_type); |
|
71
|
|
|
|
|
72
|
|
|
/* |
|
73
|
|
|
* To prevent an sql query create a new model instead |
|
74
|
|
|
* of the using the associated one |
|
75
|
|
|
*/ |
|
76
|
|
|
$model = new $modelName(); |
|
77
|
|
|
|
|
78
|
|
|
if ($model instanceof HasMediaConversions) { |
|
79
|
|
|
$model->registerMediaConversions(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->items = $model->mediaConversions; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Add the extra manipulations that are defined on the given media. |
|
87
|
|
|
* |
|
88
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function addManipulationsFromDb(Media $media) |
|
91
|
|
|
{ |
|
92
|
|
|
foreach ($media->manipulations as $conversionName => $manipulation) { |
|
93
|
|
|
$this->addManipulationToConversion($manipulation, $conversionName); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get all the conversions in the collection. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $collectionName |
|
101
|
|
|
* |
|
102
|
|
|
* @return $this |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getConversions(string $collectionName = '') |
|
105
|
|
|
{ |
|
106
|
|
|
if ($collectionName === '') { |
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->filter(function (Conversion $conversion) use ($collectionName) { |
|
111
|
|
|
return $conversion->shouldBePerformedOn($collectionName); |
|
112
|
|
|
}); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/* |
|
116
|
|
|
* Get all the conversions in the collection that should be queued. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getQueuedConversions(string $collectionName = '') : ConversionCollection |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->getConversions($collectionName)->filter(function (Conversion $conversion) { |
|
121
|
|
|
return $conversion->shouldBeQueued(); |
|
122
|
|
|
}); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/* |
|
126
|
|
|
* Add the given manipulation to the conversion with the given name. |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function addManipulationToConversion(array $manipulation, string $conversionName) |
|
129
|
|
|
{ |
|
130
|
|
|
foreach ($this as $conversion) { |
|
131
|
|
|
if ($conversion->getName() === $conversionName) { |
|
132
|
|
|
$conversion->addAsFirstManipulation($manipulation); |
|
133
|
|
|
|
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/* |
|
140
|
|
|
* Get all the conversions in the collection that should not be queued. |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getNonQueuedConversions(string $collectionName = '') : ConversionCollection |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->getConversions($collectionName)->filter(function (Conversion $conversion) { |
|
145
|
|
|
return !$conversion->shouldBeQueued(); |
|
146
|
|
|
}); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Return the list of conversion files. |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getConversionsFiles(string $collectionName = '') : ConversionCollection |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->getConversions($collectionName)->map(function (Conversion $conversion) { |
|
155
|
|
|
return $conversion->getName() . '.' . $conversion->getResultExtension(); |
|
156
|
|
|
}); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|