|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Filesystem; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use League\Flysystem\Adapter\Local as LocalAdapter; |
|
7
|
|
|
use League\Flysystem\AdapterInterface; |
|
8
|
|
|
use League\Flysystem\FilesystemInterface; |
|
9
|
|
|
use Nip\Config\Config; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class FilesystemManager |
|
13
|
|
|
* @package Nip\Filesystem |
|
14
|
|
|
*/ |
|
15
|
|
|
class FilesystemManager |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The application instance. |
|
19
|
|
|
* |
|
20
|
|
|
* @var \Nip\Application |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $app; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The array of resolved filesystem drivers. |
|
26
|
|
|
* |
|
27
|
|
|
* @var FileDisk[] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $disks = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The registered custom driver creators. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $customCreators = []; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a new filesystem manager instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Nip\Application $app |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($app) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->app = $app; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get a filesystem instance. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @return FileDisk |
|
54
|
|
|
*/ |
|
55
|
|
|
public function disk($name = null) |
|
56
|
|
|
{ |
|
57
|
|
|
$name = $name ?: $this->getDefaultDriver(); |
|
58
|
|
|
return $this->disks[$name] = $this->get($name); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the default driver name. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getDefaultDriver() |
|
67
|
|
|
{ |
|
68
|
|
|
return config('filesystems.default'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Attempt to get the disk from the local cache. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $name |
|
75
|
|
|
* @return FileDisk |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function get($name) |
|
78
|
|
|
{ |
|
79
|
|
|
return isset($this->disks[$name]) ? $this->disks[$name] : $this->resolve($name); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Resolve the given disk. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $name |
|
86
|
|
|
* @return FileDisk |
|
87
|
|
|
* |
|
88
|
|
|
* @throws \InvalidArgumentException |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function resolve($name) |
|
91
|
|
|
{ |
|
92
|
|
|
$config = $this->getConfig($name)->toArray(); |
|
93
|
|
|
|
|
94
|
|
|
if (isset($this->customCreators[$config['driver']])) { |
|
95
|
|
|
return $this->callCustomCreator($config); |
|
96
|
|
|
} |
|
97
|
|
|
$driverMethod = 'create' . ucfirst($config['driver']) . 'Driver'; |
|
98
|
|
|
if (method_exists($this, $driverMethod)) { |
|
99
|
|
|
return $this->{$driverMethod}($config); |
|
100
|
|
|
} else { |
|
101
|
|
|
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get the filesystem connection configuration. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $name |
|
109
|
|
|
* @return Config |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getConfig($name) |
|
112
|
|
|
{ |
|
113
|
|
|
return config("filesystems.disks.{$name}"); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Call a custom driver creator. |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $config |
|
120
|
|
|
* @return FileDisk |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function callCustomCreator(array $config) |
|
123
|
|
|
{ |
|
124
|
|
|
$driver = $this->customCreators[$config['driver']]($this->app, $config); |
|
125
|
|
|
if ($driver instanceof FilesystemInterface) { |
|
126
|
|
|
return $this->adapt($driver); |
|
127
|
|
|
} |
|
128
|
|
|
return $driver; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Adapt the filesystem implementation. |
|
133
|
|
|
* |
|
134
|
|
|
* @param \League\Flysystem\FilesystemInterface $filesystem |
|
135
|
|
|
* @return \League\Flysystem\FilesystemInterface|FileDisk |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function adapt(FilesystemInterface $filesystem) |
|
138
|
|
|
{ |
|
139
|
|
|
return $filesystem; |
|
140
|
|
|
// return new FlysystemAdapter($filesystem); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Create an instance of the local driver. |
|
145
|
|
|
* |
|
146
|
|
|
* @param array $config |
|
147
|
|
|
* @return \League\Flysystem\FilesystemInterface |
|
148
|
|
|
*/ |
|
149
|
|
|
public function createLocalDriver($config) |
|
150
|
|
|
{ |
|
151
|
|
|
$permissions = isset($config['permissions']) ? $config['permissions'] : []; |
|
152
|
|
|
$links = []; |
|
153
|
|
|
// $links = Arr::get($config, 'links') === 'skip' |
|
|
|
|
|
|
154
|
|
|
// ? LocalAdapter::SKIP_LINKS |
|
155
|
|
|
// : LocalAdapter::DISALLOW_LINKS; |
|
156
|
|
|
|
|
157
|
|
|
return $this->adapt( |
|
158
|
|
|
$this->createDisk( |
|
159
|
|
|
new LocalAdapter( |
|
160
|
|
|
$config['root'], |
|
161
|
|
|
LOCK_EX, |
|
162
|
|
|
$links, |
|
|
|
|
|
|
163
|
|
|
$permissions |
|
164
|
|
|
), |
|
165
|
|
|
$config |
|
166
|
|
|
) |
|
167
|
|
|
); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Create a Flysystem instance with the given adapter. |
|
172
|
|
|
* |
|
173
|
|
|
* @param \League\Flysystem\AdapterInterface $adapter |
|
174
|
|
|
* @param array $config |
|
175
|
|
|
* @return FileDisk |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function createDisk(AdapterInterface $adapter, $config) |
|
178
|
|
|
{ |
|
179
|
|
|
// $config = Arr::only($config, ['visibility', 'disable_asserts', 'url']); |
|
|
|
|
|
|
180
|
|
|
return new FileDisk($adapter, count($config) > 0 ? $config : null); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Set the given disk instance. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $name |
|
187
|
|
|
* @param FileDisk $disk |
|
188
|
|
|
* @return void |
|
189
|
|
|
*/ |
|
190
|
|
|
public function set($name, $disk) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->disks[$name] = $disk; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Get the default cloud driver name. |
|
197
|
|
|
* |
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getDefaultCloudDriver() |
|
201
|
|
|
{ |
|
202
|
|
|
return config('filesystems.cloud'); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.