|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Bootloader\Storage; |
|
13
|
|
|
|
|
14
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
|
15
|
|
|
use Spiral\Config\ConfiguratorInterface; |
|
16
|
|
|
use Spiral\Core\Container; |
|
17
|
|
|
use Spiral\Storage\Storage; |
|
18
|
|
|
use Spiral\Storage\StorageInterface; |
|
19
|
|
|
use Spiral\Storage\Bucket; |
|
20
|
|
|
use Spiral\Storage\BucketInterface; |
|
21
|
|
|
use Spiral\Distribution\DistributionInterface as CdnInterface; |
|
22
|
|
|
|
|
23
|
|
|
class StorageBootloader extends Bootloader |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param Container $app |
|
27
|
|
|
* @param ConfiguratorInterface $config |
|
28
|
|
|
*/ |
|
29
|
|
|
public function boot(Container $app, ConfiguratorInterface $config): void |
|
30
|
|
|
{ |
|
31
|
|
|
$config->setDefaults(StorageConfig::CONFIG, [ |
|
32
|
|
|
'default' => Storage::DEFAULT_STORAGE, |
|
33
|
|
|
'servers' => [], |
|
34
|
|
|
'buckets' => [], |
|
35
|
|
|
]); |
|
36
|
|
|
|
|
37
|
|
|
$app->bindInjector(StorageConfig::class, ConfiguratorInterface::class); |
|
38
|
|
|
|
|
39
|
|
|
$app->bindSingleton(StorageInterface::class, static function (StorageConfig $config, CdnInterface $cdn) { |
|
40
|
|
|
$manager = new Storage($config->getDefaultBucket()); |
|
41
|
|
|
|
|
42
|
|
|
$distributions = $config->getDistributions(); |
|
43
|
|
|
|
|
44
|
|
|
foreach ($config->getAdapters() as $name => $adapter) { |
|
45
|
|
|
$resolver = isset($distributions[$name]) |
|
46
|
|
|
? $cdn->resolver($distributions[$name]) |
|
47
|
|
|
: null; |
|
48
|
|
|
|
|
49
|
|
|
$manager->add($name, Bucket::fromAdapter($adapter, $name, $resolver)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $manager; |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
$app->bindSingleton(Storage::class, static function (StorageInterface $manager) { |
|
56
|
|
|
return $manager; |
|
57
|
|
|
}); |
|
58
|
|
|
|
|
59
|
|
|
$app->bindSingleton(BucketInterface::class, static function (StorageInterface $manager) { |
|
60
|
|
|
return $manager->bucket(); |
|
61
|
|
|
}); |
|
62
|
|
|
|
|
63
|
|
|
$app->bindSingleton(Bucket::class, static function (BucketInterface $storage) { |
|
64
|
|
|
return $storage; |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|