Skip to content

Commit 37b2408

Browse files
committed
Composer: upgrade to flysystem v2
1 parent f8d527b commit 37b2408

6 files changed

Lines changed: 56 additions & 86 deletions

File tree

‎composer.json‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
],
88
"require": {
99
"php": ">=8.1",
10+
"ext-fileinfo": "*",
1011
"nette/utils": "^4.0.3",
11-
"league/flysystem": "^1.1.0",
12+
"league/flysystem": "^2.5.0",
1213
"psr/event-dispatcher": "^1.0.0"
1314
},
1415
"require-dev": {
@@ -30,13 +31,13 @@
3031
"contributte/phpstan": "^0.1",
3132
"ramsey/uuid": "^4.7.5",
3233
"spatie/temporary-directory": "^1.3.0",
33-
"superbalist/flysystem-google-storage": "^7.2.2",
3434
"symfony/dependency-injection": "^6.4.0 || ^7.0.0",
3535
"symfony/filesystem": "^6.4.0 || ^7.0.0",
3636
"symfony/http-foundation": "^6.4.0 || ^7.0.0",
3737
"symfony/http-kernel": "^6.4.0 || ^7.0.0",
3838
"symfony/serializer": "^6.4.0 || ^7.0.0",
39-
"tracy/tracy": "^2.10.5"
39+
"tracy/tracy": "^2.10.5",
40+
"league/flysystem-google-cloud-storage": "^2.4.4"
4041
},
4142
"autoload": {
4243
"psr-4": {

‎src/Filesystem/FilesystemAbstract.php‎

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,116 +3,104 @@
33
namespace Contributte\Imagist\Filesystem;
44

55
use Contributte\Imagist\Exceptions\FileException;
6+
use Contributte\Imagist\Exceptions\FileNotFoundException;
67
use Contributte\Imagist\PathInfo\PathInfoInterface;
7-
use League\Flysystem\FileNotFoundException;
8-
use League\Flysystem\Filesystem;
9-
use League\Flysystem\FilesystemInterface as LeagueFilesystemInterface;
8+
use League\Flysystem\Config;
9+
use League\Flysystem\FilesystemAdapter;
10+
use League\Flysystem\FilesystemException;
11+
use League\Flysystem\StorageAttributes;
12+
use League\Flysystem\UnableToDeleteFile;
13+
use League\Flysystem\UnableToReadFile;
14+
use League\Flysystem\UnableToRetrieveMetadata;
1015

1116
abstract class FilesystemAbstract implements FilesystemInterface
1217
{
1318

14-
protected LeagueFilesystemInterface $adapter;
19+
protected FilesystemAdapter $adapter;
1520

16-
public function __construct(LeagueFilesystemInterface $adapter)
21+
public function __construct(FilesystemAdapter $adapter)
1722
{
1823
$this->adapter = $adapter;
1924
}
2025

2126
/**
22-
* @inheritDoc
27+
* {@inheritDoc}
2328
*/
2429
public function exists(PathInfoInterface $path): bool
2530
{
26-
return $this->adapter->has($path->toString());
31+
return $this->adapter->fileExists($path->toString());
2732
}
2833

2934
/**
30-
* @inheritDoc
35+
* {@inheritDoc}
3136
*/
3237
public function delete(PathInfoInterface $path): bool
3338
{
34-
return $this->withTemporaryConfig(
35-
'disable_asserts',
36-
true,
37-
false,
38-
fn (): bool => $this->adapter->delete($path->toString())
39-
);
39+
try {
40+
$this->adapter->delete($path->toString());
41+
} catch (UnableToDeleteFile $e) {
42+
throw new FileException(sprintf('Cannot delete file %s', $path->toString()), 0, $e);
43+
}
44+
45+
return true;
4046
}
4147

4248
/**
43-
* @inheritDoc
49+
* @return array<StorageAttributes>
4450
*/
45-
public function listContents(string $path): array
51+
public function listContents(string $path): iterable
4652
{
47-
return $this->adapter->listContents($path);
53+
return $this->adapter->listContents($path, false);
4854
}
4955

5056
/**
51-
* @inheritDoc
57+
* {@inheritDoc}
5258
*/
5359
public function put(PathInfoInterface $path, string $content, array $config = []): void
5460
{
55-
$this->adapter->put($path->toString(), $content, $config);
61+
$this->adapter->write($path->toString(), $content, new Config($config));
5662
}
5763

5864
/**
59-
* @inheritDoc
65+
* {@inheritDoc}
6066
*/
6167
public function putWithMkdir(PathInfoInterface $path, string $content, array $config = []): void
6268
{
63-
$this->adapter->createDir($path->toString($path::BUCKET | $path::SCOPE | $path::FILTER));
69+
$this->adapter->createDirectory($path->toString($path::BUCKET | $path::SCOPE | $path::FILTER), new Config());
6470

6571
$this->put($path, $content, $config);
6672
}
6773

6874
/**
69-
* @inheritDoc
75+
* {@inheritDoc}
7076
*/
7177
public function read(PathInfoInterface $path): string
7278
{
7379
try {
7480
$content = $this->adapter->read($path->toString());
75-
} catch (FileNotFoundException $exception) {
76-
throw new \Contributte\Imagist\Exceptions\FileNotFoundException($exception->getMessage());
77-
}
78-
79-
if ($content === false) {
80-
throw new FileException(sprintf('Cannot read file %s', $path->toString()));
81+
} catch (UnableToReadFile $e) {
82+
throw new FileNotFoundException(sprintf('Cannot read file %s', $path->toString()), 0, $e);
83+
} catch (FilesystemException $e) {
84+
throw new FileNotFoundException($e->getMessage(), 0, $e);
8185
}
8286

8387
return $content;
8488
}
8589

8690
/**
87-
* @inheritDoc
91+
* {@inheritDoc}
8892
*/
8993
public function mimeType(PathInfoInterface $path): ?string
9094
{
91-
$mimeType = $this->adapter->getMimetype($path->toString());
92-
93-
return $mimeType === false ? null : $mimeType;
94-
}
95-
96-
/**
97-
* @template T
98-
* @param mixed $value
99-
* @param mixed $default
100-
* @param callable(): T $callback
101-
* @return T
102-
*/
103-
protected function withTemporaryConfig(string $name, $value, $default, callable $callback) // phpcs:ignore -- phpcs bug
104-
{
105-
if (!$this->adapter instanceof Filesystem) {
106-
return $callback();
95+
try {
96+
$mimeType = $this->adapter->mimeType($path->toString());
97+
} catch (UnableToRetrieveMetadata $e) {
98+
throw new FileException(sprintf('Cannot get mime type of file %s', $path->toString()), 0, $e);
99+
} catch (FilesystemException $e) {
100+
throw new FileNotFoundException($e->getMessage(), 0, $e);
107101
}
108102

109-
$default = $this->adapter->getConfig()->get($name, $default);
110-
111-
$this->adapter->getConfig()->set($name, $value);
112-
$result = $callback();
113-
$this->adapter->getConfig()->set($name, $default);
114-
115-
return $result;
103+
return $mimeType->mimeType();
116104
}
117105

118106
}

‎src/Filesystem/FilesystemInterface.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function delete(PathInfoInterface $path): mixed;
1919
/**
2020
* @return mixed[]
2121
*/
22-
public function listContents(string $path): array;
22+
public function listContents(string $path): iterable;
2323

2424
/**
2525
* @param mixed[] $config

‎src/Filesystem/GoogleStorageFilesystem.php‎

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,28 @@
33
namespace Contributte\Imagist\Filesystem;
44

55
use Contributte\Imagist\PathInfo\PathInfoInterface;
6-
use Google\Cloud\Core\Exception\NotFoundException;
76
use Google\Cloud\Storage\StorageClient;
8-
use League\Flysystem\Filesystem;
9-
use LogicException;
10-
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter;
7+
use League\Flysystem\GoogleCloudStorage\GoogleCloudStorageAdapter;
118

129
final class GoogleStorageFilesystem extends FilesystemAbstract
1310
{
1411

1512
public function __construct(string $bucket, string $keyFilePath)
1613
{
17-
if (!class_exists(GoogleStorageAdapter::class)) {
18-
throw new LogicException(
19-
sprintf(
20-
'%s not found, install it by composer install superbalist/flysystem-google-storage',
21-
self::class
22-
)
23-
);
24-
}
25-
2614
$client = new StorageClient([
2715
'keyFilePath' => $keyFilePath,
2816
]);
2917
$bucket = $client->bucket($bucket);
3018

31-
parent::__construct(new Filesystem(
32-
new GoogleStorageAdapter($client, $bucket)
33-
));
19+
parent::__construct(new GoogleCloudStorageAdapter($bucket));
3420
}
3521

3622
/**
37-
* @inheritDoc
23+
* {@inheritDoc}
3824
*/
3925
public function putWithMkdir(PathInfoInterface $path, string $content, array $config = []): void
4026
{
4127
$this->put($path, $content, $config);
4228
}
4329

44-
public function delete(PathInfoInterface $path): bool
45-
{
46-
try {
47-
return parent::delete($path);
48-
} catch (NotFoundException $exception) {
49-
return false;
50-
}
51-
}
52-
5330
}

‎src/Filesystem/LocalFilesystem.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
namespace Contributte\Imagist\Filesystem;
44

5-
use League\Flysystem\Adapter\Local;
6-
use League\Flysystem\Filesystem;
5+
use League\Flysystem\Local\LocalFilesystemAdapter;
76

87
final class LocalFilesystem extends FilesystemAbstract
98
{
109

1110
public function __construct(string $root)
1211
{
13-
parent::__construct(new Filesystem(new Local($root)));
12+
parent::__construct(new LocalFilesystemAdapter($root));
1413
}
1514

1615
}

‎src/Remover/PersistentImageRemover.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Contributte\Imagist\Filter\Internal\VoidFilter;
1212
use Contributte\Imagist\PathInfo\PathInfoFactoryInterface;
1313
use DomainException;
14+
use League\Flysystem\StorageAttributes;
1415

1516
final class PersistentImageRemover implements RemoverInterface
1617
{
@@ -50,6 +51,10 @@ private function removeFiltered(PersistentImageInterface $image): void
5051
$path = $this->pathInfoFactory->create($image->withFilter(new VoidFilter('void')));
5152

5253
foreach ($this->filesystem->listContents($path->toString($path::BUCKET | $path::SCOPE)) as $path) {
54+
if ($path instanceof StorageAttributes) {
55+
$path = $path->jsonSerialize();
56+
}
57+
5358
if (!is_array($path)) {
5459
continue;
5560
}
@@ -60,7 +65,7 @@ private function removeFiltered(PersistentImageInterface $image): void
6065
continue;
6166
}
6267

63-
$filename = $path['filename'] ?? '';
68+
$filename = isset($path['path']) ? basename($path['path']) : '';
6469

6570
if (!is_string($filename) || !$filename || $filename[0] !== '_') {
6671
continue;

0 commit comments

Comments
 (0)