ldc-file-storage-service/src/StorageServiceProvider.php

119 lines
3.9 KiB
PHP
Raw Normal View History

2026-11-03 12:26:17 +01:00
<?php
declare(strict_types=1);
namespace Uwil\Storage;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Arr;
2026-11-03 12:26:17 +01:00
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
use Psr\Log\LoggerInterface;
use Uwil\Storage\Config\StorageServiceConfig;
use Uwil\Storage\Contracts\FileSanitizer;
use Uwil\Storage\Contracts\MimeTypeChecker;
use Uwil\Storage\Contracts\StorageGateway;
use Uwil\Storage\Contracts\StorageService;
use Uwil\Storage\Lib\FileSanitizerImpl;
use Uwil\Storage\Lib\MimeTypeCheckerImpl;
use Uwil\Storage\Lib\PathGenerators\DateHashPathGenerator;
use Uwil\Storage\Lib\PathGenerators\UuidPathGenerator;
use Uwil\Storage\Lib\StorageGatewayImpl;
use Uwil\Storage\Lib\StorageServiceImpl;
class StorageServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->publishes([
__DIR__ . '/../config/storage-service.php' => config_path('storage-service.php'),
], 'config');
// * Set filter condition
$this->app->singleton(
StorageServiceConfig::class,
function (Application $_) {
/** @var array<string, array<string, mixed>> $rawDisks */
$rawDisks = array_filter(
Config::array('filesystems.disks', []),
fn ($diskConf) => is_array($diskConf)
);
/** @var array<int, string> $eligibleDisks */
$eligibleDisks = array_filter(
Config::array('storage-service.eligibleDisks', []),
fn ($disk) => is_string($disk)
);
/** @var array<int, string> $allowedExtensions */
$allowedExtensions = array_filter(
Config::array('storage-service.allowedExtensions', []),
fn ($ext) => is_string($ext)
);
$rawPrefix = Config::get('storage-service.prefix');
$prefix = is_string($rawPrefix) ? $rawPrefix : null;
2026-11-03 12:26:17 +01:00
$disks = [];
foreach ($rawDisks as $name => $diskConf) {
$disks[] = (object) [
'disk' => (string) $name,
'driver' => Arr::string($diskConf, 'driver'),
2026-11-03 12:26:17 +01:00
];
}
return new StorageServiceConfig(
Config::string('storage-service.default', 'local'),
$eligibleDisks,
2026-11-03 12:26:17 +01:00
$disks,
$allowedExtensions,
$prefix
2026-11-03 12:26:17 +01:00
);
}
);
$this->app->bind(MimeTypeChecker::class, MimeTypeCheckerImpl::class);
$this->app->bind(FileSanitizer::class, FileSanitizerImpl::class);
$this->app->bind(
DateHashPathGenerator::class,
fn (Application $app) => new DateHashPathGenerator(
$app->make(StorageServiceConfig::class)
)
);
$this->app->bind(
UuidPathGenerator::class,
fn (Application $app) => new UuidPathGenerator(
$app->make(StorageServiceConfig::class)
)
);
2026-11-03 12:26:17 +01:00
$this->app->bind(
StorageGateway::class,
fn (Application $app) => new StorageGatewayImpl(
$app->make(LoggerInterface::class),
$app->make(DateHashPathGenerator::class),
$app->make(UuidPathGenerator::class),
)
);
$this->app->bind(
StorageService::class,
fn (Application $app) => new StorageServiceImpl(
$app->make(StorageServiceConfig::class),
$app->make(FileSanitizer::class),
$app->make(StorageGateway::class)
)
);
}
public function register(): void
{
$this->mergeConfigFrom(
sprintf('%s/../config/storage-service.php', __DIR__),
'storage-service'
);
}
}