1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Filesystem;
|
4
|
|
5
|
use Closure;
|
6
|
use Aws\S3\S3Client;
|
7
|
use OpenCloud\Rackspace;
|
8
|
use Illuminate\Support\Arr;
|
9
|
use InvalidArgumentException;
|
10
|
use League\Flysystem\AdapterInterface;
|
11
|
use League\Flysystem\FilesystemInterface;
|
12
|
use League\Flysystem\Filesystem as Flysystem;
|
13
|
use League\Flysystem\Adapter\Ftp as FtpAdapter;
|
14
|
use League\Flysystem\Rackspace\RackspaceAdapter;
|
15
|
use League\Flysystem\Adapter\Local as LocalAdapter;
|
16
|
use League\Flysystem\AwsS3v3\AwsS3Adapter as S3Adapter;
|
17
|
use Illuminate\Contracts\Filesystem\Factory as FactoryContract;
|
18
|
|
19
|
class FilesystemManager implements FactoryContract
|
20
|
{
|
21
|
/**
|
22
|
* The application instance.
|
23
|
*
|
24
|
* @var \Illuminate\Contracts\Foundation\Application
|
25
|
*/
|
26
|
protected $app;
|
27
|
|
28
|
/**
|
29
|
* The array of resolved filesystem drivers.
|
30
|
*
|
31
|
* @var array
|
32
|
*/
|
33
|
protected $disks = [];
|
34
|
|
35
|
/**
|
36
|
* The registered custom driver creators.
|
37
|
*
|
38
|
* @var array
|
39
|
*/
|
40
|
protected $customCreators = [];
|
41
|
|
42
|
/**
|
43
|
* Create a new filesystem manager instance.
|
44
|
*
|
45
|
* @param \Illuminate\Contracts\Foundation\Application $app
|
46
|
* @return void
|
47
|
*/
|
48
|
public function __construct($app)
|
49
|
{
|
50
|
$this->app = $app;
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* Get a filesystem instance.
|
55
|
*
|
56
|
* @param string $name
|
57
|
* @return \Illuminate\Contracts\Filesystem\Filesystem
|
58
|
*/
|
59
|
public function drive($name = null)
|
60
|
{
|
61
|
return $this->disk($name);
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Get a filesystem instance.
|
66
|
*
|
67
|
* @param string $name
|
68
|
* @return \Illuminate\Contracts\Filesystem\Filesystem
|
69
|
*/
|
70
|
public function disk($name = null)
|
71
|
{
|
72
|
$name = $name ?: $this->getDefaultDriver();
|
73
|
|
74
|
return $this->disks[$name] = $this->get($name);
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Get a default cloud filesystem instance.
|
79
|
*
|
80
|
* @return \Illuminate\Contracts\Filesystem\Filesystem
|
81
|
*/
|
82
|
public function cloud()
|
83
|
{
|
84
|
$name = $this->getDefaultCloudDriver();
|
85
|
|
86
|
return $this->disks[$name] = $this->get($name);
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Attempt to get the disk from the local cache.
|
91
|
*
|
92
|
* @param string $name
|
93
|
* @return \Illuminate\Contracts\Filesystem\Filesystem
|
94
|
*/
|
95
|
protected function get($name)
|
96
|
{
|
97
|
return isset($this->disks[$name]) ? $this->disks[$name] : $this->resolve($name);
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Resolve the given disk.
|
102
|
*
|
103
|
* @param string $name
|
104
|
* @return \Illuminate\Contracts\Filesystem\Filesystem
|
105
|
*
|
106
|
* @throws \InvalidArgumentException
|
107
|
*/
|
108
|
protected function resolve($name)
|
109
|
{
|
110
|
$config = $this->getConfig($name);
|
111
|
|
112
|
if (isset($this->customCreators[$config['driver']])) {
|
113
|
return $this->callCustomCreator($config);
|
114
|
}
|
115
|
|
116
|
$driverMethod = 'create'.ucfirst($config['driver']).'Driver';
|
117
|
|
118
|
if (method_exists($this, $driverMethod)) {
|
119
|
return $this->{$driverMethod}($config);
|
120
|
} else {
|
121
|
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
|
122
|
}
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Call a custom driver creator.
|
127
|
*
|
128
|
* @param array $config
|
129
|
* @return \Illuminate\Contracts\Filesystem\Filesystem
|
130
|
*/
|
131
|
protected function callCustomCreator(array $config)
|
132
|
{
|
133
|
$driver = $this->customCreators[$config['driver']]($this->app, $config);
|
134
|
|
135
|
if ($driver instanceof FilesystemInterface) {
|
136
|
return $this->adapt($driver);
|
137
|
}
|
138
|
|
139
|
return $driver;
|
140
|
}
|
141
|
|
142
|
/**
|
143
|
* Create an instance of the local driver.
|
144
|
*
|
145
|
* @param array $config
|
146
|
* @return \Illuminate\Contracts\Filesystem\Filesystem
|
147
|
*/
|
148
|
public function createLocalDriver(array $config)
|
149
|
{
|
150
|
$permissions = isset($config['permissions']) ? $config['permissions'] : [];
|
151
|
|
152
|
$links = Arr::get($config, 'links') === 'skip'
|
153
|
? LocalAdapter::SKIP_LINKS
|
154
|
: LocalAdapter::DISALLOW_LINKS;
|
155
|
|
156
|
return $this->adapt($this->createFlysystem(new LocalAdapter(
|
157
|
$config['root'], LOCK_EX, $links, $permissions
|
158
|
), $config));
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* Create an instance of the ftp driver.
|
163
|
*
|
164
|
* @param array $config
|
165
|
* @return \Illuminate\Contracts\Filesystem\Filesystem
|
166
|
*/
|
167
|
public function createFtpDriver(array $config)
|
168
|
{
|
169
|
$ftpConfig = Arr::only($config, [
|
170
|
'host', 'username', 'password', 'port', 'root', 'passive', 'ssl', 'timeout',
|
171
|
]);
|
172
|
|
173
|
return $this->adapt($this->createFlysystem(
|
174
|
new FtpAdapter($ftpConfig), $config
|
175
|
));
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Create an instance of the Amazon S3 driver.
|
180
|
*
|
181
|
* @param array $config
|
182
|
* @return \Illuminate\Contracts\Filesystem\Cloud
|
183
|
*/
|
184
|
public function createS3Driver(array $config)
|
185
|
{
|
186
|
$s3Config = $this->formatS3Config($config);
|
187
|
|
188
|
$root = isset($s3Config['root']) ? $s3Config['root'] : null;
|
189
|
|
190
|
$options = isset($config['options']) ? $config['options'] : [];
|
191
|
|
192
|
return $this->adapt($this->createFlysystem(
|
193
|
new S3Adapter(new S3Client($s3Config), $s3Config['bucket'], $root, $options), $config
|
194
|
));
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* Format the given S3 configuration with the default options.
|
199
|
*
|
200
|
* @param array $config
|
201
|
* @return array
|
202
|
*/
|
203
|
protected function formatS3Config(array $config)
|
204
|
{
|
205
|
$config += ['version' => 'latest'];
|
206
|
|
207
|
if ($config['key'] && $config['secret']) {
|
208
|
$config['credentials'] = Arr::only($config, ['key', 'secret']);
|
209
|
}
|
210
|
|
211
|
return $config;
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* Create an instance of the Rackspace driver.
|
216
|
*
|
217
|
* @param array $config
|
218
|
* @return \Illuminate\Contracts\Filesystem\Cloud
|
219
|
*/
|
220
|
public function createRackspaceDriver(array $config)
|
221
|
{
|
222
|
$client = new Rackspace($config['endpoint'], [
|
223
|
'username' => $config['username'], 'apiKey' => $config['key'],
|
224
|
]);
|
225
|
|
226
|
$root = isset($config['root']) ? $config['root'] : null;
|
227
|
|
228
|
return $this->adapt($this->createFlysystem(
|
229
|
new RackspaceAdapter($this->getRackspaceContainer($client, $config), $root), $config
|
230
|
));
|
231
|
}
|
232
|
|
233
|
/**
|
234
|
* Get the Rackspace Cloud Files container.
|
235
|
*
|
236
|
* @param \OpenCloud\Rackspace $client
|
237
|
* @param array $config
|
238
|
* @return \OpenCloud\ObjectStore\Resource\Container
|
239
|
*/
|
240
|
protected function getRackspaceContainer(Rackspace $client, array $config)
|
241
|
{
|
242
|
$urlType = Arr::get($config, 'url_type');
|
243
|
|
244
|
$store = $client->objectStoreService('cloudFiles', $config['region'], $urlType);
|
245
|
|
246
|
return $store->getContainer($config['container']);
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
* Create a Flysystem instance with the given adapter.
|
251
|
*
|
252
|
* @param \League\Flysystem\AdapterInterface $adapter
|
253
|
* @param array $config
|
254
|
* @return \League\Flysystem\FlysystemInterface
|
255
|
*/
|
256
|
protected function createFlysystem(AdapterInterface $adapter, array $config)
|
257
|
{
|
258
|
$config = Arr::only($config, ['visibility', 'disable_asserts']);
|
259
|
|
260
|
return new Flysystem($adapter, count($config) > 0 ? $config : null);
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* Adapt the filesystem implementation.
|
265
|
*
|
266
|
* @param \League\Flysystem\FilesystemInterface $filesystem
|
267
|
* @return \Illuminate\Contracts\Filesystem\Filesystem
|
268
|
*/
|
269
|
protected function adapt(FilesystemInterface $filesystem)
|
270
|
{
|
271
|
return new FilesystemAdapter($filesystem);
|
272
|
}
|
273
|
|
274
|
/**
|
275
|
* Get the filesystem connection configuration.
|
276
|
*
|
277
|
* @param string $name
|
278
|
* @return array
|
279
|
*/
|
280
|
protected function getConfig($name)
|
281
|
{
|
282
|
return $this->app['config']["filesystems.disks.{$name}"];
|
283
|
}
|
284
|
|
285
|
/**
|
286
|
* Get the default driver name.
|
287
|
*
|
288
|
* @return string
|
289
|
*/
|
290
|
public function getDefaultDriver()
|
291
|
{
|
292
|
return $this->app['config']['filesystems.default'];
|
293
|
}
|
294
|
|
295
|
/**
|
296
|
* Get the default cloud driver name.
|
297
|
*
|
298
|
* @return string
|
299
|
*/
|
300
|
public function getDefaultCloudDriver()
|
301
|
{
|
302
|
return $this->app['config']['filesystems.cloud'];
|
303
|
}
|
304
|
|
305
|
/**
|
306
|
* Register a custom driver creator Closure.
|
307
|
*
|
308
|
* @param string $driver
|
309
|
* @param \Closure $callback
|
310
|
* @return $this
|
311
|
*/
|
312
|
public function extend($driver, Closure $callback)
|
313
|
{
|
314
|
$this->customCreators[$driver] = $callback;
|
315
|
|
316
|
return $this;
|
317
|
}
|
318
|
|
319
|
/**
|
320
|
* Dynamically call the default driver instance.
|
321
|
*
|
322
|
* @param string $method
|
323
|
* @param array $parameters
|
324
|
* @return mixed
|
325
|
*/
|
326
|
public function __call($method, $parameters)
|
327
|
{
|
328
|
return call_user_func_array([$this->disk(), $method], $parameters);
|
329
|
}
|
330
|
}
|