Skip to content

Commit b05c700

Browse files
committed
wrapped filesystem lib
1 parent 04a6eee commit b05c700

File tree

5 files changed

+154
-8
lines changed

5 files changed

+154
-8
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Framework\Storage;
9+
10+
/**
11+
* Class FileNotFoundException
12+
*/
13+
class FileNotFoundException extends \RuntimeException
14+
{
15+
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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\Framework\Storage;
9+
10+
11+
class RootViolationException extends \RuntimeException
12+
{
13+
14+
}

lib/internal/Magento/Framework/Storage/Storage.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,68 @@
1010
/**
1111
* File storage abstraction
1212
*/
13-
class Storage extends Filesystem implements StorageInterface
13+
class Storage implements StorageInterface
1414
{
1515

16+
/**
17+
* @var Filesystem
18+
*/
19+
private $filesystem;
20+
21+
/**
22+
* Storage constructor.
23+
*
24+
* @param Filesystem $filesystem
25+
*/
26+
public function __construct(Filesystem $filesystem)
27+
{
28+
$this->filesystem = $filesystem;
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function put($path, $contents, array $config = []): bool
35+
{
36+
return $this->filesystem->put($path, $contents, $config);
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function deleteDir($dirname): bool
43+
{
44+
try {
45+
$result = $this->filesystem->deleteDir($dirname);
46+
} catch (\League\Flysystem\RootViolationException $exception) {
47+
throw new \Magento\Framework\Storage\RootViolationException($exception->getMessage());
48+
}
49+
return $result;
50+
}
51+
52+
/**
53+
* @inheritDoc
54+
*/
55+
public function getMetadata($path): ?array
56+
{
57+
try {
58+
$metadata = $this->filesystem->getMetadata($path);
59+
} catch (\League\Flysystem\FileNotFoundException $exception) {
60+
throw new \Magento\Framework\Storage\FileNotFoundException(
61+
$exception->getMessage()
62+
);
63+
}
64+
if ($metadata === false) {
65+
$metadata = null;
66+
}
67+
return $metadata;
68+
}
69+
70+
/**
71+
* @inheritDoc
72+
*/
73+
public function has($path): bool
74+
{
75+
return $this->filesystem->has($path);
76+
}
1677
}

lib/internal/Magento/Framework/Storage/StorageInterface.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,53 @@
55
*/
66
namespace Magento\Framework\Storage;
77

8-
use League\Flysystem\FilesystemInterface;
9-
108
/**
119
* Storage interface to be used by client code to manipulate objects in the storage
1210
*
1311
* Retrieve a real instance of storage via $storageProvider->get('<your-storage-name>'),
1412
* where $storageProvider is an instance of \Magento\Framework\Storage\StorageProvider
1513
*/
16-
interface StorageInterface extends FilesystemInterface
14+
interface StorageInterface
1715
{
16+
/**
17+
* Create a file or update if exists.
18+
*
19+
* @param string $path The path to the file.
20+
* @param string $contents The file contents.
21+
* @param array $config An optional configuration array.
22+
*
23+
* @return bool True on success, false on failure.
24+
*/
25+
public function put($path, $contents, array $config = []): bool;
26+
27+
/**
28+
* Delete a directory.
29+
*
30+
* @param string $dirname
31+
*
32+
* @throws RootViolationException Thrown if $dirname is empty.
33+
*
34+
* @return bool True on success, false on failure.
35+
*/
36+
public function deleteDir($dirname): bool;
37+
38+
/**
39+
* Get a file's metadata.
40+
*
41+
* @param string $path The path to the file.
42+
*
43+
* @throws FileNotFoundException
44+
*
45+
* @return array|false The file metadata or false on failure.
46+
*/
47+
public function getMetadata($path): ?array;
1848

49+
/**
50+
* Check whether a file exists.
51+
*
52+
* @param string $path
53+
*
54+
* @return bool
55+
*/
56+
public function has($path): bool;
1957
}

lib/internal/Magento/Framework/Storage/StorageProvider.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Framework\App\DeploymentConfig;
99
use Magento\Framework\Storage\AdapterFactory\LocalFactory;
1010
use Magento\Framework\Storage\StorageFactory;
11+
use League\Flysystem\FilesystemFactory;
1112

1213
/**
1314
* Main entry point for accessing file storage
@@ -31,18 +32,26 @@ class StorageProvider
3132
private $adapterProvider;
3233

3334
/**
34-
* Constructor
35-
*
35+
* @var FilesystemFactory
36+
*/
37+
private $filesystemFactory;
38+
39+
/**
40+
* StorageProvider constructor.
3641
* @param StorageAdapterProvider $adapterProvider
3742
* @param \Magento\Framework\Storage\StorageFactory $storageFactory
3843
* @param array $storage
3944
* @param DeploymentConfig $envConfig
45+
* @param FilesystemFactory $filesystemFactory
46+
* @throws \Magento\Framework\Exception\FileSystemException
47+
* @throws \Magento\Framework\Exception\RuntimeException
4048
*/
4149
public function __construct(
4250
StorageAdapterProvider $adapterProvider,
4351
StorageFactory $storageFactory,
4452
array $storage,
45-
DeploymentConfig $envConfig
53+
DeploymentConfig $envConfig,
54+
FilesystemFactory $filesystemFactory
4655
) {
4756
foreach ($storage as $storageName => $localPath) {
4857
$this->storageConfig[$storageName] = [
@@ -81,7 +90,15 @@ public function get(string $storageName): StorageInterface
8190
);
8291
}
8392
$adapter = $this->adapterProvider->create($config['adapter'], $config['options']);
84-
$this->storage[$storageName] = $this->storageFactory->create(['adapter' => $adapter]);
93+
$this->storage[$storageName] = $this->storageFactory->create(
94+
[
95+
'factory' => $this->filesystemFactory->create(
96+
[
97+
'adapter' => $adapter
98+
]
99+
)
100+
]
101+
);
85102
} else {
86103
throw new UnsupportedStorageException("No storage with name '$storageName' is declared");
87104
}

0 commit comments

Comments
 (0)