1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Filesystem; |
6
|
|
|
|
7
|
|
|
use League\Flysystem\FilesystemAdapter; |
8
|
|
|
use Yiisoft\Di\Container; |
9
|
|
|
use Yiisoft\Di\CompositeContainer; |
10
|
|
|
use Psr\Container\ContainerInterface; |
11
|
|
|
use Yiisoft\Di\Contracts\ServiceProviderInterface; |
12
|
|
|
use Yiisoft\Factory\Factory; |
13
|
|
|
|
14
|
|
|
final class FileStorageServiceProvider implements ServiceProviderInterface |
15
|
|
|
{ |
16
|
|
|
public function getDefinitions(): array |
17
|
|
|
{ |
18
|
|
|
return []; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getExtensions(): array |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
'core.di.delegates' => static function (ContainerInterface $container, CompositeContainer $delegates) { |
25
|
|
|
$factory = new Factory(); |
26
|
|
|
$configs = $container->get(FileStorageConfigs::class)->getConfigs(); |
27
|
|
|
|
28
|
|
|
$filesystemsDefinitions = []; |
29
|
|
|
foreach ($configs as $alias => $config) { |
30
|
|
|
$this->validateAdapter($alias, $config); |
31
|
|
|
$configParams = $config['config'] ?? []; |
32
|
|
|
$aliases = $config['aliases'] ?? []; |
33
|
|
|
$adapter = $factory->create($config['adapter']); |
34
|
|
|
$filesystemsDefinitions[$alias] = fn () => new Filesystem($adapter, $aliases, $configParams); |
35
|
|
|
} |
36
|
|
|
$delegates->attach(new Container($filesystemsDefinitions)); |
37
|
|
|
|
38
|
|
|
return $delegates; |
39
|
|
|
}, |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function validateAdapter(string $alias, array $config): void |
44
|
|
|
{ |
45
|
|
|
$adapter = $config['adapter']['class'] ?? false; |
46
|
|
|
if (!$adapter) { |
47
|
|
|
throw new \RuntimeException("Adapter is not defined in the \"$alias\" storage config."); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (!is_subclass_of($adapter, FilesystemAdapter::class)) { |
51
|
|
|
throw new \RuntimeException('Adapter must implement \League\Flysystem\FilesystemAdapter interface.'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|