85 lines
2.8 KiB
PHP
85 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Uwil\Storage;
|
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
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 $_) {
|
|
$rawDisks = Config::array('filesystems.disks', []);
|
|
$disks = [];
|
|
foreach ($rawDisks as $name => $diskConf) {
|
|
$disks[] = (object) [
|
|
'disk' => (string) $name,
|
|
'driver' => (string) ($diskConf['driver'] ?? ''),
|
|
];
|
|
}
|
|
|
|
return new StorageServiceConfig(
|
|
Config::string('storage-service.default', 'local'),
|
|
Config::array('storage-service.eligibleDisks', []),
|
|
$disks,
|
|
Config::array('storage-service.allowedExtensions', [])
|
|
);
|
|
}
|
|
);
|
|
|
|
$this->app->bind(MimeTypeChecker::class, MimeTypeCheckerImpl::class);
|
|
$this->app->bind(FileSanitizer::class, FileSanitizerImpl::class);
|
|
|
|
$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)
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Register any application services.
|
|
*/
|
|
public function register(): void
|
|
{
|
|
$this->mergeConfigFrom(
|
|
sprintf('%s/../config/storage-service.php', __DIR__),
|
|
'storage-service'
|
|
);
|
|
}
|
|
}
|