Skip to content

Commit 4fe238b

Browse files
markvaneijkclaude
andcommitted
Fix put() override via custom FilesystemAdapter subclass
The `put` macro on FilesystemAdapter was silently ignored because `put()` is a concrete method and macros only fire through `__call()`. Additionally, macros were registered globally on all FilesystemAdapter instances. Introduce UploadcareFilesystemAdapter that properly overrides `put()` and adds `putGetUuid`, `putFileGetUuid`, `putFileAsGetUuid`, and `fileInfo` as real methods scoped to the uploadcare disk only. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 785f61b commit 4fe238b

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed

src/UploadcareAdapterServiceProvider.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Vormkracht10\UploadcareAdapter;
44

5-
use Illuminate\Filesystem\FilesystemAdapter;
65
use Illuminate\Support\Facades\Storage;
76
use Illuminate\Support\ServiceProvider;
87
use League\Flysystem\Filesystem;
@@ -17,23 +16,7 @@ public function boot(): void
1716

1817
$adapter = new UploadcareAdapter($api, $config);
1918

20-
FilesystemAdapter::macro('putGetUuid', function (string $path, mixed $contents) use ($adapter) {
21-
return $adapter->putGetUuid($path, $contents);
22-
});
23-
24-
FilesystemAdapter::macro('putFileGetUuid', function (string $path, mixed $contents) use ($adapter) {
25-
return $adapter->putFileGetUuid($path, $contents);
26-
});
27-
28-
FilesystemAdapter::macro('putFileAsGetUuid', function (string $path, mixed $contents, string $name, array $options = []) use ($adapter) {
29-
return $adapter->putFileAsGetUuid($path, $contents, $name, $options);
30-
});
31-
32-
FilesystemAdapter::macro('fileInfo', function (string $path) use ($adapter) {
33-
return $adapter->getFileinfo($path);
34-
});
35-
36-
return new FilesystemAdapter(
19+
return new UploadcareFilesystemAdapter(
3720
new Filesystem($adapter, $config),
3821
$adapter,
3922
$config
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Vormkracht10\UploadcareAdapter;
4+
5+
use Illuminate\Filesystem\FilesystemAdapter;
6+
use League\Flysystem\FileAttributes;
7+
8+
class UploadcareFilesystemAdapter extends FilesystemAdapter
9+
{
10+
protected UploadcareAdapter $uploadcareAdapter;
11+
12+
public function __construct(
13+
\League\Flysystem\FilesystemOperator $driver,
14+
UploadcareAdapter $adapter,
15+
array $config = [],
16+
) {
17+
$this->uploadcareAdapter = $adapter;
18+
19+
parent::__construct($driver, $adapter, $config);
20+
}
21+
22+
/**
23+
* Write the contents of a file.
24+
*
25+
* @param string $path
26+
* @param \Psr\Http\Message\StreamInterface|\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|resource $contents
27+
* @param mixed $options
28+
* @return string|bool
29+
*/
30+
public function put($path, $contents, $options = [])
31+
{
32+
return $this->uploadcareAdapter->putGetUuid($path, $contents, $options);
33+
}
34+
35+
/**
36+
* @return string|bool
37+
*/
38+
public function putGetUuid(string $path, mixed $contents, mixed $options = [])
39+
{
40+
return $this->uploadcareAdapter->putGetUuid($path, $contents, $options);
41+
}
42+
43+
/**
44+
* @return string|bool
45+
*/
46+
public function putFileGetUuid(string $path, mixed $contents, mixed $options = [])
47+
{
48+
return $this->uploadcareAdapter->putFileGetUuid($path, $contents, $options);
49+
}
50+
51+
/**
52+
* @return string|false
53+
*/
54+
public function putFileAsGetUuid(string $path, mixed $file, ?string $name = null, array $options = [])
55+
{
56+
return $this->uploadcareAdapter->putFileAsGetUuid($path, $file, $name, $options);
57+
}
58+
59+
public function fileInfo(string $path): FileAttributes
60+
{
61+
return $this->uploadcareAdapter->getFileinfo($path);
62+
}
63+
}

0 commit comments

Comments
 (0)