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 | * Controls the storage permission. Could be 'private' or 'public'. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $storage_permission; |
||
58 | |||
59 | /** |
||
60 | * Initialize the storage instance. |
||
61 | * |
||
62 | * @return void. |
||
63 | */ |
||
64 | protected function initStorage() |
||
68 | |||
69 | /** |
||
70 | * Set default options form image field. |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | protected function setupDefaultOptions() |
||
104 | |||
105 | /** |
||
106 | * Set preview options form image field. |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | protected function setupPreviewOptions() |
||
121 | |||
122 | /** |
||
123 | * Allow use to remove file. |
||
124 | * |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function removable() |
||
133 | |||
134 | /** |
||
135 | * Allow use to close preview. |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | public function closeable() |
||
145 | |||
146 | /** |
||
147 | * Set options for file-upload plugin. |
||
148 | * |
||
149 | * @param array $options |
||
150 | * |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function options($options = []) |
||
159 | |||
160 | /** |
||
161 | * Set disk for storage. |
||
162 | * |
||
163 | * @param string $disk Disks defined in `config/filesystems.php`. |
||
164 | * |
||
165 | * @throws \Exception |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function disk($disk) |
||
188 | |||
189 | /** |
||
190 | * Specify the directory and name for upload file. |
||
191 | * |
||
192 | * @param string $directory |
||
193 | * @param null|string $name |
||
194 | * |
||
195 | * @return $this |
||
196 | */ |
||
197 | public function move($directory, $name = null) |
||
205 | |||
206 | /** |
||
207 | * Specify the directory upload file. |
||
208 | * |
||
209 | * @param string $dir |
||
210 | * |
||
211 | * @return $this |
||
212 | */ |
||
213 | public function dir($dir) |
||
221 | |||
222 | /** |
||
223 | * Set name of store name. |
||
224 | * |
||
225 | * @param string|callable $name |
||
226 | * |
||
227 | * @return $this |
||
228 | */ |
||
229 | public function name($name) |
||
237 | |||
238 | /** |
||
239 | * Use unique name for store upload file. |
||
240 | * |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function uniqueName() |
||
249 | |||
250 | /** |
||
251 | * Use sequence name for store upload file. |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function sequenceName() |
||
261 | |||
262 | /** |
||
263 | * Get store name of upload file. |
||
264 | * |
||
265 | * @param UploadedFile $file |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | protected function getStoreName(UploadedFile $file) |
||
289 | |||
290 | /** |
||
291 | * Get directory for store file. |
||
292 | * |
||
293 | * @return mixed|string |
||
294 | */ |
||
295 | public function getDirectory() |
||
303 | |||
304 | /** |
||
305 | * Upload file and delete original file. |
||
306 | * |
||
307 | * @param UploadedFile $file |
||
308 | * |
||
309 | * @return mixed |
||
310 | */ |
||
311 | protected function upload(UploadedFile $file) |
||
321 | |||
322 | /** |
||
323 | * If name already exists, rename it. |
||
324 | * |
||
325 | * @param $file |
||
326 | * |
||
327 | * @return void |
||
328 | */ |
||
329 | public function renameIfExists(UploadedFile $file) |
||
335 | |||
336 | /** |
||
337 | * Get file visit url. |
||
338 | * |
||
339 | * @param $path |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | public function objectUrl($path) |
||
355 | |||
356 | /** |
||
357 | * Generate a unique name for uploaded file. |
||
358 | * |
||
359 | * @param UploadedFile $file |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | protected function generateUniqueName(UploadedFile $file) |
||
367 | |||
368 | /** |
||
369 | * Generate a sequence name for uploaded file. |
||
370 | * |
||
371 | * @param UploadedFile $file |
||
372 | * |
||
373 | * @return string |
||
374 | */ |
||
375 | protected function generateSequenceName(UploadedFile $file) |
||
389 | |||
390 | /** |
||
391 | * Destroy original files. |
||
392 | * |
||
393 | * @return void. |
||
394 | */ |
||
395 | public function destroy() |
||
401 | |||
402 | public function storage_permission($permission) |
||
408 | } |
||
409 |
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: