Skip to content

Commit b5cbdbb

Browse files
committed
Added Filesystem cache property for caching feature
1 parent 5145166 commit b5cbdbb

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

src/Filesystem.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
namespace creocoder\flysystem;
99

10+
use League\Flysystem\AdapterInterface;
11+
use League\Flysystem\Cached\CachedAdapter;
1012
use League\Flysystem\Filesystem as NativeFilesystem;
1113
use League\Flysystem\Replicate\ReplicateAdapter;
1214
use Yii;
1315
use yii\base\Component;
1416
use yii\base\InvalidConfigException;
17+
use yii\caching\Cache;
1518

1619
/**
1720
* Filesystem
@@ -57,6 +60,10 @@ abstract class Filesystem extends Component
5760
* @var \League\Flysystem\Config|array|string|null
5861
*/
5962
public $config;
63+
/**
64+
* @var string|null
65+
*/
66+
public $cache;
6067
/**
6168
* @var string|null
6269
*/
@@ -75,29 +82,40 @@ public function init()
7582
}
7683

7784
/**
78-
* @return \League\Flysystem\AdapterInterface
85+
* @return AdapterInterface
7986
*/
8087
abstract protected function prepareAdapter();
8188

8289
/**
83-
* @param \League\Flysystem\AdapterInterface $adapter
84-
* @return \League\Flysystem\AdapterInterface
90+
* @param AdapterInterface $adapter
91+
* @return AdapterInterface
8592
* @throws InvalidConfigException
8693
*/
87-
protected function decorateAdapter($adapter)
94+
protected function decorateAdapter(AdapterInterface $adapter)
8895
{
89-
if ($this->replica === null) {
90-
return $adapter;
96+
if ($this->cache !== null) {
97+
/* @var Cache $cache */
98+
$cache = Yii::$app->get($this->cache);
99+
100+
if (!$cache instanceof Cache) {
101+
throw new InvalidConfigException('The "cache" property must be an instance of \yii\caching\Cache subclasses.');
102+
}
103+
104+
$adapter = new CachedAdapter($adapter, new YiiCache($cache));
91105
}
92106

93-
/* @var Filesystem $filesystem */
94-
$filesystem = Yii::$app->get($this->replica);
107+
if ($this->replica !== null) {
108+
/* @var Filesystem $filesystem */
109+
$filesystem = Yii::$app->get($this->replica);
110+
111+
if (!$filesystem instanceof Filesystem) {
112+
throw new InvalidConfigException('The "replica" property must be an instance of \creocoder\flysystem\Filesystem subclasses.');
113+
}
95114

96-
if (!$filesystem instanceof Filesystem) {
97-
throw new InvalidConfigException('The "replica" property must be an instance of \creocoder\flysystem\Filesystem subclasses.');
115+
$adapter = new ReplicateAdapter($adapter, $filesystem->getAdapter());
98116
}
99117

100-
return new ReplicateAdapter($adapter, $filesystem->getAdapter());
118+
return $adapter;
101119
}
102120

103121
/**

0 commit comments

Comments
 (0)