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() |
||
97 | |||
98 | /** |
||
99 | * Set preview options form image field. |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | protected function setupPreviewOptions() |
||
114 | |||
115 | /** |
||
116 | * Allow use to remove file. |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function removable() |
||
126 | |||
127 | /** |
||
128 | * Set options for file-upload plugin. |
||
129 | * |
||
130 | * @param array $options |
||
131 | * |
||
132 | * @return $this |
||
133 | */ |
||
134 | public function options($options = []) |
||
140 | |||
141 | /** |
||
142 | * Set disk for storage. |
||
143 | * |
||
144 | * @param string $disk Disks defined in `config/filesystems.php`. |
||
145 | * |
||
146 | * @throws \Exception |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function disk($disk) |
||
169 | |||
170 | /** |
||
171 | * Specify the directory and name for upload file. |
||
172 | * |
||
173 | * @param string $directory |
||
174 | * @param null|string $name |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function move($directory, $name = null) |
||
186 | |||
187 | /** |
||
188 | * Specify the directory upload file. |
||
189 | * |
||
190 | * @param string $dir |
||
191 | * |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function dir($dir) |
||
202 | |||
203 | /** |
||
204 | * Set name of store name. |
||
205 | * |
||
206 | * @param string|callable $name |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function name($name) |
||
218 | |||
219 | /** |
||
220 | * Use unique name for store upload file. |
||
221 | * |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function uniqueName() |
||
230 | |||
231 | /** |
||
232 | * Use sequence name for store upload file. |
||
233 | * |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function sequenceName() |
||
242 | |||
243 | /** |
||
244 | * Get store name of upload file. |
||
245 | * |
||
246 | * @param UploadedFile $file |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | protected function getStoreName(UploadedFile $file) |
||
270 | |||
271 | /** |
||
272 | * Get directory for store file. |
||
273 | * |
||
274 | * @return mixed|string |
||
275 | */ |
||
276 | public function getDirectory() |
||
284 | |||
285 | /** |
||
286 | * Upload file and delete original file. |
||
287 | * |
||
288 | * @param UploadedFile $file |
||
289 | * |
||
290 | * @return mixed |
||
291 | */ |
||
292 | protected function upload(UploadedFile $file) |
||
302 | |||
303 | /** |
||
304 | * If name already exists, rename it. |
||
305 | * |
||
306 | * @param $file |
||
307 | * |
||
308 | * @return void |
||
309 | */ |
||
310 | public function renameIfExists(UploadedFile $file) |
||
316 | |||
317 | /** |
||
318 | * Get file visit url. |
||
319 | * |
||
320 | * @param $path |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | public function objectUrl($path) |
||
336 | |||
337 | /** |
||
338 | * Generate a unique name for uploaded file. |
||
339 | * |
||
340 | * @param UploadedFile $file |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | protected function generateUniqueName(UploadedFile $file) |
||
348 | |||
349 | /** |
||
350 | * Generate a sequence name for uploaded file. |
||
351 | * |
||
352 | * @param UploadedFile $file |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | protected function generateSequenceName(UploadedFile $file) |
||
370 | |||
371 | /** |
||
372 | * Destroy original files. |
||
373 | * |
||
374 | * @return void. |
||
375 | */ |
||
376 | public function destroy() |
||
382 | |||
383 | public function storage_permission($permission) |
||
388 | } |
||
389 |
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: