1 | <?php |
||
2 | |||
3 | namespace Nip\Filesystem; |
||
4 | |||
5 | use InvalidArgumentException; |
||
6 | use League\Flysystem\Local\LocalFilesystemAdapter as LocalAdapter; |
||
7 | use League\Flysystem\FilesystemAdapter; |
||
8 | use League\Flysystem\FilesystemOperator; |
||
9 | use Nip\Config\Config; |
||
10 | |||
11 | /** |
||
12 | * Class FilesystemManager |
||
13 | * @package Nip\Filesystem |
||
14 | */ |
||
15 | class FilesystemManager |
||
16 | { |
||
17 | use FilesystemManager\HasCloudDriverTrait; |
||
18 | use FilesystemManager\HasDisksTrait; |
||
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 | public function __construct($app = null) |
||
41 | 4 | { |
|
42 | if ($app) { |
||
43 | 4 | $this->app = $app; |
|
44 | } |
||
45 | } |
||
46 | 4 | ||
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 | 1 | { |
|
57 | $config = $this->getConfig($name); |
||
58 | 1 | if (empty($config)) { |
|
59 | 1 | throw new InvalidArgumentException("No configuration found for Disk [{$name}]."); |
|
60 | } |
||
61 | |||
62 | if (is_string($config)) { |
||
63 | 1 | return $this->get($config); |
|
64 | 1 | } |
|
65 | |||
66 | if (isset($this->customCreators[$config['driver']])) { |
||
67 | 1 | return $this->callCustomCreator($config); |
|
68 | } |
||
69 | $driverMethod = 'create'.ucfirst($config['driver']).'Driver'; |
||
70 | 1 | if (method_exists($this, $driverMethod)) { |
|
71 | 1 | return $this->{$driverMethod}($config); |
|
72 | 1 | } else { |
|
73 | throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Get the filesystem connection configuration. |
||
79 | * |
||
80 | * @param string $name |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function getConfig($name) |
||
84 | 1 | { |
|
85 | if (!function_exists('config')) { |
||
86 | 1 | return null; |
|
87 | } |
||
88 | $config = config(); |
||
89 | 1 | $configName = "filesystems.disks.{$name}"; |
|
90 | 1 | if (!$config->has($configName)) { |
|
91 | 1 | return null; |
|
92 | } |
||
93 | |||
94 | $value = $config->get($configName); |
||
95 | 1 | ||
96 | return $value instanceof Config ? $value->toArray() : $value; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
97 | 1 | } |
|
98 | |||
99 | /** |
||
100 | * Call a custom driver creator. |
||
101 | * |
||
102 | * @param array $config |
||
103 | * @return FileDisk |
||
104 | */ |
||
105 | protected function callCustomCreator(array $config) |
||
106 | { |
||
107 | $driver = $this->customCreators[$config['driver']]($this->app, $config); |
||
108 | if ($driver instanceof FilesystemOperator) { |
||
109 | return $this->adapt($driver); |
||
110 | } |
||
111 | |||
112 | return $driver; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Adapt the filesystem implementation. |
||
117 | * |
||
118 | * @param \League\Flysystem\FilesystemOperator $filesystem |
||
119 | * @return \League\Flysystem\FilesystemOperator|FileDisk |
||
120 | */ |
||
121 | protected function adapt(FilesystemOperator $filesystem) |
||
122 | 3 | { |
|
123 | return $filesystem; |
||
124 | 3 | // return new FlysystemAdapter($filesystem); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Create an instance of the local driver. |
||
129 | * |
||
130 | * @param array $config |
||
131 | * @return \League\Flysystem\FilesystemOperator |
||
132 | */ |
||
133 | public function createLocalDriver($config) |
||
134 | 3 | { |
|
135 | $permissions = $config['permissions'] ?? []; |
||
136 | 3 | $links = []; |
|
137 | 3 | // $links = Arr::get($config, 'links') === 'skip' |
|
138 | // ? LocalAdapter::SKIP_LINKS |
||
139 | // : LocalAdapter::DISALLOW_LINKS; |
||
140 | |||
141 | return $this->adapt( |
||
142 | 3 | $this->createDisk( |
|
143 | 3 | new LocalAdapter( |
|
144 | 3 | $config['root'], |
|
145 | 3 | null, |
|
146 | 3 | LOCK_EX |
|
147 | 3 | // $links, |
|
148 | 3 | // $permissions |
|
149 | ), |
||
150 | 3 | $config |
|
151 | ) |
||
152 | ); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Create a Flysystem instance with the given adapter. |
||
157 | * |
||
158 | * @param \League\Flysystem\FilesystemAdapter $adapter |
||
159 | * @param array $config |
||
160 | * @return FileDisk |
||
161 | */ |
||
162 | 3 | protected function createDisk(FilesystemAdapter $adapter, $config) |
|
163 | { |
||
164 | // $config = Arr::only($config, ['visibility', 'disable_asserts', 'url']); |
||
165 | // $this->checkForCacheNeeded($adapter, $config); |
||
166 | 3 | ||
167 | return new FileDisk($adapter, count($config) > 0 ? $config : null); |
||
168 | 3 | } |
|
169 | |||
170 | |||
171 | /** |
||
172 | * Set the given disk instance. |
||
173 | * |
||
174 | * @param string $name |
||
175 | * @param FileDisk $disk |
||
176 | * @return void |
||
177 | */ |
||
178 | public function set($name, $disk) |
||
179 | { |
||
180 | $this->disks[$name] = $disk; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get the default cloud driver name. |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | public function getDefaultCloudDriver() |
||
189 | { |
||
190 | return config('filesystems.cloud'); |
||
0 ignored issues
–
show
|
|||
191 | } |
||
192 | } |
||
193 |