Projekt

Obecné

Profil

Stáhnout (9.12 KB) Statistiky
| Větev: | Revize:
1 cb15593b Cajova-Houba
<?php
2
3
namespace Illuminate\Filesystem;
4
5
use RuntimeException;
6
use InvalidArgumentException;
7
use Illuminate\Support\Collection;
8
use League\Flysystem\AdapterInterface;
9
use League\Flysystem\FilesystemInterface;
10
use League\Flysystem\AwsS3v3\AwsS3Adapter;
11
use League\Flysystem\FileNotFoundException;
12
use League\Flysystem\Adapter\Local as LocalAdapter;
13
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;
14
use Illuminate\Contracts\Filesystem\Cloud as CloudFilesystemContract;
15
use Illuminate\Contracts\Filesystem\FileNotFoundException as ContractFileNotFoundException;
16
17
class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
18
{
19
    /**
20
     * The Flysystem filesystem implementation.
21
     *
22
     * @var \League\Flysystem\FilesystemInterface
23
     */
24
    protected $driver;
25
26
    /**
27
     * Create a new filesystem adapter instance.
28
     *
29
     * @param  \League\Flysystem\FilesystemInterface  $driver
30
     * @return void
31
     */
32
    public function __construct(FilesystemInterface $driver)
33
    {
34
        $this->driver = $driver;
35
    }
36
37
    /**
38
     * Determine if a file exists.
39
     *
40
     * @param  string  $path
41
     * @return bool
42
     */
43
    public function exists($path)
44
    {
45
        return $this->driver->has($path);
46
    }
47
48
    /**
49
     * Get the contents of a file.
50
     *
51
     * @param  string  $path
52
     * @return string
53
     *
54
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
55
     */
56
    public function get($path)
57
    {
58
        try {
59
            return $this->driver->read($path);
60
        } catch (FileNotFoundException $e) {
61
            throw new ContractFileNotFoundException($path, $e->getCode(), $e);
62
        }
63
    }
64
65
    /**
66
     * Write the contents of a file.
67
     *
68
     * @param  string  $path
69
     * @param  string|resource  $contents
70
     * @param  string  $visibility
71
     * @return bool
72
     */
73
    public function put($path, $contents, $visibility = null)
74
    {
75
        if ($visibility = $this->parseVisibility($visibility)) {
76
            $config = ['visibility' => $visibility];
77
        } else {
78
            $config = [];
79
        }
80
81
        if (is_resource($contents)) {
82
            return $this->driver->putStream($path, $contents, $config);
83
        } else {
84
            return $this->driver->put($path, $contents, $config);
85
        }
86
    }
87
88
    /**
89
     * Get the visibility for the given path.
90
     *
91
     * @param  string  $path
92
     * @return string
93
     */
94
    public function getVisibility($path)
95
    {
96
        if ($this->driver->getVisibility($path) == AdapterInterface::VISIBILITY_PUBLIC) {
97
            return FilesystemContract::VISIBILITY_PUBLIC;
98
        }
99
100
        return FilesystemContract::VISIBILITY_PRIVATE;
101
    }
102
103
    /**
104
     * Set the visibility for the given path.
105
     *
106
     * @param  string  $path
107
     * @param  string  $visibility
108
     * @return void
109
     */
110
    public function setVisibility($path, $visibility)
111
    {
112
        return $this->driver->setVisibility($path, $this->parseVisibility($visibility));
113
    }
114
115
    /**
116
     * Prepend to a file.
117
     *
118
     * @param  string  $path
119
     * @param  string  $data
120
     * @return int
121
     */
122
    public function prepend($path, $data, $separator = PHP_EOL)
123
    {
124
        if ($this->exists($path)) {
125
            return $this->put($path, $data.$separator.$this->get($path));
126
        }
127
128
        return $this->put($path, $data);
129
    }
130
131
    /**
132
     * Append to a file.
133
     *
134
     * @param  string  $path
135
     * @param  string  $data
136
     * @return int
137
     */
138
    public function append($path, $data, $separator = PHP_EOL)
139
    {
140
        if ($this->exists($path)) {
141
            return $this->put($path, $this->get($path).$separator.$data);
142
        }
143
144
        return $this->put($path, $data);
145
    }
146
147
    /**
148
     * Delete the file at a given path.
149
     *
150
     * @param  string|array  $paths
151
     * @return bool
152
     */
153
    public function delete($paths)
154
    {
155
        $paths = is_array($paths) ? $paths : func_get_args();
156
157
        foreach ($paths as $path) {
158
            try {
159
                $this->driver->delete($path);
160
            } catch (FileNotFoundException $e) {
161
                //
162
            }
163
        }
164
165
        return true;
166
    }
167
168
    /**
169
     * Copy a file to a new location.
170
     *
171
     * @param  string  $from
172
     * @param  string  $to
173
     * @return bool
174
     */
175
    public function copy($from, $to)
176
    {
177
        return $this->driver->copy($from, $to);
178
    }
179
180
    /**
181
     * Move a file to a new location.
182
     *
183
     * @param  string  $from
184
     * @param  string  $to
185
     * @return bool
186
     */
187
    public function move($from, $to)
188
    {
189
        return $this->driver->rename($from, $to);
190
    }
191
192
    /**
193
     * Get the file size of a given file.
194
     *
195
     * @param  string  $path
196
     * @return int
197
     */
198
    public function size($path)
199
    {
200
        return $this->driver->getSize($path);
201
    }
202
203
    /**
204
     * Get the mime-type of a given file.
205
     *
206
     * @param  string  $path
207
     * @return string|false
208
     */
209
    public function mimeType($path)
210
    {
211
        return $this->driver->getMimetype($path);
212
    }
213
214
    /**
215
     * Get the file's last modification time.
216
     *
217
     * @param  string  $path
218
     * @return int
219
     */
220
    public function lastModified($path)
221
    {
222
        return $this->driver->getTimestamp($path);
223
    }
224
225
    /**
226
     * Get the URL for the file at the given path.
227
     *
228
     * @param  string  $path
229
     * @return string
230
     */
231
    public function url($path)
232
    {
233
        $adapter = $this->driver->getAdapter();
234
235
        if ($adapter instanceof AwsS3Adapter) {
236
            $path = $adapter->getPathPrefix().$path;
237
238
            return $adapter->getClient()->getObjectUrl($adapter->getBucket(), $path);
239
        } elseif ($adapter instanceof LocalAdapter) {
240
            return '/storage/'.$path;
241
        } elseif (method_exists($adapter, 'getUrl')) {
242
            return $adapter->getUrl($path);
243
        } else {
244
            throw new RuntimeException('This driver does not support retrieving URLs.');
245
        }
246
    }
247
248
    /**
249
     * Get an array of all files in a directory.
250
     *
251
     * @param  string|null  $directory
252
     * @param  bool  $recursive
253
     * @return array
254
     */
255
    public function files($directory = null, $recursive = false)
256
    {
257
        $contents = $this->driver->listContents($directory, $recursive);
258
259
        return $this->filterContentsByType($contents, 'file');
260
    }
261
262
    /**
263
     * Get all of the files from the given directory (recursive).
264
     *
265
     * @param  string|null  $directory
266
     * @return array
267
     */
268
    public function allFiles($directory = null)
269
    {
270
        return $this->files($directory, true);
271
    }
272
273
    /**
274
     * Get all of the directories within a given directory.
275
     *
276
     * @param  string|null  $directory
277
     * @param  bool  $recursive
278
     * @return array
279
     */
280
    public function directories($directory = null, $recursive = false)
281
    {
282
        $contents = $this->driver->listContents($directory, $recursive);
283
284
        return $this->filterContentsByType($contents, 'dir');
285
    }
286
287
    /**
288
     * Get all (recursive) of the directories within a given directory.
289
     *
290
     * @param  string|null  $directory
291
     * @return array
292
     */
293
    public function allDirectories($directory = null)
294
    {
295
        return $this->directories($directory, true);
296
    }
297
298
    /**
299
     * Create a directory.
300
     *
301
     * @param  string  $path
302
     * @return bool
303
     */
304
    public function makeDirectory($path)
305
    {
306
        return $this->driver->createDir($path);
307
    }
308
309
    /**
310
     * Recursively delete a directory.
311
     *
312
     * @param  string  $directory
313
     * @return bool
314
     */
315
    public function deleteDirectory($directory)
316
    {
317
        return $this->driver->deleteDir($directory);
318
    }
319
320
    /**
321
     * Get the Flysystem driver.
322
     *
323
     * @return \League\Flysystem\FilesystemInterface
324
     */
325
    public function getDriver()
326
    {
327
        return $this->driver;
328
    }
329
330
    /**
331
     * Filter directory contents by type.
332
     *
333
     * @param  array  $contents
334
     * @param  string  $type
335
     * @return array
336
     */
337
    protected function filterContentsByType($contents, $type)
338
    {
339
        return Collection::make($contents)
340
            ->where('type', $type)
341
            ->pluck('path')
342
            ->values()
343
            ->all();
344
    }
345
346
    /**
347
     * Parse the given visibility value.
348
     *
349
     * @param  string|null  $visibility
350
     * @return string|null
351
     *
352
     * @throws \InvalidArgumentException
353
     */
354
    protected function parseVisibility($visibility)
355
    {
356
        if (is_null($visibility)) {
357
            return;
358
        }
359
360
        switch ($visibility) {
361
            case FilesystemContract::VISIBILITY_PUBLIC:
362
                return AdapterInterface::VISIBILITY_PUBLIC;
363
            case FilesystemContract::VISIBILITY_PRIVATE:
364
                return AdapterInterface::VISIBILITY_PRIVATE;
365
        }
366
367
        throw new InvalidArgumentException('Unknown visibility: '.$visibility);
368
    }
369
370
    /**
371
     * Pass dynamic methods call onto Flysystem.
372
     *
373
     * @param  string  $method
374
     * @param  array  $parameters
375
     * @return mixed
376
     *
377
     * @throws \BadMethodCallException
378
     */
379
    public function __call($method, array $parameters)
380
    {
381
        return call_user_func_array([$this->driver, $method], $parameters);
382
    }
383
}