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 | * Initialize the storage instance. |
||
54 | * |
||
55 | * @return void. |
||
56 | */ |
||
57 | protected function initStorage() |
||
58 | { |
||
59 | $this->disk(config('admin.upload.disk')); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Set default options form image field. |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | protected function setupDefaultOptions() |
||
68 | { |
||
69 | $defaultOptions = [ |
||
70 | 'overwriteInitial' => false, |
||
71 | 'initialPreviewAsData' => true, |
||
72 | 'browseLabel' => trans('admin.browse'), |
||
73 | 'showRemove' => false, |
||
74 | 'showUpload' => false, |
||
75 | // 'initialCaption' => $this->initialCaption($this->value), |
||
76 | 'deleteExtraData' => [ |
||
77 | $this->formatName($this->column) => static::FILE_DELETE_FLAG, |
||
78 | static::FILE_DELETE_FLAG => '', |
||
79 | '_token' => csrf_token(), |
||
80 | '_method' => 'PUT', |
||
81 | ], |
||
82 | ]; |
||
83 | |||
84 | if ($this->form instanceof Form) { |
||
85 | $defaultOptions['deleteUrl'] = $this->form->resource() . '/' . $this->form->model()->getKey(); |
||
86 | } |
||
87 | |||
88 | $this->options($defaultOptions); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Set preview options form image field. |
||
93 | * |
||
94 | * @return void |
||
95 | */ |
||
96 | protected function setupPreviewOptions() |
||
107 | |||
108 | /** |
||
109 | * Allow use to remove file. |
||
110 | * |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function removable() |
||
119 | |||
120 | /** |
||
121 | * Set options for file-upload plugin. |
||
122 | * |
||
123 | * @param array $options |
||
124 | * |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function options($options = []) |
||
133 | |||
134 | /** |
||
135 | * Set disk for storage. |
||
136 | * |
||
137 | * @param string $disk Disks defined in `config/filesystems.php`. |
||
138 | * |
||
139 | * @throws \Exception |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function disk($disk) |
||
162 | |||
163 | /** |
||
164 | * Specify the directory and name for upload file. |
||
165 | * |
||
166 | * @param string $directory |
||
167 | * @param null|string $name |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function move($directory, $name = null) |
||
179 | |||
180 | /** |
||
181 | * Specify the directory upload file. |
||
182 | * |
||
183 | * @param string $dir |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function dir($dir) |
||
195 | |||
196 | /** |
||
197 | * Set name of store name. |
||
198 | * |
||
199 | * @param string|callable $name |
||
200 | * |
||
201 | * @return $this |
||
202 | */ |
||
203 | public function name($name) |
||
211 | |||
212 | /** |
||
213 | * Use unique name for store upload file. |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function uniqueName() |
||
223 | |||
224 | /** |
||
225 | * Get store name of upload file. |
||
226 | * |
||
227 | * @param UploadedFile $file |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | protected function getStoreName(UploadedFile $file) |
||
251 | |||
252 | /** |
||
253 | * Get directory for store file. |
||
254 | * |
||
255 | * @return mixed|string |
||
256 | */ |
||
257 | public function getDirectory() |
||
265 | |||
266 | /** |
||
267 | * Upload file and delete original file. |
||
268 | * |
||
269 | * @param UploadedFile $file |
||
270 | * |
||
271 | * @return mixed |
||
272 | */ |
||
273 | protected function upload(UploadedFile $file) |
||
279 | |||
280 | /** |
||
281 | * If name already exists, rename it. |
||
282 | * |
||
283 | * @param $file |
||
284 | * |
||
285 | * @return void |
||
286 | */ |
||
287 | public function renameIfExists(UploadedFile $file) |
||
293 | |||
294 | /** |
||
295 | * Get file visit url. |
||
296 | * |
||
297 | * @param $path |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | public function objectUrl($path) |
||
313 | |||
314 | /** |
||
315 | * Generate a unique name for uploaded file. |
||
316 | * |
||
317 | * @param UploadedFile $file |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | protected function generateUniqueName(UploadedFile $file) |
||
325 | |||
326 | /** |
||
327 | * Generate a sequence name for uploaded file. |
||
328 | * |
||
329 | * @param UploadedFile $file |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | protected function generateSequenceName(UploadedFile $file) |
||
347 | |||
348 | /** |
||
349 | * Destroy original files. |
||
350 | * |
||
351 | * @return void. |
||
352 | */ |
||
353 | public function destroy() |
||
359 | } |
||
360 |
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: