77
88namespace creocoder \flysystem ;
99
10+ use League \Flysystem \AdapterInterface ;
11+ use League \Flysystem \Cached \CachedAdapter ;
1012use League \Flysystem \Filesystem as NativeFilesystem ;
1113use League \Flysystem \Replicate \ReplicateAdapter ;
1214use Yii ;
1315use yii \base \Component ;
1416use 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