/
Bootstrap.php
125 lines (109 loc) · 3.18 KB
/
Bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Wearesho\Yii\Filesystem;
use yii\base;
use yii\di;
use League\Flysystem;
/**
* Class Bootstrap
* @package Wearesho\Yii\Filesystem
*/
class Bootstrap extends base\BaseObject implements base\BootstrapInterface
{
/** @var array[]|string[]|AdapterInterface[] array of definitions */
public $adapters = [
'local' => [
'class' => Local\Adapter::class,
'config' => [
'class' => Local\EnvironmentConfig::class,
'keyPrefix' => 'FILESYSTEM_LOCAL_',
],
],
's3' => [
'class' => S3\Adapter::class,
'config' => [
'class' => S3\EnvironmentConfig::class,
'keyPrefix' => 'S3_',
],
],
'ftp' => [
'class' => Ftp\Adapter::class,
'config' => [
'class' => Ftp\EnvironmentConfig::class,
'keyPrefix' => 'FTP_',
],
],
];
/** @var array|string|ConfigInterface definition */
public $config = [
'class' => EnvironmentConfig::class,
'keyPrefix' => 'FILESYSTEM_',
];
/** @var bool Should container be configured in bootstrap */
public $container = false;
/** @var string \Yii::$app component name to be set. If null, no component will be configured */
public $id = null;
/**
* @throws base\InvalidConfigException
*/
public function init(): void
{
parent::init();
$this->config = di\Instance::ensure($this->config, ConfigInterface::class);
}
/**
* @inheritdoc
* @throws base\InvalidConfigException
*/
public function bootstrap($app): void
{
if (!is_null($this->id)) {
/** @noinspection MissedFieldInspection */
$app->set($this->id, [
'class' => Filesystem::class,
'adapter' => $this->getAdapterReference(),
]);
}
if ($this->container === true) {
$this->configure(\Yii::$container);
}
}
/**
* @param di\Container $container
* @throws base\InvalidConfigException
*/
public function configure(di\Container $container): void
{
$adapter = $this->getAdapterReference();
$container->setSingleton(
Flysystem\AdapterInterface::class,
AdapterInterface::class
);
$container->setSingleton(
AdapterInterface::class,
$adapter
);
$container->setSingleton(
Flysystem\Filesystem::class,
Filesystem::class
);
$container->setSingleton(
Filesystem::class,
[
'class' => Filesystem::class,
'adapter' => AdapterInterface::class,
]
);
}
/**
* @return array|string
* @throws base\InvalidConfigException
*/
public function getAdapterReference()
{
$adapterKey = $this->config->getAdapter();
if (!array_key_exists($adapterKey, $this->adapters)) {
throw new base\InvalidConfigException("Adapter {$adapterKey} is not configured");
}
return $this->adapters[$adapterKey];
}
}