1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Lenevor Framework |
5
|
|
|
* |
6
|
|
|
* LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
9
|
|
|
* with this package in the file license.md. |
10
|
|
|
* It is also available through the world-wide-web at this URL: |
11
|
|
|
* https://lenevor.com/license |
12
|
|
|
* If you did not receive a copy of the license and are unable to |
13
|
|
|
* obtain it through the world-wide-web, please send an email |
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
15
|
|
|
* |
16
|
|
|
* @package Lenevor |
17
|
|
|
* @subpackage Base |
18
|
|
|
* @link https://lenevor.com |
19
|
|
|
* @copyright Copyright (c) 2019 - 2023 Alexander Campo <[email protected]> |
20
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Syscodes\Components\Filesystem; |
24
|
|
|
|
25
|
|
|
use InvalidArgumentException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Allows manage the distint adapters of file system. |
29
|
|
|
*/ |
30
|
|
|
class FilesystemManager |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* The application instance. |
34
|
|
|
* |
35
|
|
|
* @var \Syscodes\Components\Contracts\Core\Application $app |
36
|
|
|
*/ |
37
|
|
|
protected $app; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The registered custom driver creators. |
41
|
|
|
* |
42
|
|
|
* @var array $cumtomCreators |
43
|
|
|
*/ |
44
|
|
|
protected $customCreators = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The array of resolved filesystem drivers. |
48
|
|
|
* |
49
|
|
|
* @var array $disks |
50
|
|
|
*/ |
51
|
|
|
protected $disks = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Constructor. Create a new FilesystemManager instance. |
55
|
|
|
* |
56
|
|
|
* @param \Syscodes\Components\Contracts\Core\Application $app |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function __construct($app) |
61
|
|
|
{ |
62
|
|
|
$this->app = $app; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get a filesystem instance. |
67
|
|
|
* |
68
|
|
|
* @param string|null $name |
69
|
|
|
* |
70
|
|
|
* @return \Syscodes\Components\Contracts\Filesystem\Filesystem |
71
|
|
|
*/ |
72
|
|
|
public function disk($name = null) |
73
|
|
|
{ |
74
|
|
|
$name = $name ?: $this->getDefaultDriver(); |
75
|
|
|
|
76
|
|
|
return $this->disks[$name] = $this->get($name); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Attempt to get the disk from the local cache. |
81
|
|
|
* |
82
|
|
|
* @param string $name |
83
|
|
|
* |
84
|
|
|
* @return \Syscodes\Components\Contracts\Filesystem\Filesystem |
85
|
|
|
*/ |
86
|
|
|
protected function get($name) |
87
|
|
|
{ |
88
|
|
|
return $this->disks[$name] ?? $this->resolve($name); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Resolve the given disk. |
93
|
|
|
* |
94
|
|
|
* @param string $name |
95
|
|
|
* @param array|null $config |
96
|
|
|
* |
97
|
|
|
* @return \Syscodes\Components\Contracts\Filesystem\Filesystem |
98
|
|
|
* |
99
|
|
|
* @throws \InvalidArgumentException |
100
|
|
|
*/ |
101
|
|
|
protected function resolve($name, $config = null) |
102
|
|
|
{ |
103
|
|
|
$config ??= $this->getConfig($name); |
104
|
|
|
|
105
|
|
|
if (empty($config['driver'])) { |
106
|
|
|
throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver"); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$name = $config['driver']; |
110
|
|
|
|
111
|
|
|
if (isset($this->customCreators[$name])) { |
112
|
|
|
return $this->callCustomCreator($config); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$driverMethod = 'create'.ucfirst($name).'Driver'; |
116
|
|
|
|
117
|
|
|
if ( ! method_exists($this, $driverMethod)) { |
118
|
|
|
throw new InvalidArgumentException("Driver [{$name}] is not supported"); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->{$driverMethod}($config); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Call a custom driver creator. |
126
|
|
|
* |
127
|
|
|
* @param array $config |
128
|
|
|
* |
129
|
|
|
* @return \Syscodes\Components\Contracts\Filesystem\Filesystem |
130
|
|
|
*/ |
131
|
|
|
protected function callCustomCreator(array $config) |
132
|
|
|
{ |
133
|
|
|
return $this->customCreators[$config['driver']]($this->app, $config); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set the given disk instance. |
138
|
|
|
* |
139
|
|
|
* @param string $name |
140
|
|
|
* @param mixed $disk |
141
|
|
|
* |
142
|
|
|
* @return static |
143
|
|
|
*/ |
144
|
|
|
public function set($name, $disk): static |
145
|
|
|
{ |
146
|
|
|
$this->disks[$name] = $disk; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the filesystem connection configuration. |
153
|
|
|
* |
154
|
|
|
* @param string $name |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
protected function getConfig($name): string |
159
|
|
|
{ |
160
|
|
|
return $this->app['config']["filesystems.disks.{$name}"] ?: []; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the default driver name. |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function getDefaultDriver(): string |
169
|
|
|
{ |
170
|
|
|
return $this->app['config']['filesystems.default']; |
171
|
|
|
} |
172
|
|
|
} |