1 | <?php namespace Arcanesoft\Media; |
||
19 | class Media implements MediaContract |
||
20 | { |
||
21 | /* ----------------------------------------------------------------- |
||
22 | | Constants |
||
23 | | ----------------------------------------------------------------- |
||
24 | */ |
||
25 | |||
26 | const VERSION = '2.2.2'; |
||
27 | |||
28 | /* ----------------------------------------------------------------- |
||
29 | | Properties |
||
30 | | ----------------------------------------------------------------- |
||
31 | */ |
||
32 | |||
33 | /** |
||
34 | * The application instance. |
||
35 | * |
||
36 | * @var \Illuminate\Contracts\Foundation\Application |
||
37 | */ |
||
38 | protected $app; |
||
39 | |||
40 | /* ----------------------------------------------------------------- |
||
41 | | Constructor |
||
42 | | ----------------------------------------------------------------- |
||
43 | */ |
||
44 | |||
45 | /** |
||
46 | * Media constructor. |
||
47 | * |
||
48 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
49 | */ |
||
50 | 60 | public function __construct(Application $app) |
|
54 | |||
55 | /* ----------------------------------------------------------------- |
||
56 | | Getters & Setters |
||
57 | | ----------------------------------------------------------------- |
||
58 | */ |
||
59 | |||
60 | /** |
||
61 | * Get the Filesystem Manager instance. |
||
62 | * |
||
63 | * @return \Illuminate\Contracts\Filesystem\Factory |
||
64 | */ |
||
65 | 40 | public function filesystem() |
|
69 | |||
70 | /** |
||
71 | * Get the Config Repository. |
||
72 | * |
||
73 | * @return \Illuminate\Contracts\Config\Repository |
||
74 | */ |
||
75 | 44 | protected function config() |
|
79 | |||
80 | /** |
||
81 | * Get the default disk name. |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | 40 | public function getDefaultDiskName() |
|
89 | |||
90 | /** |
||
91 | * Get excluded directories. |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | 22 | public function getExcludedDirectories() |
|
96 | { |
||
97 | 22 | return Helpers\ExcludePattern::directories( |
|
98 | 22 | $this->config()->get('arcanesoft.media.directories.excluded', []) |
|
99 | ); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Get excluded files. |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | 16 | public function getExcludedFiles() |
|
111 | |||
112 | /* ----------------------------------------------------------------- |
||
113 | | Main Methods |
||
114 | | ----------------------------------------------------------------- |
||
115 | */ |
||
116 | |||
117 | /** |
||
118 | * Get a filesystem adapter. |
||
119 | * |
||
120 | * @param string|null $driver |
||
121 | * |
||
122 | * @return \Illuminate\Filesystem\FilesystemAdapter|\Illuminate\Contracts\Filesystem\Filesystem |
||
123 | */ |
||
124 | 40 | public function disk($driver = null) |
|
128 | |||
129 | /** |
||
130 | * Get all the directories & files from a given location. |
||
131 | * |
||
132 | * @param string $directory |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | public function all($directory = '/') |
||
137 | { |
||
138 | 6 | $directories = $this->directories($directory)->transform(function ($item) { |
|
139 | 6 | return $item + ['type' => self::MEDIA_TYPE_DIRECTORY]; |
|
140 | 6 | }); |
|
141 | |||
142 | 6 | $files = $this->files($directory)->transform(function (array $item) { |
|
143 | 4 | return $item + ['type' => self::MEDIA_TYPE_FILE]; |
|
144 | 6 | }); |
|
145 | |||
146 | 6 | return array_merge($directories->toArray(), $files->toArray()); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Get all of the directories within a given directory. |
||
151 | * |
||
152 | * @param string $directory |
||
153 | * |
||
154 | * @return \Arcanesoft\Media\Entities\DirectoryCollection |
||
155 | */ |
||
156 | 14 | public function directories($directory) |
|
170 | |||
171 | /** |
||
172 | * Get a collection of all files in a directory. |
||
173 | * |
||
174 | * @param string $directory |
||
175 | * |
||
176 | * @return \Arcanesoft\Media\Entities\FileCollection |
||
177 | */ |
||
178 | 14 | public function files($directory) |
|
199 | |||
200 | /** |
||
201 | * Get the file details. |
||
202 | * |
||
203 | * @param string $path |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | public function file($path) |
||
208 | { |
||
209 | 4 | return $this->files(dirname($path))->first(function ($file) use ($path) { |
|
210 | 4 | return $file['path'] === $path; |
|
211 | }, function () use ($path) { |
||
212 | 2 | throw new FileNotFoundException("File [$path] not found!"); |
|
213 | 4 | }); |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * Store an array of files. |
||
218 | * |
||
219 | * @param string $directory |
||
220 | * @param array $files |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | 4 | public function storeMany($directory, array $files) |
|
235 | |||
236 | /** |
||
237 | * Store a file. |
||
238 | * |
||
239 | * @param string $path |
||
240 | * @param \Illuminate\Http\UploadedFile $file |
||
241 | * @param array $options |
||
242 | * |
||
243 | * @return string|false |
||
244 | */ |
||
245 | 8 | public function store($path, UploadedFile $file, array $options = []) |
|
255 | |||
256 | /** |
||
257 | * Create a directory. |
||
258 | * |
||
259 | * @param string $path |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | 8 | public function makeDirectory($path) |
|
271 | |||
272 | /** |
||
273 | * Delete a directory. |
||
274 | * |
||
275 | * @param string $directory |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | 8 | public function deleteDirectory($directory) |
|
287 | |||
288 | /** |
||
289 | * Delete a file. |
||
290 | * |
||
291 | * @param string $path |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | 6 | public function deleteFile($path) |
|
303 | |||
304 | /** |
||
305 | * Move a file to a new location. |
||
306 | * |
||
307 | * @param string $from |
||
308 | * @param string $to |
||
309 | * |
||
310 | * @return bool |
||
311 | */ |
||
312 | 8 | public function move($from, $to) |
|
320 | |||
321 | /* ----------------------------------------------------------------- |
||
322 | | Check Methods |
||
323 | | ----------------------------------------------------------------- |
||
324 | */ |
||
325 | |||
326 | /** |
||
327 | * Determine if a file/directory exists. |
||
328 | * |
||
329 | * @param string $path |
||
330 | * |
||
331 | * @return bool |
||
332 | */ |
||
333 | 26 | public function exists($path) |
|
337 | |||
338 | /** |
||
339 | * Check if the directory is excluded. |
||
340 | * |
||
341 | * @param string $directory |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | 20 | public function isExcludedDirectory($directory) |
|
353 | |||
354 | /* ----------------------------------------------------------------- |
||
355 | | Other Methods |
||
356 | | ----------------------------------------------------------------- |
||
357 | */ |
||
358 | |||
359 | /** |
||
360 | * Check the given directory location. |
||
361 | * |
||
362 | * @param string &$directory |
||
363 | * |
||
364 | * @throws \Arcanesoft\Media\Exceptions\DirectoryNotFound |
||
365 | * @throws \Arcanesoft\Media\Exceptions\AccessNotAllowed |
||
366 | */ |
||
367 | 22 | protected function checkDirectory(&$directory) |
|
374 | |||
375 | /** |
||
376 | * Check if the directory exists. |
||
377 | * |
||
378 | * @param string $directory |
||
379 | * |
||
380 | * @throws \Arcanesoft\Media\Exceptions\DirectoryNotFound |
||
381 | */ |
||
382 | 22 | protected function checkDirectoryExists($directory) |
|
388 | |||
389 | /** |
||
390 | * Check if can access the directory. |
||
391 | * |
||
392 | * @param string $directory |
||
393 | * |
||
394 | * @throws \Arcanesoft\Media\Exceptions\AccessNotAllowed |
||
395 | */ |
||
396 | 20 | protected function checkDirectoryAccess($directory) |
|
402 | } |
||
403 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.