Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<section id="hyde-kernel-filesystem-methods">

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

#### `filesystem()`

Expand Down Expand Up @@ -51,6 +51,14 @@ No description provided.
Hyde::pathToRelative(string $path): string
```

#### `assets()`

No description provided.

```php
Hyde::assets(): Illuminate\Support\Collection
```

<!-- End generated docs for Hyde\Foundation\Concerns\ForwardsFilesystem -->

</section>
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 @@ -42,4 +43,9 @@ public function pathToRelative(string $path): string
{
return $this->filesystem->pathToRelative($path);
}

public function assets(): Collection
{
return $this->filesystem->assets();
}
}
57 changes: 57 additions & 0 deletions packages/framework/src/Foundation/Concerns/HasMediaFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Hyde\Foundation\Concerns;

use Hyde\Hyde;
use Hyde\Facades\Config;
use Hyde\Support\Filesystem\MediaFile;
use Illuminate\Support\Collection;

use function implode;
use function collect;
use function sprintf;
use function glob;

/**
* @internal Single-use trait for the Filesystem class.
*
* @see \Hyde\Foundation\Kernel\Filesystem
*/
trait HasMediaFiles
{
/** @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 Collection<string, \Hyde\Support\Filesystem\MediaFile>
*/
public function assets(): Collection
{
return $this->assets ??= static::discoverMediaFiles();
}

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

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

protected static function getMediaFiles(): array
{
return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: [];
}

protected static function getMediaGlobPattern(): string
{
return sprintf(Hyde::getMediaDirectory().'/{*,**/*,**/*/*}.{%s}', implode(',',
Config::getArray('hyde.media_extensions', MediaFile::EXTENSIONS)
));
}
}
3 changes: 3 additions & 0 deletions packages/framework/src/Foundation/Kernel/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Hyde\Hyde;
use Hyde\Foundation\HydeKernel;
use Hyde\Foundation\PharSupport;
use Hyde\Foundation\Concerns\HasMediaFiles;
use Illuminate\Support\Collection;

use function collect;
Expand All @@ -29,6 +30,8 @@
*/
class Filesystem
{
use HasMediaFiles;

protected HydeKernel $kernel;

public function __construct(HydeKernel $kernel)
Expand Down
2 changes: 2 additions & 0 deletions packages/framework/src/Hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Hyde\Foundation\HydeKernel;
use Hyde\Support\V1Compatibility;
use Illuminate\Support\Collection;
use Hyde\Support\Filesystem\MediaFile;
use Hyde\Foundation\Kernel\FileCollection;
use Hyde\Foundation\Kernel\Filesystem;
use Hyde\Foundation\Kernel\PageCollection;
Expand Down Expand Up @@ -37,6 +38,7 @@
* @method static string formatLink(string $destination)
* @method static string relativeLink(string $destination)
* @method static string mediaLink(string $destination, bool $validate = false)
* @method static Collection<string, MediaFile> assets()
* @method static string asset(string $name)
* @method static string url(string $path = '')
* @method static Route|null route(string $key)
Expand Down
35 changes: 5 additions & 30 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,14 +15,9 @@
use function extension_loaded;
use function file_exists;
use function array_merge;
use function array_keys;
use function filesize;
use function implode;
use function pathinfo;
use function collect;
use function is_file;
use function sprintf;
use function glob;

/**
* File abstraction for a project media file.
Expand All @@ -31,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 static::discoverMediaFiles();
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 Expand Up @@ -123,27 +119,6 @@ public function getMimeType(): string
return 'text/plain';
}

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

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

protected static function getMediaFiles(): array
{
return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: [];
}

protected static function getMediaGlobPattern(): string
{
return sprintf(Hyde::getMediaDirectory().'/{*,**/*,**/*/*}.{%s}', implode(',',
Config::getArray('hyde.media_extensions', self::EXTENSIONS)
));
}

/** @internal */
public static function getCacheBustKey(string $file): string
{
Expand Down
14 changes: 10 additions & 4 deletions packages/framework/tests/Feature/DiscoveryServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ public function testGetMediaAssetFilesDiscoversFiles()
{
$testFiles = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];

foreach ($testFiles as $fileType) {
$path = 'test.'.$fileType;
$this->file('_media/'.$path);
$this->assertContains($path, MediaFile::files());
foreach ($testFiles as $type) {
$this->file("_media/test.$type");
}

foreach ($testFiles as $type) {
$this->assertContains("test.$type", MediaFile::files());
}
}

Expand All @@ -101,6 +103,8 @@ public function testGetMediaAssetFilesDiscoversCustomFileTypes()
$this->file("_media/$path");
$this->assertNotContains($path, MediaFile::files());
self::mockConfig(['hyde.media_extensions' => ['custom']]);

$this->resetKernel(); // Reboot to rediscover new files
$this->assertContains($path, MediaFile::files());
}

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

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

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

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

Expand Down
31 changes: 31 additions & 0 deletions packages/framework/tests/Feature/Foundation/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Framework\Testing\Feature\Foundation;

use Hyde\Foundation\HydeKernel;
use Illuminate\Support\Collection;
use Hyde\Foundation\Kernel\Filesystem;
use Hyde\Foundation\PharSupport;
use Hyde\Hyde;
Expand All @@ -21,6 +22,7 @@
/**
* @covers \Hyde\Foundation\HydeKernel
* @covers \Hyde\Foundation\Kernel\Filesystem
* @covers \Hyde\Foundation\Concerns\HasMediaFiles
*/
class FilesystemTest extends UnitTestCase
{
Expand All @@ -29,6 +31,7 @@ class FilesystemTest extends UnitTestCase
protected Filesystem $filesystem;

protected static bool $needsKernel = true;
protected static bool $needsConfig = true;

protected function setUp(): void
{
Expand Down Expand Up @@ -354,4 +357,32 @@ public function testPathToRelativeHelperDoesNotModifyNonProjectPaths()
$this->assertSame(normalize_slashes($testString), Hyde::pathToRelative($testString));
}
}

public function testAssetsMethodGetsAllSiteAssets()
{
$this->assertEquals(new Collection([
'app.css' => new MediaFile('_media/app.css'),
]), $this->filesystem->assets());
}

public function testAssetsMethodGetsAllSiteAssetsAsArray()
{
$assets = $this->filesystem->assets()->toArray();

$assets['app.css']['length'] = 123;

$this->assertSame([
'app.css' => [
'name' => 'app.css',
'path' => '_media/app.css',
'length' => 123,
'mimeType' => 'text/css',
],
], $assets);
}

public function testAssetsMethodReturnsAssetCollectionSingleton()
{
$this->assertSame($this->filesystem->assets(), $this->filesystem->assets());
}
}
12 changes: 12 additions & 0 deletions packages/framework/tests/Feature/HydeKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ public function testImageHelperSupportsCustomMediaDirectories()
$this->assertSame('assets/foo.jpg', Hyde::asset('foo.jpg'));
}

public function testAssetsHelperGetsAllSiteAssets()
{
$this->assertEquals(new Collection([
'app.css' => new MediaFile('_media/app.css'),
]), Hyde::assets());
}

public function testAssetsHelperReturnsAssetCollectionSingleton()
{
$this->assertSame(Hyde::assets(), Hyde::assets());
}

public function testRouteHelper()
{
$this->assertNotNull(Hyde::route('index'));
Expand Down
Loading