1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Uploaders\Support; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\Uploaders\Support\Interfaces\UploaderInterface; |
6
|
|
|
|
7
|
|
|
final class UploadersRepository |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The array of uploaders classes for field types. |
11
|
|
|
*/ |
12
|
|
|
private array $uploaderClasses; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Uploaders registered in a repeatable group. |
16
|
|
|
*/ |
17
|
|
|
private array $repeatableUploaders = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Uploaders that have already been handled (events registered) for each field/column instance. |
21
|
|
|
*/ |
22
|
|
|
private array $handledUploaders = []; |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->uploaderClasses = config('backpack.crud.uploaders'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Mark the given uploader as handled. |
31
|
|
|
*/ |
32
|
|
|
public function markAsHandled(string $objectName): void |
33
|
|
|
{ |
34
|
|
|
if (! in_array($objectName, $this->handledUploaders)) { |
35
|
|
|
$this->handledUploaders[] = $objectName; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check if the given uploader for field/column have been handled. |
41
|
|
|
*/ |
42
|
|
|
public function isUploadHandled(string $objectName): bool |
43
|
|
|
{ |
44
|
|
|
return in_array($objectName, $this->handledUploaders); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Check if there are uploads for the give object(field/column) type. |
49
|
|
|
*/ |
50
|
|
|
public function hasUploadFor(string $objectType, string $group): bool |
51
|
|
|
{ |
52
|
|
|
return array_key_exists($objectType, $this->uploaderClasses[$group]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return the uploader for the given object type. |
57
|
|
|
*/ |
58
|
|
|
public function getUploadFor(string $objectType, string $group): string |
59
|
|
|
{ |
60
|
|
|
return $this->uploaderClasses[$group][$objectType]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Register new uploaders or override existing ones. |
65
|
|
|
*/ |
66
|
|
|
public function addUploaderClasses(array $uploaders, string $group): void |
67
|
|
|
{ |
68
|
|
|
$this->uploaderClasses[$group] = array_merge($this->getGroupUploadersClasses($group), $uploaders); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the uploaders classes for the given group. |
73
|
|
|
*/ |
74
|
|
|
private function getGroupUploadersClasses(string $group): array |
75
|
|
|
{ |
76
|
|
|
return $this->uploaderClasses[$group] ?? []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Register the specified uploader for the given upload name. |
81
|
|
|
*/ |
82
|
|
|
public function registerRepeatableUploader(string $uploadName, UploaderInterface $uploader): void |
83
|
|
|
{ |
84
|
|
|
if (! array_key_exists($uploadName, $this->repeatableUploaders) || ! in_array($uploader, $this->repeatableUploaders[$uploadName])) { |
85
|
|
|
$this->repeatableUploaders[$uploadName][] = $uploader; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check if there are uploaders registered for the given upload name. |
91
|
|
|
*/ |
92
|
|
|
public function hasRepeatableUploadersFor(string $uploadName): bool |
93
|
|
|
{ |
94
|
|
|
return array_key_exists($uploadName, $this->repeatableUploaders); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the repeatable uploaders for the given upload name. |
99
|
|
|
*/ |
100
|
|
|
public function getRepeatableUploadersFor(string $uploadName): array |
101
|
|
|
{ |
102
|
|
|
return $this->repeatableUploaders[$uploadName] ?? []; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Check if the specified upload is registered for the given repeatable uploads. |
107
|
|
|
*/ |
108
|
|
|
public function isUploadRegistered(string $uploadName, UploaderInterface $upload): bool |
109
|
|
|
{ |
110
|
|
|
return $this->hasRepeatableUploadersFor($uploadName) && in_array($upload->getName(), $this->getRegisteredUploadNames($uploadName)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return the registered uploaders names for the given repeatable upload name. |
115
|
|
|
*/ |
116
|
|
|
public function getRegisteredUploadNames(string $uploadName): array |
117
|
|
|
{ |
118
|
|
|
return array_map(function ($uploader) { |
119
|
|
|
return $uploader->getName(); |
120
|
|
|
}, $this->getRepeatableUploadersFor($uploadName)); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|