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
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class FilesystemManager |
12
|
|
|
* @package Nip\Filesystem |
13
|
|
|
*/ |
14
|
|
|
class FilesystemManager |
15
|
|
|
{ |
16
|
|
|
use FilesystemManager\HasCloudDriverTrait; |
17
|
|
|
use FilesystemManager\HasDisksTrait; |
18
|
|
|
use FilesystemManager\HasCacheStoreTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The application instance. |
22
|
|
|
* |
23
|
|
|
* @var \Nip\Application |
24
|
|
|
*/ |
25
|
|
|
protected $app; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The registered custom driver creators. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $customCreators = []; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a new filesystem manager instance. |
37
|
|
|
* |
38
|
|
|
* @param \Nip\Application $app |
39
|
|
|
*/ |
40
|
2 |
|
public function __construct($app = null) |
41
|
|
|
{ |
42
|
2 |
|
if ($app) { |
43
|
|
|
$this->app = $app; |
44
|
|
|
} |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Resolve the given disk. |
49
|
|
|
* |
50
|
|
|
* @param string $name |
51
|
|
|
* @return FileDisk |
52
|
|
|
* |
53
|
|
|
* @throws \InvalidArgumentException |
54
|
|
|
*/ |
55
|
|
|
protected function resolve($name) |
56
|
|
|
{ |
57
|
|
|
$config = $this->getConfig($name); |
58
|
|
|
if (empty($config)) { |
59
|
|
|
throw new InvalidArgumentException("No configuration found for Disk [{$name}]."); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (isset($this->customCreators[$config['driver']])) { |
63
|
|
|
return $this->callCustomCreator($config); |
64
|
|
|
} |
65
|
|
|
$driverMethod = 'create'.ucfirst($config['driver']).'Driver'; |
66
|
|
|
if (method_exists($this, $driverMethod)) { |
67
|
|
|
return $this->{$driverMethod}($config); |
68
|
|
|
} else { |
69
|
|
|
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the filesystem connection configuration. |
75
|
|
|
* |
76
|
|
|
* @param string $name |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
protected function getConfig($name) |
80
|
|
|
{ |
81
|
|
|
if (!function_exists('config')) { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
$config = config(); |
85
|
|
|
$configName = "filesystems.disks.{$name}"; |
86
|
|
|
if (!$config->has($configName)) { |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $config->get($configName)->toArray(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Call a custom driver creator. |
95
|
|
|
* |
96
|
|
|
* @param array $config |
97
|
|
|
* @return FileDisk |
98
|
|
|
*/ |
99
|
|
|
protected function callCustomCreator(array $config) |
100
|
|
|
{ |
101
|
|
|
$driver = $this->customCreators[$config['driver']]($this->app, $config); |
102
|
|
|
if ($driver instanceof FilesystemInterface) { |
103
|
|
|
return $this->adapt($driver); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $driver; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Adapt the filesystem implementation. |
111
|
|
|
* |
112
|
|
|
* @param \League\Flysystem\FilesystemInterface $filesystem |
113
|
|
|
* @return \League\Flysystem\FilesystemInterface|FileDisk |
114
|
|
|
*/ |
115
|
2 |
|
protected function adapt(FilesystemInterface $filesystem) |
116
|
|
|
{ |
117
|
2 |
|
return $filesystem; |
118
|
|
|
// return new FlysystemAdapter($filesystem); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Create an instance of the local driver. |
123
|
|
|
* |
124
|
|
|
* @param array $config |
125
|
|
|
* @return \League\Flysystem\FilesystemInterface |
126
|
|
|
*/ |
127
|
2 |
|
public function createLocalDriver($config) |
128
|
|
|
{ |
129
|
2 |
|
$permissions = isset($config['permissions']) ? $config['permissions'] : []; |
130
|
2 |
|
$links = []; |
131
|
|
|
// $links = Arr::get($config, 'links') === 'skip' |
132
|
|
|
// ? LocalAdapter::SKIP_LINKS |
133
|
|
|
// : LocalAdapter::DISALLOW_LINKS; |
134
|
|
|
|
135
|
2 |
|
return $this->adapt( |
136
|
2 |
|
$this->createDisk( |
137
|
2 |
|
new LocalAdapter( |
138
|
2 |
|
$config['root'], |
139
|
2 |
|
LOCK_EX, |
140
|
2 |
|
$links, |
|
|
|
|
141
|
2 |
|
$permissions |
142
|
|
|
), |
143
|
2 |
|
$config |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Create a Flysystem instance with the given adapter. |
150
|
|
|
* |
151
|
|
|
* @param \League\Flysystem\AdapterInterface $adapter |
152
|
|
|
* @param array $config |
153
|
|
|
* @return FileDisk |
154
|
|
|
*/ |
155
|
2 |
|
protected function createDisk(AdapterInterface $adapter, $config) |
156
|
|
|
{ |
157
|
|
|
// $config = Arr::only($config, ['visibility', 'disable_asserts', 'url']); |
158
|
|
|
|
159
|
2 |
|
$this->checkForCacheNeeded($adapter, $config); |
160
|
|
|
|
161
|
2 |
|
return new FileDisk($adapter, count($config) > 0 ? $config : null); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set the given disk instance. |
167
|
|
|
* |
168
|
|
|
* @param string $name |
169
|
|
|
* @param FileDisk $disk |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
|
|
public function set($name, $disk) |
173
|
|
|
{ |
174
|
|
|
$this->disks[$name] = $disk; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the default cloud driver name. |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getDefaultCloudDriver() |
183
|
|
|
{ |
184
|
|
|
return config('filesystems.cloud'); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: