|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Uploads; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Uploads\Interfaces\UploaderInterface; |
|
6
|
|
|
|
|
7
|
|
|
final class UploadersRepository |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The array of uploaders classes for field types. |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
private array $uploaderClasses; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The array of uploaders that have been handled, aka events registered. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private array $handledUploaders = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The array of uploaders that have been registered, aka would be handled if needed by the event register. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private array $registeredUploaders = []; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->uploaderClasses = config('backpack.crud.uploaders'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Mark the given uploader as handled. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $objectName - the field/column name |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
|
|
public function markAsHandled(string $objectName) |
|
42
|
|
|
{ |
|
43
|
|
|
if (! in_array($objectName, $this->handledUploaders)) { |
|
44
|
|
|
$this->handledUploaders[] = $objectName; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Check if the given uploader for field/column have been handled. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $objectName |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public function isUploadHandled(string $objectName) |
|
55
|
|
|
{ |
|
56
|
|
|
return in_array($objectName, $this->handledUploaders); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Check if there are uploads for the give object(field/column) type. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $objectType |
|
63
|
|
|
* @param string $group |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public function hasUploadFor(string $objectType, string $group) |
|
67
|
|
|
{ |
|
68
|
|
|
return array_key_exists($objectType, $this->uploaderClasses[$group]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Return the uploader for the given object type. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $objectType |
|
75
|
|
|
* @param string $group |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getUploadFor(string $objectType, string $group) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->uploaderClasses[$group][$objectType]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Register new uploaders or override existing ones. |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $uploaders |
|
87
|
|
|
* @param string $group |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function addUploaderClasses(array $uploaders, string $group) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->uploaderClasses[$group] = array_merge($this->getGroupUploadersClasses($group), $uploaders); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Return the uploaders classes for the given group. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $group |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
private function getGroupUploadersClasses(string $group) |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->uploaderClasses[$group] ?? []; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Register the specified uploader for the given upload name. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $uploadName |
|
110
|
|
|
* @param UploaderInterface $uploader |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
|
|
public function registerUploader(string $uploadName, UploaderInterface $uploader) |
|
114
|
|
|
{ |
|
115
|
|
|
if (! array_key_exists($uploadName, $this->registeredUploaders) || ! in_array($uploader, $this->registeredUploaders[$uploadName])) { |
|
116
|
|
|
$this->registeredUploaders[$uploadName][] = $uploader; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Check if there are uploaders registered for the given upload name. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $uploadName |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
public function hasUploadersRegisteredFor(string $uploadName) |
|
127
|
|
|
{ |
|
128
|
|
|
return array_key_exists($uploadName, $this->registeredUploaders); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get the registered uploaders for the given upload name. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $uploadName |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getRegisteredUploadersFor(string $uploadName) |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->registeredUploaders[$uploadName] ?? []; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Check if the specified upload is registered for the given upload name. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $uploadName |
|
146
|
|
|
* @param UploaderInterface $upload |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
|
|
public function isUploadRegistered(string $uploadName, UploaderInterface $upload) |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->hasUploadersRegisteredFor($uploadName) && in_array($upload->getName(), $this->getRegisteredUploadNames($uploadName)); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Return the registered uploaders names for the given upload name. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $uploadName |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getRegisteredUploadNames(string $uploadName) |
|
161
|
|
|
{ |
|
162
|
|
|
return array_map(function ($uploader) { |
|
163
|
|
|
return $uploader->getName(); |
|
164
|
|
|
}, $this->getRegisteredUploadersFor($uploadName)); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|