3.0.0
This commit is contained in:
parent
8ccfce7f65
commit
917c25c99b
|
|
@ -31,7 +31,7 @@ if (file_exists($autoload)) {
|
|||
}
|
||||
|
||||
use FlysystemOffload\Plugin;
|
||||
use Throwable;
|
||||
// use Throwable;
|
||||
|
||||
add_action('plugins_loaded', static function (): void {
|
||||
if (! class_exists(Plugin::class)) {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ namespace FlysystemOffload\Filesystem\Adapters;
|
|||
|
||||
use InvalidArgumentException;
|
||||
use League\Flysystem\FilesystemAdapter;
|
||||
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
|
||||
use League\Flysystem\Visibility;
|
||||
use League\Flysystem\WebDAV\WebDAVAdapter;
|
||||
use Sabre\DAV\Client;
|
||||
|
||||
|
|
@ -13,20 +15,20 @@ final class WebdavAdapter
|
|||
{
|
||||
public function createAdapter(array $config): FilesystemAdapter
|
||||
{
|
||||
$webdav = $config['webdav'] ?? null;
|
||||
|
||||
if (!is_array($webdav)) {
|
||||
if (!isset($config['webdav']) || !is_array($config['webdav'])) {
|
||||
throw new InvalidArgumentException('La configuración de WebDAV no está definida.');
|
||||
}
|
||||
|
||||
$endpoint = $this->requiredString($webdav, 'endpoint');
|
||||
$webdav = $config['webdav'];
|
||||
|
||||
$endpoint = $this->requiredString($webdav, 'endpoint', 'webdav.endpoint');
|
||||
$credentials = $webdav['credentials'] ?? [];
|
||||
|
||||
$username = $this->requiredString($credentials, 'username', 'webdav.credentials.username');
|
||||
$password = $this->requiredString($credentials, 'password', 'webdav.credentials.password');
|
||||
|
||||
$clientConfig = [
|
||||
'baseUri' => $this->normaliseEndpoint($endpoint),
|
||||
'baseUri' => $this->normaliseEndpoint($endpoint),
|
||||
'userName' => $username,
|
||||
'password' => $password,
|
||||
];
|
||||
|
|
@ -36,6 +38,18 @@ final class WebdavAdapter
|
|||
$clientConfig['authType'] = $authType;
|
||||
}
|
||||
|
||||
if (!empty($webdav['default_headers']) && is_array($webdav['default_headers'])) {
|
||||
$clientConfig['headers'] = $this->normaliseHeaders($webdav['default_headers']);
|
||||
}
|
||||
|
||||
if (!empty($webdav['timeout'])) {
|
||||
$clientConfig['timeout'] = (int) $webdav['timeout'];
|
||||
}
|
||||
|
||||
if (!empty($webdav['curl_options']) && is_array($webdav['curl_options'])) {
|
||||
$clientConfig['curl.options'] = $webdav['curl_options'];
|
||||
}
|
||||
|
||||
$client = new Client($clientConfig);
|
||||
|
||||
$prefix = '';
|
||||
|
|
@ -43,14 +57,20 @@ final class WebdavAdapter
|
|||
$prefix = trim((string) $webdav['prefix'], '/');
|
||||
}
|
||||
|
||||
return new WebDAVAdapter($client, $prefix);
|
||||
$defaultVisibility = $webdav['default_visibility']
|
||||
?? ($config['visibility'] ?? Visibility::PRIVATE);
|
||||
|
||||
$visibility = $this->normaliseVisibility($defaultVisibility);
|
||||
$visibilityConverter = PortableVisibilityConverter::fromArray([], $visibility);
|
||||
|
||||
return new WebDAVAdapter($client, $prefix, $visibilityConverter);
|
||||
}
|
||||
|
||||
private function requiredString(array $config, string $key, ?string $path = null): string
|
||||
{
|
||||
$value = $config[$key] ?? null;
|
||||
|
||||
if (!is_string($value) || $value === '') {
|
||||
if (!is_string($value) || trim($value) === '') {
|
||||
$path ??= $key;
|
||||
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
|
|
@ -64,6 +84,28 @@ final class WebdavAdapter
|
|||
|
||||
private function normaliseEndpoint(string $endpoint): string
|
||||
{
|
||||
return rtrim($endpoint, '/') . '/';
|
||||
return rtrim(trim($endpoint), '/') . '/';
|
||||
}
|
||||
|
||||
private function normaliseHeaders(array $headers): array
|
||||
{
|
||||
$normalised = [];
|
||||
|
||||
foreach ($headers as $header => $value) {
|
||||
if (!is_string($header) || $header === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$normalised[$header] = is_array($value) ? implode(',', $value) : (string) $value;
|
||||
}
|
||||
|
||||
return $normalised;
|
||||
}
|
||||
|
||||
private function normaliseVisibility(string $visibility): string
|
||||
{
|
||||
return strtolower($visibility) === Visibility::PUBLIC
|
||||
? Visibility::PUBLIC
|
||||
: Visibility::PRIVATE;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue