Skip to content

Commit 0fa855f

Browse files
Roman HaninRoman Hanin
authored andcommitted
B2B-1610 Upgrade to Flysystem 2.0
- review fix
1 parent b2fcdc1 commit 0fa855f

File tree

5 files changed

+64
-36
lines changed

5 files changed

+64
-36
lines changed

app/code/Magento/AwsS3/Driver/AwsS3.php

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,15 @@ public function filePutContents($path, $content, $mode = null): int
279279
*/
280280
public function readDirectoryRecursively($path = null): array
281281
{
282-
return $this->readPath($this->normalizeRelativePath($path), true);
282+
return $this->readPath($path, true);
283283
}
284284

285285
/**
286286
* @inheritDoc
287287
*/
288288
public function readDirectory($path): array
289289
{
290-
return $this->readPath($this->normalizeRelativePath($path), false);
290+
return $this->readPath($path, false);
291291
}
292292

293293
/**
@@ -402,12 +402,19 @@ public function isReadable($path): bool
402402

403403
/**
404404
* Check is specified path a file.
405+
*
406+
* @param string $path
407+
* @return bool
405408
*/
406409
private function isTypeFile($path)
407410
{
408-
$metadata = $this->metadataProvider->getMetadata($this->normalizeRelativePath($path));
409-
if ($metadata && isset($metadata['type'])) {
410-
return $metadata['type'] === 'file';
411+
try {
412+
$metadata = $this->metadataProvider->getMetadata($this->normalizeRelativePath($path, true));
413+
if ($metadata && isset($metadata['type'])) {
414+
return $metadata['type'] === self::TYPE_FILE;
415+
}
416+
} catch (UnableToRetrieveMetadata $e) {
417+
return false;
411418
}
412419
return false;
413420
}
@@ -420,15 +427,7 @@ public function isFile($path): bool
420427
if (!$path || $path === '/') {
421428
return false;
422429
}
423-
424-
$path = $this->normalizeRelativePath($path, true);
425-
426-
try {
427-
return $this->isTypeFile($path);
428-
} catch (\League\Flysystem\FilesystemException $e) {
429-
$this->logger->error($e->getMessage());
430-
}
431-
return false;
430+
return $this->isTypeFile($path);
432431
}
433432

434433
/**
@@ -440,26 +439,20 @@ public function isDirectory($path): bool
440439
return true;
441440
}
442441

443-
$path = $this->normalizeRelativePath($path, true);
444-
445442
if (!$path) {
446443
return true;
447444
}
448445

449446
try {
450-
return !$this->isTypeFile($path);
451-
} catch (UnableToRetrieveMetadata $e) {
452-
try {
453-
return iterator_count($this->adapter->listContents($path, false)) > 0;
454-
} catch (\Throwable $e) {
455-
// catch closed iterator
456-
return false;
457-
}
458-
} catch (\League\Flysystem\FilesystemException $e) {
459-
$this->logger->error($e->getMessage());
447+
return iterator_count(
448+
$this->adapter->listContents(
449+
$this->normalizeRelativePath($path, true),
450+
false)
451+
) > 0;
452+
} catch (\Throwable $e) {
453+
// catch closed iterator
454+
return false;
460455
}
461-
462-
return false;
463456
}
464457

465458
/**
@@ -884,7 +877,7 @@ public function fileOpen($path, $mode)
884877
}
885878
}
886879

887-
return $this->streams[$path] ?? null;
880+
return $this->streams[$path];
888881
}
889882

890883
/**

app/code/Magento/AwsS3/Driver/AwsS3Factory.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
1212
use Magento\Framework\Exception\LocalizedException;
1313
use Magento\Framework\ObjectManagerInterface;
14-
use Magento\RemoteStorage\Driver\Adapter\Cache\CacheInterface;
15-
use Magento\RemoteStorage\Driver\Adapter\CachedAdapter;
14+
use Magento\RemoteStorage\Driver\Adapter\Cache\CacheInterfaceFactory;
15+
use Magento\RemoteStorage\Driver\Adapter\CachedAdapterInterfaceFactory;
1616
use Magento\RemoteStorage\Driver\Adapter\MetadataProviderFactoryInterface;
1717
use Magento\RemoteStorage\Driver\DriverException;
1818
use Magento\RemoteStorage\Driver\DriverFactoryInterface;
@@ -39,19 +39,35 @@ class AwsS3Factory implements DriverFactoryInterface
3939
*/
4040
private $metadataProviderFactory;
4141

42+
/**
43+
* @var CacheInterfaceFactory
44+
*/
45+
private $cacheInterfaceFactory;
46+
47+
/**
48+
* @var CachedAdapterInterfaceFactory
49+
*/
50+
private $cachedAdapterInterfaceFactory;
51+
4252
/**
4353
* @param ObjectManagerInterface $objectManager
4454
* @param Config $config
4555
* @param MetadataProviderFactoryInterface $metadataProviderFactory
56+
* @param CacheInterfaceFactory $cacheInterfaceFactory
57+
* @param CachedAdapterInterfaceFactory $cachedAdapterInterfaceFactory
4658
*/
4759
public function __construct(
4860
ObjectManagerInterface $objectManager,
4961
Config $config,
50-
MetadataProviderFactoryInterface $metadataProviderFactory
62+
MetadataProviderFactoryInterface $metadataProviderFactory,
63+
CacheInterfaceFactory $cacheInterfaceFactory,
64+
CachedAdapterInterfaceFactory $cachedAdapterInterfaceFactory
5165
) {
5266
$this->objectManager = $objectManager;
5367
$this->config = $config;
5468
$this->metadataProviderFactory = $metadataProviderFactory;
69+
$this->cacheInterfaceFactory = $cacheInterfaceFactory;
70+
$this->cachedAdapterInterfaceFactory = $cachedAdapterInterfaceFactory;
5571
}
5672

5773
/**
@@ -94,12 +110,12 @@ public function createConfigured(
94110

95111
$client = new S3Client($config);
96112
$adapter = new AwsS3V3Adapter($client, $config['bucket'], $prefix);
97-
$cache = $this->objectManager->get(CacheInterface::class);
113+
$cache = $this->cacheInterfaceFactory->create();
98114

99115
return $this->objectManager->create(
100116
AwsS3::class,
101117
[
102-
'adapter' => $this->objectManager->create(CachedAdapter::class, [
118+
'adapter' => $this->cachedAdapterInterfaceFactory->create([
103119
'adapter' => $adapter,
104120
'cache' => $cache
105121
]),

app/code/Magento/RemoteStorage/Driver/Adapter/CachedAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Cached adapter implementation for filesystem storage.
1717
*/
18-
class CachedAdapter implements FilesystemAdapter
18+
class CachedAdapter implements CachedAdapterInterface
1919
{
2020
/**
2121
* @var FilesystemAdapter
@@ -159,7 +159,7 @@ public function fileExists(string $path): bool
159159
}
160160
}
161161

162-
if (! $exists) {
162+
if (!$exists) {
163163
$this->cache->storeFileNotExists($path);
164164
} else {
165165
$cacheEntry = is_array($exists) ? $exists : ['path' => $path];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\RemoteStorage\Driver\Adapter;
9+
10+
use League\Flysystem\FilesystemAdapter;
11+
12+
/**
13+
* Cached adapter interface for filesystem storage.
14+
*/
15+
interface CachedAdapterInterface extends FilesystemAdapter
16+
{
17+
18+
}

app/code/Magento/RemoteStorage/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\RemoteStorage\Driver\Adapter\Cache\CacheInterface" type="Magento\RemoteStorage\Driver\Adapter\Cache\Generic"/>
10+
<preference for="Magento\RemoteStorage\Driver\Adapter\CachedAdapterInterface" type="Magento\RemoteStorage\Driver\Adapter\CachedAdapter"/>
1011
<preference for="Magento\RemoteStorage\Driver\Adapter\MetadataProviderInterface" type="Magento\RemoteStorage\Driver\Adapter\MetadataProvider"/>
1112
<preference for="Magento\RemoteStorage\Driver\Adapter\MetadataProviderFactoryInterface" type="Magento\RemoteStorage\Driver\Adapter\MetadataProviderFactory"/>
1213
<virtualType name="remoteWriteFactory" type="Magento\Framework\Filesystem\Directory\WriteFactory">

0 commit comments

Comments
 (0)