Skip to content

Commit 22cd1a6

Browse files
committed
Move asset discovery to kernel filesystem
1 parent 826c4db commit 22cd1a6

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

packages/framework/src/Foundation/Concerns/HasMediaFiles.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55
namespace Hyde\Foundation\Concerns;
66

7+
use Hyde\Hyde;
8+
use Hyde\Facades\Config;
79
use Hyde\Support\Filesystem\MediaFile;
810

11+
use function implode;
12+
use function collect;
13+
use function sprintf;
14+
use function glob;
15+
916
/**
1017
* @internal Single-use trait for the Filesystem class.
1118
*
@@ -25,4 +32,25 @@ public function assets(): array
2532
{
2633
return $this->assets ??= MediaFile::all();
2734
}
35+
36+
protected static function discoverMediaFiles(): array
37+
{
38+
return collect(static::getMediaFiles())->mapWithKeys(function (string $path): array {
39+
$file = MediaFile::make($path);
40+
41+
return [$file->getIdentifier() => $file];
42+
})->all();
43+
}
44+
45+
protected static function getMediaFiles(): array
46+
{
47+
return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: [];
48+
}
49+
50+
protected static function getMediaGlobPattern(): string
51+
{
52+
return sprintf(Hyde::getMediaDirectory().'/{*,**/*,**/*/*}.{%s}', implode(',',
53+
Config::getArray('hyde.media_extensions', MediaFile::EXTENSIONS)
54+
));
55+
}
2856
}

packages/framework/src/Support/Filesystem/MediaFile.php

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616
use function array_merge;
1717
use function array_keys;
1818
use function filesize;
19-
use function implode;
2019
use function pathinfo;
21-
use function collect;
2220
use function is_file;
23-
use function sprintf;
24-
use function glob;
2521

2622
/**
2723
* File abstraction for a project media file.
@@ -34,7 +30,7 @@ class MediaFile extends ProjectFile
3430
/** @return array<string, \Hyde\Support\Filesystem\MediaFile> The array keys are the filenames relative to the _media/ directory */
3531
public static function all(): array
3632
{
37-
return static::discoverMediaFiles();
33+
return Hyde::assets();
3834
}
3935

4036
/** @return array<string> Array of filenames relative to the _media/ directory */
@@ -123,27 +119,6 @@ public function getMimeType(): string
123119
return 'text/plain';
124120
}
125121

126-
protected static function discoverMediaFiles(): array
127-
{
128-
return collect(static::getMediaFiles())->mapWithKeys(function (string $path): array {
129-
$file = static::make($path);
130-
131-
return [$file->getIdentifier() => $file];
132-
})->all();
133-
}
134-
135-
protected static function getMediaFiles(): array
136-
{
137-
return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: [];
138-
}
139-
140-
protected static function getMediaGlobPattern(): string
141-
{
142-
return sprintf(Hyde::getMediaDirectory().'/{*,**/*,**/*/*}.{%s}', implode(',',
143-
Config::getArray('hyde.media_extensions', self::EXTENSIONS)
144-
));
145-
}
146-
147122
/** @internal */
148123
public static function getCacheBustKey(string $file): string
149124
{

0 commit comments

Comments
 (0)