-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathStorage.php
More file actions
223 lines (191 loc) · 5.73 KB
/
Copy pathStorage.php
File metadata and controls
223 lines (191 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
declare(strict_types=1);
namespace Bow\Storage;
use BadMethodCallException;
use Bow\Storage\Contracts\FilesystemInterface;
use Bow\Storage\Exception\DiskNotFoundException;
use Bow\Storage\Exception\ServiceConfigurationNotFoundException;
use Bow\Storage\Exception\ServiceNotFoundException;
use Bow\Storage\Service\DiskFilesystemService;
use Bow\Storage\Service\FTPService;
use Bow\Storage\Service\S3Service;
use ErrorException;
use InvalidArgumentException;
class Storage
{
/**
* The data configuration
*
* @var array
*/
private static array $config = [];
/**
* The disk mounting
*
* @var ?DiskFilesystemService
*/
private static ?DiskFilesystemService $disk = null;
/**
* The service lists
*
* @var array
*/
private static array $available_services_drivers = [
'ftp' => FTPService::class,
's3' => S3Service::class,
];
/**
* Mount service
*
* @param string $service
* @return FTPService|S3Service
* @throws ServiceConfigurationNotFoundException
* @throws ServiceNotFoundException
*/
public static function service(string $service): S3Service|FTPService
{
$config = static::$config['services'][$service] ?? null;
if (is_null($config)) {
throw (new ServiceConfigurationNotFoundException(
sprintf(
'"%s" configuration not found.',
$service
)
))->setService($service);
}
$driver = $config["driver"] ?? null;
if (is_null($driver)) {
throw (new ServiceNotFoundException(
sprintf(
'"%s" driver is not support.',
$driver
)
))->setService($service);
}
if (!array_key_exists($driver, self::$available_services_drivers)) {
throw (new ServiceNotFoundException(
sprintf(
'"%s" is not registered as a service.',
$driver
)
))->setService($service);
}
$service_class = static::$available_services_drivers[$driver];
return $service_class::configure($config);
}
/**
* Configure Storage
*
* @param array $config
* @return FilesystemInterface
* @throws
*/
public static function configure(array $config): FilesystemInterface
{
static::$config = $config;
if (is_null(static::$disk)) {
static::$disk = static::local($config['disk']['mount']);
}
return static::$disk;
}
/**
* Mount disk
*
* @param string|null $disk
*
* @return DiskFilesystemService
* @throws DiskNotFoundException
*/
public static function local(?string $disk = null): DiskFilesystemService
{
// Use the default disk as fallback
if (is_null($disk)) {
if (!is_null(static::$disk)) {
return static::$disk;
}
$disk = static::$config['disk']['mount'];
}
if (!isset(static::$config['disk']['path'][$disk])) {
throw new DiskNotFoundException('The ' . $disk . ' disk is not define.');
}
$config = static::$config['disk']['path'][$disk];
if (is_null($config)) {
throw new DiskNotFoundException('The ' . $disk . ' disk is not define.');
}
if (!is_dir($config)) {
// Try to create the directory
if (!mkdir($config, 0755, true)) {
throw new DiskNotFoundException('The ' . $disk . ' disk directory does not exist.');
}
}
return static::$disk = new DiskFilesystemService($config);
}
/**
* Mount disk
*
* @param string|null $disk
* @return DiskFilesystemService
* @throws DiskNotFoundException
*/
public static function disk(?string $disk = null): DiskFilesystemService
{
return static::local($disk);
}
/**
* Push a new service who implement
* the Bow\Storage\Contracts\ServiceInterface
*
* @param array $drivers
*/
public static function pushService(array $drivers): void
{
foreach ($drivers as $driver => $handler) {
if (isset(static::$available_services_drivers[$driver])) {
throw new InvalidArgumentException("The $driver is already define");
}
static::$available_services_drivers[$driver] = $handler;
}
}
/**
* __callStatic
*
* @param string $name
* @param array $arguments
* @return mixed
* @throws ErrorException
*/
public static function __callStatic(string $name, array $arguments)
{
if (is_null(static::$disk)) {
throw new ErrorException(
"Unable to get storage instance before configuration"
);
}
if (method_exists(static::$disk, $name)) {
return call_user_func_array([static::$disk, $name], $arguments);
}
throw new BadMethodCallException(
"The method $name is not defined"
);
}
/**
* __call
*
* @param string $name
* @param array $arguments
* @return mixed
* @throws ErrorException
*/
public function __call(string $name, array $arguments = [])
{
if (is_null(static::$disk)) {
throw new ErrorException(
"Unable to get storage instance before configuration"
);
}
if (method_exists(static::$disk, $name)) {
return call_user_func_array([static::$disk, $name], $arguments);
}
throw new BadMethodCallException("unkdown $name method");
}
}