Skip to content

Commit

Permalink
Update assets to be stored as a Collection
Browse files Browse the repository at this point in the history
Better matches other file collections
  • Loading branch information
caendesilva committed Jul 28, 2024
1 parent 1d12225 commit 5389932
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Foundation\Concerns;

use Hyde\Foundation\Kernel\Filesystem;
use Illuminate\Support\Collection;

/**
* @internal Single-use trait for the HydeKernel class.
Expand Down Expand Up @@ -43,7 +44,7 @@ public function pathToRelative(string $path): string
return $this->filesystem->pathToRelative($path);
}

public function assets(): array
public function assets(): Collection
{
return $this->filesystem->assets();
}
Expand Down
13 changes: 7 additions & 6 deletions packages/framework/src/Foundation/Concerns/HasMediaFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Hyde\Hyde;
use Hyde\Facades\Config;
use Hyde\Support\Filesystem\MediaFile;
use Illuminate\Support\Collection;

use function implode;
use function collect;
Expand All @@ -20,26 +21,26 @@
*/
trait HasMediaFiles
{
/** @return array<string, \Hyde\Support\Filesystem\MediaFile> The array keys are the filenames relative to the _media/ directory */
protected array $assets;
/** @var Collection<string, \Hyde\Support\Filesystem\MediaFile> The Collection keys are the filenames relative to the _media/ directory */
protected Collection $assets;

/**
* Get all media files in the project.
*
* @return array<string, \Hyde\Support\Filesystem\MediaFile>
* @return Collection<string, \Hyde\Support\Filesystem\MediaFile>
*/
public function assets(): array
public function assets(): Collection
{
return $this->assets ??= static::discoverMediaFiles();
}

protected static function discoverMediaFiles(): array
protected static function discoverMediaFiles(): Collection
{
return collect(static::getMediaFiles())->mapWithKeys(function (string $path): array {
$file = MediaFile::make($path);

return [$file->getIdentifier() => $file];
})->all();
});
}

protected static function getMediaFiles(): array
Expand Down
8 changes: 4 additions & 4 deletions packages/framework/src/Support/Filesystem/MediaFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Hyde\Hyde;
use Hyde\Facades\Config;
use Illuminate\Support\Collection;
use Hyde\Framework\Exceptions\FileNotFoundException;
use Illuminate\Support\Str;

Expand All @@ -14,7 +15,6 @@
use function extension_loaded;
use function file_exists;
use function array_merge;
use function array_keys;
use function filesize;
use function pathinfo;
use function is_file;
Expand All @@ -27,16 +27,16 @@ class MediaFile extends ProjectFile
/** @var array<string> The default extensions for media types */
final public const EXTENSIONS = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];

/** @return array<string, \Hyde\Support\Filesystem\MediaFile> The array keys are the filenames relative to the _media/ directory */
public static function all(): array
/** @return \Illuminate\Support\Collection<string, \Hyde\Support\Filesystem\MediaFile> The array keys are the filenames relative to the _media/ directory */
public static function all(): Collection
{
return Hyde::assets();
}

/** @return array<string> Array of filenames relative to the _media/ directory */
public static function files(): array
{
return array_keys(static::all());
return static::all()->keys()->all();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/tests/Unit/Support/MediaFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function testAllHelperReturnsAllMediaFiles()
{
$this->assertEquals([
'app.css' => new MediaFile('_media/app.css'),
], MediaFile::all());
], MediaFile::all()->all());
}

public function testAllHelperDoesNotIncludeNonMediaFiles()
Expand All @@ -231,7 +231,7 @@ public function testAllHelperDoesNotIncludeNonMediaFiles()

$this->assertEquals([
'app.css' => new MediaFile('_media/app.css'),
], MediaFile::all());
], MediaFile::all()->all());
}

public function testFilesHelperReturnsAllMediaFiles()
Expand Down

0 comments on commit 5389932

Please sign in to comment.