Skip to content

Commit

Permalink
Add missing resolveUri method to FlysystemStorage (#1441)
Browse files Browse the repository at this point in the history
  • Loading branch information
sovetski committed Apr 11, 2024
1 parent f315b4b commit ec9ad4b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Storage/FlysystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,24 @@ protected function getFilesystem(PropertyMapping $mapping): FilesystemOperator

return $this->registry->get($mapping->getUploadDestination());
}

public function resolveUri(object|array $obj, ?string $fieldName = null, ?string $className = null): ?string
{
$path = $this->resolvePath($obj, $fieldName, $className, true);

if (empty($path)) {
return null;
}

$mapping = null === $fieldName ?
$this->factory->fromFirstField($obj, $className) :
$this->factory->fromField($obj, $fieldName, $className);
$fs = $this->getFilesystem($mapping);

try {
return $fs->publicUrl($path);
} catch (FilesystemException) {
return $mapping->getUriPrefix().'/'.$path;
}
}
}
23 changes: 23 additions & 0 deletions tests/Storage/Flysystem/AbstractFlysystemStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,27 @@ public static function pathProvider(): array
['foo', '/absolute/foo/file.txt', false],
];
}

public function testResolveUri(): void
{
$this->mapping
->expects(self::once())
->method('getUriPrefix')
->willReturn('/uploads');

$this->mapping
->expects(self::once())
->method('getFileName')
->willReturn('file.txt');

$this->factory
->expects(self::once())
->method('fromField')
->with($this->object, 'file_field')
->willReturn($this->mapping);

$path = $this->getStorage()->resolveUri($this->object, 'file_field');

self::assertEquals('/uploads/file.txt', $path);
}
}

0 comments on commit ec9ad4b

Please sign in to comment.