8
8
namespace Magento \AwsS3 \Driver ;
9
9
10
10
use Aws \S3 \S3Client ;
11
- use League \Flysystem \AwsS3v3 \AwsS3Adapter ;
12
- use League \Flysystem \Cached \CachedAdapter ;
11
+ use League \Flysystem \AwsS3V3 \AwsS3V3Adapter ;
13
12
use Magento \Framework \Exception \LocalizedException ;
14
13
use Magento \Framework \ObjectManagerInterface ;
15
- use Magento \RemoteStorage \Driver \Cache \CacheFactory ;
14
+ use Magento \RemoteStorage \Driver \Adapter \Cache \CacheInterfaceFactory ;
15
+ use Magento \RemoteStorage \Driver \Adapter \CachedAdapterInterfaceFactory ;
16
+ use Magento \RemoteStorage \Driver \Adapter \MetadataProviderInterfaceFactory ;
16
17
use Magento \RemoteStorage \Driver \DriverException ;
17
18
use Magento \RemoteStorage \Driver \DriverFactoryInterface ;
18
19
use Magento \RemoteStorage \Driver \RemoteDriverInterface ;
@@ -29,25 +30,52 @@ class AwsS3Factory implements DriverFactoryInterface
29
30
private $ objectManager ;
30
31
31
32
/**
32
- * @var CacheFactory
33
+ * @var Config
33
34
*/
34
- private $ cacheFactory ;
35
+ private $ config ;
35
36
36
37
/**
37
- * @var Config
38
+ * @var MetadataProviderInterfaceFactory
38
39
*/
39
- private $ config ;
40
+ private $ metadataProviderFactory ;
41
+
42
+ /**
43
+ * @var CacheInterfaceFactory
44
+ */
45
+ private $ cacheInterfaceFactory ;
46
+
47
+ /**
48
+ * @var CachedAdapterInterfaceFactory
49
+ */
50
+ private $ cachedAdapterInterfaceFactory ;
51
+
52
+ /**
53
+ * @var string|null
54
+ */
55
+ private $ cachePrefix ;
40
56
41
57
/**
42
58
* @param ObjectManagerInterface $objectManager
43
- * @param CacheFactory $cacheFactory
44
59
* @param Config $config
60
+ * @param MetadataProviderInterfaceFactory $metadataProviderFactory
61
+ * @param CacheInterfaceFactory $cacheInterfaceFactory
62
+ * @param CachedAdapterInterfaceFactory $cachedAdapterInterfaceFactory
63
+ * @param string|null $cachePrefix
45
64
*/
46
- public function __construct (ObjectManagerInterface $ objectManager , CacheFactory $ cacheFactory , Config $ config )
47
- {
65
+ public function __construct (
66
+ ObjectManagerInterface $ objectManager ,
67
+ Config $ config ,
68
+ MetadataProviderInterfaceFactory $ metadataProviderFactory ,
69
+ CacheInterfaceFactory $ cacheInterfaceFactory ,
70
+ CachedAdapterInterfaceFactory $ cachedAdapterInterfaceFactory ,
71
+ string $ cachePrefix = null
72
+ ) {
48
73
$ this ->objectManager = $ objectManager ;
49
- $ this ->cacheFactory = $ cacheFactory ;
50
74
$ this ->config = $ config ;
75
+ $ this ->metadataProviderFactory = $ metadataProviderFactory ;
76
+ $ this ->cacheInterfaceFactory = $ cacheInterfaceFactory ;
77
+ $ this ->cachedAdapterInterfaceFactory = $ cachedAdapterInterfaceFactory ;
78
+ $ this ->cachePrefix = $ cachePrefix ;
51
79
}
52
80
53
81
/**
@@ -58,9 +86,7 @@ public function create(): RemoteDriverInterface
58
86
try {
59
87
return $ this ->createConfigured (
60
88
$ this ->config ->getConfig (),
61
- $ this ->config ->getPrefix (),
62
- $ this ->config ->getCacheAdapter (),
63
- $ this ->config ->getCacheConfig ()
89
+ $ this ->config ->getPrefix ()
64
90
);
65
91
} catch (LocalizedException $ exception ) {
66
92
throw new DriverException (__ ($ exception ->getMessage ()), $ exception );
@@ -73,8 +99,8 @@ public function create(): RemoteDriverInterface
73
99
public function createConfigured (
74
100
array $ config ,
75
101
string $ prefix ,
76
- string $ cacheAdapter ,
77
- array $ cacheConfig
102
+ string $ cacheAdapter = '' ,
103
+ array $ cacheConfig = []
78
104
): RemoteDriverInterface {
79
105
$ config ['version ' ] = 'latest ' ;
80
106
@@ -91,16 +117,31 @@ public function createConfigured(
91
117
}
92
118
93
119
$ client = new S3Client ($ config );
94
- $ adapter = new AwsS3Adapter ($ client , $ config ['bucket ' ], $ prefix );
95
-
120
+ $ adapter = new AwsS3V3Adapter ($ client , $ config ['bucket ' ], $ prefix );
121
+ $ cache = $ this ->cacheInterfaceFactory ->create (
122
+ // Custom cache prefix required to distinguish cache records for different sources.
123
+ // phpcs:ignore Magento2.Security.InsecureFunction
124
+ $ this ->cachePrefix ? ['prefix ' => $ this ->cachePrefix ] : ['prefix ' => md5 ($ config ['bucket ' ] . $ prefix )]
125
+ );
126
+ $ metadataProvider = $ this ->metadataProviderFactory ->create (
127
+ [
128
+ 'adapter ' => $ adapter ,
129
+ 'cache ' => $ cache
130
+ ]
131
+ );
132
+ $ objectUrl = rtrim ($ client ->getObjectUrl ($ config ['bucket ' ], './ ' ), '/ ' ) . trim ($ prefix , '\\/ ' ) . '/ ' ;
96
133
return $ this ->objectManager ->create (
97
134
AwsS3::class,
98
135
[
99
- 'adapter ' => $ this ->objectManager ->create (CachedAdapter::class, [
100
- 'adapter ' => $ adapter ,
101
- 'cache ' => $ this ->cacheFactory ->create ($ cacheAdapter , $ cacheConfig )
102
- ]),
103
- 'objectUrl ' => $ client ->getObjectUrl ($ adapter ->getBucket (), $ adapter ->applyPathPrefix ('. ' ))
136
+ 'adapter ' => $ this ->cachedAdapterInterfaceFactory ->create (
137
+ [
138
+ 'adapter ' => $ adapter ,
139
+ 'cache ' => $ cache ,
140
+ 'metadataProvider ' => $ metadataProvider
141
+ ]
142
+ ),
143
+ 'objectUrl ' => $ objectUrl ,
144
+ 'metadataProvider ' => $ metadataProvider ,
104
145
]
105
146
);
106
147
}
0 commit comments