1 | <?php |
||
10 | trait UploadField |
||
11 | { |
||
12 | /** |
||
13 | * Upload directory. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $directory = ''; |
||
18 | |||
19 | /** |
||
20 | * File name. |
||
21 | * |
||
22 | * @var null |
||
23 | */ |
||
24 | protected $name = null; |
||
25 | |||
26 | /** |
||
27 | * Storage instance. |
||
28 | * |
||
29 | * @var \Illuminate\Filesystem\Filesystem |
||
30 | */ |
||
31 | protected $storage = ''; |
||
32 | |||
33 | /** |
||
34 | * If use unique name to store upload file. |
||
35 | * |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $useUniqueName = false; |
||
39 | |||
40 | /** |
||
41 | * If use sequence name to store upload file. |
||
42 | * |
||
43 | * @var bool |
||
44 | */ |
||
45 | protected $useSequenceName = false; |
||
46 | |||
47 | /** |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $removable = false; |
||
51 | |||
52 | /** |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $closeable = false; |
||
56 | |||
57 | /** |
||
58 | * Controls the storage permission. Could be 'private' or 'public'. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $storage_permission; |
||
63 | |||
64 | /** |
||
65 | * Initialize the storage instance. |
||
66 | * |
||
67 | * @return void. |
||
68 | */ |
||
69 | protected function initStorage() |
||
73 | |||
74 | /** |
||
75 | * Set default options form image field. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | protected function setupDefaultOptions() |
||
109 | |||
110 | /** |
||
111 | * Set preview options form image field. |
||
112 | * |
||
113 | * @return void |
||
114 | */ |
||
115 | protected function setupPreviewOptions() |
||
126 | |||
127 | /** |
||
128 | * Allow use to remove file. |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function removable() |
||
138 | |||
139 | /** |
||
140 | * Allow use to close preview. |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function closeable() |
||
150 | |||
151 | /** |
||
152 | * Set options for file-upload plugin. |
||
153 | * |
||
154 | * @param array $options |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function options($options = []) |
||
164 | |||
165 | /** |
||
166 | * Set disk for storage. |
||
167 | * |
||
168 | * @param string $disk Disks defined in `config/filesystems.php`. |
||
169 | * |
||
170 | * @throws \Exception |
||
171 | * |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function disk($disk) |
||
193 | |||
194 | /** |
||
195 | * Specify the directory and name for upload file. |
||
196 | * |
||
197 | * @param string $directory |
||
198 | * @param null|string $name |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function move($directory, $name = null) |
||
210 | |||
211 | /** |
||
212 | * Specify the directory upload file. |
||
213 | * |
||
214 | * @param string $dir |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function dir($dir) |
||
226 | |||
227 | /** |
||
228 | * Set name of store name. |
||
229 | * |
||
230 | * @param string|callable $name |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function name($name) |
||
242 | |||
243 | /** |
||
244 | * Use unique name for store upload file. |
||
245 | * |
||
246 | * @return $this |
||
247 | */ |
||
248 | public function uniqueName() |
||
254 | |||
255 | /** |
||
256 | * Use sequence name for store upload file. |
||
257 | * |
||
258 | * @return $this |
||
259 | */ |
||
260 | public function sequenceName() |
||
266 | |||
267 | /** |
||
268 | * Get store name of upload file. |
||
269 | * |
||
270 | * @param UploadedFile $file |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | protected function getStoreName(UploadedFile $file) |
||
294 | |||
295 | /** |
||
296 | * Get directory for store file. |
||
297 | * |
||
298 | * @return mixed|string |
||
299 | */ |
||
300 | public function getDirectory() |
||
308 | |||
309 | /** |
||
310 | * Upload file and delete original file. |
||
311 | * |
||
312 | * @param UploadedFile $file |
||
313 | * |
||
314 | * @return mixed |
||
315 | */ |
||
316 | protected function upload(UploadedFile $file) |
||
326 | |||
327 | /** |
||
328 | * If name already exists, rename it. |
||
329 | * |
||
330 | * @param $file |
||
331 | * |
||
332 | * @return void |
||
333 | */ |
||
334 | public function renameIfExists(UploadedFile $file) |
||
340 | |||
341 | /** |
||
342 | * Get file visit url. |
||
343 | * |
||
344 | * @param $path |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | public function objectUrl($path) |
||
360 | |||
361 | /** |
||
362 | * Generate a unique name for uploaded file. |
||
363 | * |
||
364 | * @param UploadedFile $file |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | protected function generateUniqueName(UploadedFile $file) |
||
372 | |||
373 | /** |
||
374 | * Generate a sequence name for uploaded file. |
||
375 | * |
||
376 | * @param UploadedFile $file |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | protected function generateSequenceName(UploadedFile $file) |
||
394 | |||
395 | /** |
||
396 | * Destroy original files. |
||
397 | * |
||
398 | * @return void. |
||
399 | */ |
||
400 | public function destroy() |
||
406 | |||
407 | public function storage_permission($permission) |
||
413 | } |
||
414 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: