Skip to content

Commit ec9ad4b

Browse files
authored
Add missing resolveUri method to FlysystemStorage (#1441)
1 parent f315b4b commit ec9ad4b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Storage/FlysystemStorage.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,24 @@ protected function getFilesystem(PropertyMapping $mapping): FilesystemOperator
104104

105105
return $this->registry->get($mapping->getUploadDestination());
106106
}
107+
108+
public function resolveUri(object|array $obj, ?string $fieldName = null, ?string $className = null): ?string
109+
{
110+
$path = $this->resolvePath($obj, $fieldName, $className, true);
111+
112+
if (empty($path)) {
113+
return null;
114+
}
115+
116+
$mapping = null === $fieldName ?
117+
$this->factory->fromFirstField($obj, $className) :
118+
$this->factory->fromField($obj, $fieldName, $className);
119+
$fs = $this->getFilesystem($mapping);
120+
121+
try {
122+
return $fs->publicUrl($path);
123+
} catch (FilesystemException) {
124+
return $mapping->getUriPrefix().'/'.$path;
125+
}
126+
}
107127
}

tests/Storage/Flysystem/AbstractFlysystemStorageTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,27 @@ public static function pathProvider(): array
158158
['foo', '/absolute/foo/file.txt', false],
159159
];
160160
}
161+
162+
public function testResolveUri(): void
163+
{
164+
$this->mapping
165+
->expects(self::once())
166+
->method('getUriPrefix')
167+
->willReturn('/uploads');
168+
169+
$this->mapping
170+
->expects(self::once())
171+
->method('getFileName')
172+
->willReturn('file.txt');
173+
174+
$this->factory
175+
->expects(self::once())
176+
->method('fromField')
177+
->with($this->object, 'file_field')
178+
->willReturn($this->mapping);
179+
180+
$path = $this->getStorage()->resolveUri($this->object, 'file_field');
181+
182+
self::assertEquals('/uploads/file.txt', $path);
183+
}
161184
}

0 commit comments

Comments
 (0)