Skip to content

Commit e00cbe0

Browse files
authored
Merge pull request #1917 from hydephp/bring-media-assets-into-the-hyde-kernel
[2.x] Bring media assets into the Hyde kernel
2 parents 314c57a + 94e3f21 commit e00cbe0

File tree

11 files changed

+303
-37
lines changed

11 files changed

+303
-37
lines changed

docs/_data/partials/hyde-pages-api/hyde-kernel-filesystem-methods.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<section id="hyde-kernel-filesystem-methods">
22

33
<!-- Start generated docs for Hyde\Foundation\Concerns\ForwardsFilesystem -->
4-
<!-- Generated by HydePHP DocGen script at 2024-07-26 15:15:12 in 0.09ms -->
4+
<!-- Generated by HydePHP DocGen script at 2024-07-28 11:20:12 in 0.11ms -->
55

66
#### `filesystem()`
77

@@ -51,6 +51,14 @@ No description provided.
5151
Hyde::pathToRelative(string $path): string
5252
```
5353

54+
#### `assets()`
55+
56+
No description provided.
57+
58+
```php
59+
Hyde::assets(): Illuminate\Support\Collection
60+
```
61+
5462
<!-- End generated docs for Hyde\Foundation\Concerns\ForwardsFilesystem -->
5563

5664
</section>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Hyde\Foundation\Concerns;
66

77
use Hyde\Foundation\Kernel\Filesystem;
8+
use Illuminate\Support\Collection;
89

910
/**
1011
* @internal Single-use trait for the HydeKernel class.
@@ -42,4 +43,9 @@ public function pathToRelative(string $path): string
4243
{
4344
return $this->filesystem->pathToRelative($path);
4445
}
46+
47+
public function assets(): Collection
48+
{
49+
return $this->filesystem->assets();
50+
}
4551
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Foundation\Concerns;
6+
7+
use Hyde\Hyde;
8+
use Hyde\Facades\Config;
9+
use Hyde\Support\Filesystem\MediaFile;
10+
use Illuminate\Support\Collection;
11+
12+
use function implode;
13+
use function collect;
14+
use function sprintf;
15+
use function glob;
16+
17+
/**
18+
* @internal Single-use trait for the Filesystem class.
19+
*
20+
* @see \Hyde\Foundation\Kernel\Filesystem
21+
*/
22+
trait HasMediaFiles
23+
{
24+
/** @var Collection<string, \Hyde\Support\Filesystem\MediaFile> The Collection keys are the filenames relative to the _media/ directory */
25+
protected Collection $assets;
26+
27+
/**
28+
* Get all media files in the project.
29+
*
30+
* @return Collection<string, \Hyde\Support\Filesystem\MediaFile>
31+
*/
32+
public function assets(): Collection
33+
{
34+
return $this->assets ??= static::discoverMediaFiles();
35+
}
36+
37+
protected static function discoverMediaFiles(): Collection
38+
{
39+
return collect(static::getMediaFiles())->mapWithKeys(function (string $path): array {
40+
$file = MediaFile::make($path);
41+
42+
return [$file->getIdentifier() => $file];
43+
});
44+
}
45+
46+
protected static function getMediaFiles(): array
47+
{
48+
return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: [];
49+
}
50+
51+
protected static function getMediaGlobPattern(): string
52+
{
53+
return sprintf(Hyde::getMediaDirectory().'/{*,**/*,**/*/*}.{%s}', implode(',',
54+
Config::getArray('hyde.media_extensions', MediaFile::EXTENSIONS)
55+
));
56+
}
57+
}

packages/framework/src/Foundation/Kernel/Filesystem.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Hyde\Hyde;
88
use Hyde\Foundation\HydeKernel;
99
use Hyde\Foundation\PharSupport;
10+
use Hyde\Foundation\Concerns\HasMediaFiles;
1011
use Illuminate\Support\Collection;
1112

1213
use function collect;
@@ -29,6 +30,8 @@
2930
*/
3031
class Filesystem
3132
{
33+
use HasMediaFiles;
34+
3235
protected HydeKernel $kernel;
3336

3437
public function __construct(HydeKernel $kernel)

packages/framework/src/Hyde.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Hyde\Foundation\HydeKernel;
1010
use Hyde\Support\V1Compatibility;
1111
use Illuminate\Support\Collection;
12+
use Hyde\Support\Filesystem\MediaFile;
1213
use Hyde\Foundation\Kernel\FileCollection;
1314
use Hyde\Foundation\Kernel\Filesystem;
1415
use Hyde\Foundation\Kernel\PageCollection;
@@ -37,6 +38,7 @@
3738
* @method static string formatLink(string $destination)
3839
* @method static string relativeLink(string $destination)
3940
* @method static string mediaLink(string $destination, bool $validate = false)
41+
* @method static Collection<string, MediaFile> assets()
4042
* @method static string asset(string $name)
4143
* @method static string url(string $path = '')
4244
* @method static Route|null route(string $key)

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

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Hyde\Hyde;
88
use Hyde\Facades\Config;
9+
use Illuminate\Support\Collection;
910
use Hyde\Framework\Exceptions\FileNotFoundException;
1011
use Illuminate\Support\Str;
1112

@@ -14,14 +15,9 @@
1415
use function extension_loaded;
1516
use function file_exists;
1617
use function array_merge;
17-
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.
@@ -31,16 +27,16 @@ class MediaFile extends ProjectFile
3127
/** @var array<string> The default extensions for media types */
3228
final public const EXTENSIONS = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];
3329

34-
/** @return array<string, \Hyde\Support\Filesystem\MediaFile> The array keys are the filenames relative to the _media/ directory */
35-
public static function all(): array
30+
/** @return \Illuminate\Support\Collection<string, \Hyde\Support\Filesystem\MediaFile> The array keys are the filenames relative to the _media/ directory */
31+
public static function all(): Collection
3632
{
37-
return static::discoverMediaFiles();
33+
return Hyde::assets();
3834
}
3935

4036
/** @return array<string> Array of filenames relative to the _media/ directory */
4137
public static function files(): array
4238
{
43-
return array_keys(static::all());
39+
return static::all()->keys()->all();
4440
}
4541

4642
/**
@@ -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
{

packages/framework/tests/Feature/DiscoveryServiceTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ public function testGetMediaAssetFilesDiscoversFiles()
8888
{
8989
$testFiles = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];
9090

91-
foreach ($testFiles as $fileType) {
92-
$path = 'test.'.$fileType;
93-
$this->file('_media/'.$path);
94-
$this->assertContains($path, MediaFile::files());
91+
foreach ($testFiles as $type) {
92+
$this->file("_media/test.$type");
93+
}
94+
95+
foreach ($testFiles as $type) {
96+
$this->assertContains("test.$type", MediaFile::files());
9597
}
9698
}
9799

@@ -101,6 +103,8 @@ public function testGetMediaAssetFilesDiscoversCustomFileTypes()
101103
$this->file("_media/$path");
102104
$this->assertNotContains($path, MediaFile::files());
103105
self::mockConfig(['hyde.media_extensions' => ['custom']]);
106+
107+
$this->resetKernel(); // Reboot to rediscover new files
104108
$this->assertContains($path, MediaFile::files());
105109
}
106110

@@ -131,6 +135,7 @@ public function testMediaAssetExtensionsCanBeAddedByCommaSeparatedValues()
131135
$this->assertSame([], MediaFile::files());
132136

133137
self::mockConfig(['hyde.media_extensions' => ['1,2,3']]);
138+
$this->resetKernel(); // Reboot to rediscover new files
134139
$this->assertSame(['test.1', 'test.2', 'test.3'], MediaFile::files());
135140
}
136141

@@ -143,6 +148,7 @@ public function testMediaAssetExtensionsCanBeAddedByArray()
143148

144149
$this->assertSame([], MediaFile::files());
145150
self::mockConfig(['hyde.media_extensions' => ['1', '2', '3']]);
151+
$this->resetKernel(); // Reboot to rediscover new files
146152
$this->assertSame(['test.1', 'test.2', 'test.3'], MediaFile::files());
147153
}
148154

packages/framework/tests/Feature/Foundation/FilesystemTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Hyde\Framework\Testing\Feature\Foundation;
66

77
use Hyde\Foundation\HydeKernel;
8+
use Illuminate\Support\Collection;
89
use Hyde\Foundation\Kernel\Filesystem;
910
use Hyde\Foundation\PharSupport;
1011
use Hyde\Hyde;
@@ -21,6 +22,7 @@
2122
/**
2223
* @covers \Hyde\Foundation\HydeKernel
2324
* @covers \Hyde\Foundation\Kernel\Filesystem
25+
* @covers \Hyde\Foundation\Concerns\HasMediaFiles
2426
*/
2527
class FilesystemTest extends UnitTestCase
2628
{
@@ -29,6 +31,7 @@ class FilesystemTest extends UnitTestCase
2931
protected Filesystem $filesystem;
3032

3133
protected static bool $needsKernel = true;
34+
protected static bool $needsConfig = true;
3235

3336
protected function setUp(): void
3437
{
@@ -354,4 +357,32 @@ public function testPathToRelativeHelperDoesNotModifyNonProjectPaths()
354357
$this->assertSame(normalize_slashes($testString), Hyde::pathToRelative($testString));
355358
}
356359
}
360+
361+
public function testAssetsMethodGetsAllSiteAssets()
362+
{
363+
$this->assertEquals(new Collection([
364+
'app.css' => new MediaFile('_media/app.css'),
365+
]), $this->filesystem->assets());
366+
}
367+
368+
public function testAssetsMethodGetsAllSiteAssetsAsArray()
369+
{
370+
$assets = $this->filesystem->assets()->toArray();
371+
372+
$assets['app.css']['length'] = 123;
373+
374+
$this->assertSame([
375+
'app.css' => [
376+
'name' => 'app.css',
377+
'path' => '_media/app.css',
378+
'length' => 123,
379+
'mimeType' => 'text/css',
380+
],
381+
], $assets);
382+
}
383+
384+
public function testAssetsMethodReturnsAssetCollectionSingleton()
385+
{
386+
$this->assertSame($this->filesystem->assets(), $this->filesystem->assets());
387+
}
357388
}

packages/framework/tests/Feature/HydeKernelTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ public function testImageHelperSupportsCustomMediaDirectories()
204204
$this->assertSame('assets/foo.jpg', Hyde::asset('foo.jpg'));
205205
}
206206

207+
public function testAssetsHelperGetsAllSiteAssets()
208+
{
209+
$this->assertEquals(new Collection([
210+
'app.css' => new MediaFile('_media/app.css'),
211+
]), Hyde::assets());
212+
}
213+
214+
public function testAssetsHelperReturnsAssetCollectionSingleton()
215+
{
216+
$this->assertSame(Hyde::assets(), Hyde::assets());
217+
}
218+
207219
public function testRouteHelper()
208220
{
209221
$this->assertNotNull(Hyde::route('index'));

0 commit comments

Comments
 (0)