Skip to content

Commit

Permalink
FilesystemMapper: returns only existing files
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 16, 2025
1 parent c2bbc77 commit aaa786d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 29 deletions.
5 changes: 0 additions & 5 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,4 @@ public function getUrl(): string;
* Shortcut for getUrl()
*/
public function __toString(): string;

/**
* Checks if the asset file exists in the filesystem.
*/
public function exists(): bool;
}
9 changes: 6 additions & 3 deletions src/Assets/FilesystemMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ public function __construct(string $baseUrl, string $basePath, array $extensions
/**
* Returns asset instance for given reference.
*/
public function getAsset(string $reference, array $options = []): FileAsset
public function getAsset(string $reference, array $options = []): ?FileAsset
{
Helpers::checkOptions($options);
$path = $this->resolvePath($reference);
$ext = $this->extensions && !is_file($path)
$path .= $ext = $this->extensions && !is_file($path)
? $this->findExtension($path)
: '';
return new FileAsset($this->buildUrl($reference . $ext, $options), $path . $ext);

return is_file($path)
? new FileAsset($this->buildUrl($reference . $ext, $options), $path)
: null;
}


Expand Down
2 changes: 1 addition & 1 deletion src/Assets/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface Mapper
/**
* Returns asset instance for given reference.
*/
public function getAsset(string $reference, array $options = []): Asset;
public function getAsset(string $reference, array $options = []): ?Asset;
}
4 changes: 2 additions & 2 deletions src/Assets/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public function getMapper(string $id = ''): Mapper
/**
* Returns asset instance for given mapper-qualified reference.
*/
public function getAsset(string $qualifiedRef, array $options = []): Asset
public function getAsset(string $qualifiedRef, array $options = []): ?Asset
{
$cacheKey = $qualifiedRef . ($options ? '?' . http_build_query($options) : '');
if (isset($this->cache[$cacheKey])) {
if (array_key_exists($cacheKey, $this->cache)) {
return $this->cache[$cacheKey];
}

Expand Down
22 changes: 4 additions & 18 deletions tests/Assets/FilesystemMapper.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ test('Basic mapper functionality', function () {

Assert::same('http://example.com/assets/test.txt?v=2700000000', $asset->getUrl());
Assert::same(__DIR__ . '/fixtures/test.txt', $asset->getPath());
Assert::true($asset->exists());
});

test('URL without trailing slash', function () {
Expand All @@ -28,10 +27,7 @@ test('URL without trailing slash', function () {

test('Non-existent file version handling', function () {
$mapper = new FilesystemMapper('http://example.com/assets', __DIR__ . '/fixtures');
$asset = $mapper->getAsset('missing.txt');

Assert::same('http://example.com/assets/missing.txt', $asset->getUrl());
Assert::false($asset->exists());
Assert::null($mapper->getAsset('missing.txt'));
});

test('Mandatory extension autodetection', function () {
Expand All @@ -43,19 +39,12 @@ test('Mandatory extension autodetection', function () {

$exact = $mapper->getAsset('image.gif');
Assert::match('http://example.com/assets/image.gif?v=%d%', $exact->getUrl());
Assert::true($exact->exists());

$gif = $mapper->getAsset('image');
Assert::match('http://example.com/assets/image.gif?v=%d%', $gif->getUrl());
Assert::true($gif->exists());

$notFound = $mapper->getAsset('missing');
Assert::same('http://example.com/assets/missing.gif', $notFound->getUrl());
Assert::false($notFound->exists());

$subdir = $mapper->getAsset('subdir');
Assert::same('http://example.com/assets/subdir.gif', $subdir->getUrl());
Assert::false($subdir->exists());
Assert::null($mapper->getAsset('missing'));
Assert::null($mapper->getAsset('subdir'));
});

test('Optional extension autodetection', function () {
Expand All @@ -67,11 +56,8 @@ test('Optional extension autodetection', function () {

$gif = $mapper->getAsset('image');
Assert::match('http://example.com/assets/image.gif?v=%d%', $gif->getUrl());
Assert::true($gif->exists());

$notFound = $mapper->getAsset('missing');
Assert::same('http://example.com/assets/missing', $notFound->getUrl());
Assert::false($notFound->exists());
Assert::null($mapper->getAsset('missing'));
});

test('Option validation', function () {
Expand Down

0 comments on commit aaa786d

Please sign in to comment.