Skip to content

Commit

Permalink
removed exists()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 22, 2024
1 parent 88bb6fb commit 3bfaefe
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 22 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;
}
13 changes: 2 additions & 11 deletions src/Assets/FileAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ public function __toString(): string
}


/**
* Checks if the asset file exists in the filesystem.
*/
public function exists(): bool
{
return is_file($this->path);
}


/**
* Returns duration in seconds for MP3 audio file.
*/
Expand Down Expand Up @@ -89,11 +80,11 @@ public function getHeight(): int

/**
* Returns the dimensions [width, height] of an image file.
* @throws \RuntimeException if file is not an image or doesn't exist
* @throws \RuntimeException if file is not an image
*/
private function getSize(): array
{
return $this->size ??= @getimagesize($this->getPath()) // @ - file may not exist or is not an image
return $this->size ??= @getimagesize($this->getPath()) // @ - file may not be an image
?: throw new \RuntimeException(sprintf(
"Cannot get size of image '%s'. %s",
$this->getPath(),
Expand Down
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
1 change: 1 addition & 0 deletions tests/Assets/Provider.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MockAsset implements Asset
}
}


class MockMapper implements Mapper
{
public function __construct(
Expand Down

0 comments on commit 3bfaefe

Please sign in to comment.