Framework/src/Providers/Core/StorageServiceProvider.php

44 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Phred\Providers\Core;
use DI\Container;
use DI\ContainerBuilder;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Phred\Support\Contracts\ConfigInterface;
use Phred\Support\Contracts\ServiceProviderInterface;
final class StorageServiceProvider implements ServiceProviderInterface
{
public function register(ContainerBuilder $builder, ConfigInterface $config): void
{
$builder->addDefinitions([
FilesystemOperator::class => \DI\get(Filesystem::class),
Filesystem::class => function (ConfigInterface $config) {
$default = $config->get('storage.default', 'local');
$diskConfig = $config->get("storage.disks.$default");
if (!$diskConfig) {
throw new \RuntimeException("Storage disk [$default] is not configured.");
}
$driver = $diskConfig['driver'] ?? 'local';
if ($driver !== 'local') {
throw new \RuntimeException("Unsupported storage driver [$driver]. Only 'local' is supported currently.");
}
$root = $diskConfig['root'];
$adapter = new LocalFilesystemAdapter($root);
return new Filesystem($adapter);
},
]);
}
public function boot(Container $container): void {}
}