Skip to content

Commit 6cf60bf

Browse files
authored
Merge pull request #1933 from hydephp/bring-media-assets-into-the-hyde-kernel
[2.x] Lazy loaded MediaFile metadata properties
2 parents 5ba193a + aea81c9 commit 6cf60bf

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,13 @@ class MediaFile extends ProjectFile
2525
/** @var array<string> The default extensions for media types */
2626
final public const EXTENSIONS = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];
2727

28-
public readonly int $length;
29-
public readonly string $mimeType;
30-
public readonly string $hash;
28+
protected readonly int $length;
29+
protected readonly string $mimeType;
30+
protected readonly string $hash;
3131

3232
public function __construct(string $path)
3333
{
3434
parent::__construct($this->getNormalizedPath($path));
35-
36-
$this->length = $this->findContentLength();
37-
$this->mimeType = $this->findMimeType();
38-
$this->hash = $this->findHash();
3935
}
4036

4137
/**
@@ -101,16 +97,22 @@ public function toArray(): array
10197

10298
public function getContentLength(): int
10399
{
100+
$this->ensureInstanceIsBooted('length');
101+
104102
return $this->length;
105103
}
106104

107105
public function getMimeType(): string
108106
{
107+
$this->ensureInstanceIsBooted('mimeType');
108+
109109
return $this->mimeType;
110110
}
111111

112112
public function getHash(): string
113113
{
114+
$this->ensureInstanceIsBooted('hash');
115+
114116
return $this->hash;
115117
}
116118

@@ -124,6 +126,7 @@ public static function getCacheBustKey(string $file): string
124126

125127
protected function getNormalizedPath(string $path): string
126128
{
129+
// Ensure we are working with a relative project path
127130
$path = Hyde::pathToRelative($path);
128131

129132
// Normalize paths using output directory to have source directory prefix
@@ -134,10 +137,6 @@ protected function getNormalizedPath(string $path): string
134137
// Normalize the path to include the media directory
135138
$path = static::sourcePath(trim_slashes(Str::after($path, Hyde::getMediaDirectory())));
136139

137-
if (Filesystem::missing($path)) {
138-
throw new FileNotFoundException($path);
139-
}
140-
141140
return $path;
142141
}
143142

@@ -183,4 +182,22 @@ protected function findHash(): string
183182
{
184183
return Filesystem::hash($this->getPath(), 'crc32');
185184
}
185+
186+
protected function ensureInstanceIsBooted(string $property): void
187+
{
188+
if (! isset($this->$property)) {
189+
$this->boot();
190+
}
191+
}
192+
193+
protected function boot(): void
194+
{
195+
if (Filesystem::missing($this->getPath())) {
196+
throw new FileNotFoundException($this->getPath());
197+
}
198+
199+
$this->length = $this->findContentLength();
200+
$this->mimeType = $this->findMimeType();
201+
$this->hash = $this->findHash();
202+
}
186203
}

packages/framework/tests/Feature/Support/MediaFileTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function testMediaFilePathHandling()
8181
public function testMediaFileExceptionHandling()
8282
{
8383
$this->expectException(FileNotFoundException::class);
84-
MediaFile::make('non_existent_file.txt');
84+
MediaFile::make('non_existent_file.txt')->getContentLength();
8585
}
8686

8787
public function testMediaDirectoryCustomization()

packages/framework/tests/Unit/Support/MediaFileUnitTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ public function testConstructorWithVariousInputFormats()
147147
public function testConstructorSetsProperties()
148148
{
149149
$file = new MediaFile('foo.txt');
150-
$this->assertNotNull($file->length);
151-
$this->assertNotNull($file->mimeType);
152-
$this->assertNotNull($file->hash);
150+
$this->assertNotNull($file->getContentLength());
151+
$this->assertNotNull($file->getMimeType());
152+
$this->assertNotNull($file->getHash());
153153
}
154154

155155
public function testNormalizePathWithAbsolutePath()
@@ -271,7 +271,8 @@ public function testGetHashReturnsHash()
271271
$this->assertSame(hash('crc32', 'Hello World!'), MediaFile::make('foo.txt')->getHash());
272272
}
273273

274-
public function testExceptionIsThrownWhenConstructingFileThatDoesNotExist()
274+
/** @dataProvider bootableMethodsProvider */
275+
public function testExceptionIsThrownWhenBootingFileThatDoesNotExist(string $bootableMethod)
275276
{
276277
$this->mockFilesystem->shouldReceive('missing')
277278
->with(Hyde::path('_media/foo'))
@@ -280,7 +281,7 @@ public function testExceptionIsThrownWhenConstructingFileThatDoesNotExist()
280281
$this->expectException(FileNotFoundException::class);
281282
$this->expectExceptionMessage('File [_media/foo] not found.');
282283

283-
MediaFile::make('foo');
284+
MediaFile::make('foo')->$bootableMethod();
284285
}
285286

286287
public function testExceptionIsNotThrownWhenConstructingFileThatDoesExist()
@@ -389,4 +390,13 @@ public function testOutputPathWithLeadingSlash()
389390
{
390391
$this->assertSame(Hyde::sitePath('media/foo'), MediaFile::outputPath('/foo'));
391392
}
393+
394+
public static function bootableMethodsProvider(): array
395+
{
396+
return [
397+
['getContentLength'],
398+
['getMimeType'],
399+
['getHash'],
400+
];
401+
}
392402
}

0 commit comments

Comments
 (0)