1 | <?php |
||
58 | abstract class AbstractFsComponent extends Component |
||
59 | { |
||
60 | /** |
||
61 | * @var \League\Flysystem\Config|array|string|null |
||
62 | */ |
||
63 | public $config; |
||
64 | /** |
||
65 | * @var string|null |
||
66 | */ |
||
67 | public $cache; |
||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | public $cacheKey = 'flysystem'; |
||
72 | /** |
||
73 | * @var integer |
||
74 | */ |
||
75 | public $cacheDuration = 3600; |
||
76 | /** |
||
77 | * @var string|null |
||
78 | */ |
||
79 | public $replica; |
||
80 | /** |
||
81 | * @var \League\Flysystem\FilesystemInterface |
||
82 | */ |
||
83 | protected $filesystem; |
||
84 | |||
85 | /** |
||
86 | * @inheritdoc |
||
87 | */ |
||
88 | public function __call($method, $parameters) |
||
89 | { |
||
90 | return call_user_func_array([$this->filesystem, $method], $parameters); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @inheritdoc |
||
95 | */ |
||
96 | public function init() |
||
97 | { |
||
98 | $adapter = $this->checkReplica($this->checkCached($this->initAdapter())); |
||
99 | |||
100 | $this->filesystem = new Filesystem($adapter, $this->config); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param AdapterInterface $adapter |
||
105 | * |
||
106 | * @return AdapterInterface|CachedAdapter |
||
107 | * @throws InvalidConfigException |
||
108 | */ |
||
109 | protected function checkCached(AdapterInterface $adapter) |
||
110 | { |
||
111 | if (null !== $this->cache) { |
||
112 | /* @var Cache $cache */ |
||
113 | $cache = Yii::$app->get($this->cache); |
||
114 | if (!$cache instanceof Cache) { |
||
115 | throw new InvalidConfigException( |
||
116 | printf('The "cache" property must be an instance of %s subclasses.', Cache::class) |
||
117 | ); |
||
118 | } |
||
119 | $adapter = new CachedAdapter($adapter, new YiiCache($cache, $this->cacheKey, $this->cacheDuration)); |
||
120 | } |
||
121 | |||
122 | return $adapter; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param AdapterInterface $adapter |
||
127 | * |
||
128 | * @return ReplicateAdapter|AdapterInterface |
||
129 | * @throws InvalidConfigException |
||
130 | */ |
||
131 | protected function checkReplica(AdapterInterface $adapter) |
||
132 | { |
||
133 | if ($this->replica !== null) { |
||
134 | /* @var Filesystem $filesystem */ |
||
135 | $filesystem = Yii::$app->get($this->replica); |
||
136 | if (!$filesystem instanceof Filesystem) { |
||
137 | throw new InvalidConfigException( |
||
138 | printf('The "replica" property must be an instance of %s subclasses.', AbstractFsComponent::class) |
||
139 | ); |
||
140 | } |
||
141 | $adapter = new ReplicateAdapter($adapter, $filesystem->getAdapter()); |
||
142 | } |
||
143 | |||
144 | return $adapter; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return AdapterInterface $adapter |
||
149 | */ |
||
150 | abstract protected function initAdapter(); |
||
151 | } |
||
152 |