From 3bfaefee9ae92d894e50b7eb687ee1bbeb733df2 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 22 Nov 2024 16:16:32 +0100 Subject: [PATCH] removed exists() --- src/Assets/Asset.php | 5 ----- src/Assets/FileAsset.php | 13 ++----------- src/Assets/FilesystemMapper.php | 9 ++++++--- src/Assets/Mapper.php | 2 +- src/Assets/Registry.php | 4 ++-- tests/Assets/Provider.basic.phpt | 1 + 6 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index ac85e44..0f92d1c 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -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; } diff --git a/src/Assets/FileAsset.php b/src/Assets/FileAsset.php index e9e07bc..a8adf3e 100644 --- a/src/Assets/FileAsset.php +++ b/src/Assets/FileAsset.php @@ -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. */ @@ -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(), diff --git a/src/Assets/FilesystemMapper.php b/src/Assets/FilesystemMapper.php index 4bf74c0..07e0068 100644 --- a/src/Assets/FilesystemMapper.php +++ b/src/Assets/FilesystemMapper.php @@ -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; } diff --git a/src/Assets/Mapper.php b/src/Assets/Mapper.php index 075280b..a7c4f85 100644 --- a/src/Assets/Mapper.php +++ b/src/Assets/Mapper.php @@ -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; } diff --git a/src/Assets/Registry.php b/src/Assets/Registry.php index f21fb63..b09d23b 100644 --- a/src/Assets/Registry.php +++ b/src/Assets/Registry.php @@ -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]; } diff --git a/tests/Assets/Provider.basic.phpt b/tests/Assets/Provider.basic.phpt index 4c65034..8b0515e 100644 --- a/tests/Assets/Provider.basic.phpt +++ b/tests/Assets/Provider.basic.phpt @@ -30,6 +30,7 @@ class MockAsset implements Asset } } + class MockMapper implements Mapper { public function __construct(