diff --git a/app/config.php b/app/config.php index 009c0f586b7..aed2519bbce 100644 --- a/app/config.php +++ b/app/config.php @@ -105,6 +105,7 @@ 'MarkdownPage' => \Hyde\Pages\MarkdownPage::class, 'MarkdownPost' => \Hyde\Pages\MarkdownPost::class, 'DocumentationPage' => \Hyde\Pages\DocumentationPage::class, + 'MediaFile' => \Hyde\Support\Filesystem\MediaFile::class, 'DataCollection' => \Hyde\Support\DataCollection::class, 'Includes' => \Hyde\Support\Includes::class, 'Feature' => \Hyde\Enums\Feature::class, diff --git a/docs/_data/partials/hyde-pages-api/hyde-kernel-filesystem-methods.md b/docs/_data/partials/hyde-pages-api/hyde-kernel-filesystem-methods.md index bdbe12dd34d..eb5983597e6 100644 --- a/docs/_data/partials/hyde-pages-api/hyde-kernel-filesystem-methods.md +++ b/docs/_data/partials/hyde-pages-api/hyde-kernel-filesystem-methods.md @@ -1,7 +1,7 @@
- + #### `filesystem()` @@ -27,14 +27,6 @@ No description provided. Hyde::vendorPath(string $path, string $package): string ``` -#### `mediaPath()` - -No description provided. - -```php -Hyde::mediaPath(string $path): string -``` - #### `sitePath()` No description provided. @@ -43,14 +35,6 @@ No description provided. Hyde::sitePath(string $path): string ``` -#### `siteMediaPath()` - -No description provided. - -```php -Hyde::siteMediaPath(string $path): string -``` - #### `pathToAbsolute()` No description provided. diff --git a/packages/framework/resources/views/layouts/head.blade.php b/packages/framework/resources/views/layouts/head.blade.php index 32ed4390d8b..111aced665b 100644 --- a/packages/framework/resources/views/layouts/head.blade.php +++ b/packages/framework/resources/views/layouts/head.blade.php @@ -2,7 +2,7 @@ {{ $page->title() }} -@if (file_exists(Hyde::mediaPath('favicon.ico'))) +@if (file_exists(MediaFile::sourcePath('favicon.ico'))) @endif diff --git a/packages/framework/src/Facades/Asset.php b/packages/framework/src/Facades/Asset.php index 9691a43c211..45e16a0a881 100644 --- a/packages/framework/src/Facades/Asset.php +++ b/packages/framework/src/Facades/Asset.php @@ -5,6 +5,7 @@ namespace Hyde\Facades; use Hyde\Hyde; +use Hyde\Support\Filesystem\MediaFile; use function md5_file; use function file_exists; @@ -24,13 +25,13 @@ public static function mediaLink(string $file): string public static function hasMediaFile(string $file): bool { - return file_exists(Hyde::mediaPath($file)); + return file_exists(MediaFile::sourcePath($file)); } protected static function getCacheBustKey(string $file): string { return Config::getBool('hyde.enable_cache_busting', true) - ? '?v='.md5_file(Hyde::mediaPath("$file")) + ? '?v='.md5_file(MediaFile::sourcePath("$file")) : ''; } } diff --git a/packages/framework/src/Foundation/Concerns/ForwardsFilesystem.php b/packages/framework/src/Foundation/Concerns/ForwardsFilesystem.php index 88e8aba452b..cea06f5bbc2 100644 --- a/packages/framework/src/Foundation/Concerns/ForwardsFilesystem.php +++ b/packages/framework/src/Foundation/Concerns/ForwardsFilesystem.php @@ -28,21 +28,11 @@ public function vendorPath(string $path = '', string $package = 'framework'): st return $this->filesystem->vendorPath($path, $package); } - public function mediaPath(string $path = ''): string - { - return $this->filesystem->mediaPath($path); - } - public function sitePath(string $path = ''): string { return $this->filesystem->sitePath($path); } - public function siteMediaPath(string $path = ''): string - { - return $this->filesystem->siteMediaPath($path); - } - public function pathToAbsolute(string|array $path): string|array { return $this->filesystem->pathToAbsolute($path); diff --git a/packages/framework/src/Foundation/Kernel/Filesystem.php b/packages/framework/src/Foundation/Kernel/Filesystem.php index a8f36a32df1..8f3bb2fd2aa 100644 --- a/packages/framework/src/Foundation/Kernel/Filesystem.php +++ b/packages/framework/src/Foundation/Kernel/Filesystem.php @@ -91,18 +91,6 @@ public function pathToRelative(string $path): string : $path); } - /** - * Get the absolute path to the media source directory, or a file within it. - */ - public function mediaPath(string $path = ''): string - { - if (empty($path)) { - return $this->path(Hyde::getMediaDirectory()); - } - - return $this->path(path_join(Hyde::getMediaDirectory(), unslash($path))); - } - /** * Get the absolute path to the compiled site directory, or a file within it. */ @@ -115,20 +103,6 @@ public function sitePath(string $path = ''): string return $this->path(path_join(Hyde::getOutputDirectory(), unslash($path))); } - /** - * Get the absolute path to the compiled site's media directory, or a file within it. - */ - public function siteMediaPath(string $path = ''): string - { - if (empty($path)) { - return $this->sitePath(Hyde::getMediaOutputDirectory()); - } - - $path = unslash($path); - - return $this->sitePath(Hyde::getMediaOutputDirectory()."/$path"); - } - /** * Works similarly to the path() function, but returns a file in the Framework package. * diff --git a/packages/framework/src/Framework/Actions/PreBuildTasks/CleanSiteDirectory.php b/packages/framework/src/Framework/Actions/PreBuildTasks/CleanSiteDirectory.php index 849f077b7cb..ba64f24ee76 100644 --- a/packages/framework/src/Framework/Actions/PreBuildTasks/CleanSiteDirectory.php +++ b/packages/framework/src/Framework/Actions/PreBuildTasks/CleanSiteDirectory.php @@ -7,6 +7,7 @@ use Hyde\Hyde; use Hyde\Facades\Config; use Hyde\Facades\Filesystem; +use Hyde\Support\Filesystem\MediaFile; use Hyde\Framework\Features\BuildTasks\PreBuildTask; use function basename; @@ -22,7 +23,7 @@ public function handle(): void { if ($this->isItSafeToCleanOutputDirectory()) { Filesystem::unlink(glob(Hyde::sitePath('*.{html,json}'), GLOB_BRACE)); - Filesystem::cleanDirectory(Hyde::siteMediaPath()); + Filesystem::cleanDirectory(MediaFile::outputPath()); } } diff --git a/packages/framework/src/Framework/Actions/PreBuildTasks/TransferMediaAssets.php b/packages/framework/src/Framework/Actions/PreBuildTasks/TransferMediaAssets.php index fdec03a75ff..3acb112ea9d 100644 --- a/packages/framework/src/Framework/Actions/PreBuildTasks/TransferMediaAssets.php +++ b/packages/framework/src/Framework/Actions/PreBuildTasks/TransferMediaAssets.php @@ -4,7 +4,6 @@ namespace Hyde\Framework\Actions\PreBuildTasks; -use Hyde\Hyde; use Hyde\Support\Filesystem\MediaFile; use Hyde\Framework\Features\BuildTasks\PreBuildTask; use Hyde\Framework\Concerns\InteractsWithDirectories; @@ -17,14 +16,14 @@ class TransferMediaAssets extends PreBuildTask public function handle(): void { - $this->needsDirectory(Hyde::siteMediaPath()); + $this->needsDirectory(MediaFile::outputPath()); $this->newLine(); $this->withProgressBar(MediaFile::files(), function (string $identifier): void { - $sitePath = Hyde::siteMediaPath($identifier); + $sitePath = MediaFile::outputPath($identifier); $this->needsParentDirectory($sitePath); - copy(Hyde::mediaPath($identifier), $sitePath); + copy(MediaFile::sourcePath($identifier), $sitePath); }); $this->newLine(); diff --git a/packages/framework/src/Framework/Features/Blogging/Models/FeaturedImage.php b/packages/framework/src/Framework/Features/Blogging/Models/FeaturedImage.php index 0f42f961c87..5c4acef1996 100644 --- a/packages/framework/src/Framework/Features/Blogging/Models/FeaturedImage.php +++ b/packages/framework/src/Framework/Features/Blogging/Models/FeaturedImage.php @@ -11,6 +11,7 @@ use Hyde\Support\BuildWarnings; use Illuminate\Support\Facades\Http; use Hyde\Foundation\Kernel\Hyperlinks; +use Hyde\Support\Filesystem\MediaFile; use Hyde\Framework\Exceptions\FileNotFoundException; use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\FeaturedImageSchema; @@ -213,7 +214,7 @@ protected function has(string $property): bool protected function getContentLengthForLocalImage(): int { - $storagePath = Hyde::mediaPath($this->source); + $storagePath = MediaFile::sourcePath($this->source); if (! file_exists($storagePath)) { throw new FileNotFoundException(customMessage: sprintf('Featured image [%s] not found.', Hyde::pathToRelative($storagePath))); diff --git a/packages/framework/src/Framework/Services/ValidationService.php b/packages/framework/src/Framework/Services/ValidationService.php index 88deee3496b..052486d4041 100644 --- a/packages/framework/src/Framework/Services/ValidationService.php +++ b/packages/framework/src/Framework/Services/ValidationService.php @@ -11,6 +11,7 @@ use Hyde\Pages\MarkdownPage; use Hyde\Pages\DocumentationPage; use Hyde\Enums\Feature; +use Hyde\Support\Filesystem\MediaFile; use Hyde\Support\Models\ValidationResult as Result; use function count; @@ -102,12 +103,12 @@ public function check_documentation_site_has_an_index_page(Result $result): Resu public function check_site_has_an_app_css_stylesheet(Result $result): Result { - if (file_exists(Hyde::siteMediaPath('/app.css')) || file_exists(Hyde::mediaPath('app.css'))) { + if (file_exists(MediaFile::outputPath('/app.css')) || file_exists(MediaFile::sourcePath('app.css'))) { return $result->pass('Your site has an app.css stylesheet'); } return $result->fail(sprintf('Could not find an app.css file in the %s or %s directory!', - Hyde::pathToRelative(Hyde::siteMediaPath()), Hyde::getMediaDirectory() + Hyde::pathToRelative(MediaFile::outputPath()), Hyde::getMediaDirectory() ))->withTip('You may need to run `npm run dev`.`'); } diff --git a/packages/framework/src/Hyde.php b/packages/framework/src/Hyde.php index cc309a3b21c..9590fb6133a 100644 --- a/packages/framework/src/Hyde.php +++ b/packages/framework/src/Hyde.php @@ -7,6 +7,7 @@ use Hyde\Enums\Feature; use Hyde\Facades\Features; use Hyde\Foundation\HydeKernel; +use Hyde\Support\V1Compatibility; use Illuminate\Support\Collection; use Hyde\Foundation\Kernel\FileCollection; use Hyde\Foundation\Kernel\Filesystem; @@ -33,8 +34,6 @@ * @method static string pathToAbsolute(string $path) * @method static string pathToRelative(string $path) * @method static string sitePath(string $path = '') - * @method static string mediaPath(string $path = '') - * @method static string siteMediaPath(string $path = '') * @method static string formatLink(string $destination) * @method static string relativeLink(string $destination) * @method static string mediaLink(string $destination, bool $validate = false) @@ -79,6 +78,8 @@ */ class Hyde extends Facade { + use V1Compatibility; + public static function version(): string { return HydeKernel::version(); diff --git a/packages/framework/src/Support/Filesystem/MediaFile.php b/packages/framework/src/Support/Filesystem/MediaFile.php index 45298a50694..3ed834e7562 100644 --- a/packages/framework/src/Support/Filesystem/MediaFile.php +++ b/packages/framework/src/Support/Filesystem/MediaFile.php @@ -9,6 +9,8 @@ use Hyde\Framework\Exceptions\FileNotFoundException; use Illuminate\Support\Str; +use function Hyde\unslash; +use function Hyde\path_join; use function extension_loaded; use function file_exists; use function array_merge; @@ -32,7 +34,7 @@ class MediaFile extends ProjectFile /** @return array The array keys are the filenames relative to the _media/ directory */ public static function all(): array { - return static::discoverMediaAssetFiles(); + return static::discoverMediaFiles(); } /** @return array Array of filenames relative to the _media/ directory */ @@ -41,6 +43,32 @@ public static function files(): array return array_keys(static::all()); } + /** + * Get the absolute path to the media source directory, or a file within it. + */ + public static function sourcePath(string $path = ''): string + { + if (empty($path)) { + return Hyde::path(Hyde::getMediaDirectory()); + } + + return Hyde::path(path_join(Hyde::getMediaDirectory(), unslash($path))); + } + + /** + * Get the absolute path to the compiled site's media directory, or a file within it. + */ + public static function outputPath(string $path = ''): string + { + if (empty($path)) { + return Hyde::sitePath(Hyde::getMediaOutputDirectory()); + } + + $path = unslash($path); + + return Hyde::sitePath(Hyde::getMediaOutputDirectory()."/$path"); + } + public function getIdentifier(): string { return Str::after($this->getPath(), Hyde::getMediaDirectory().'/'); @@ -95,16 +123,16 @@ public function getMimeType(): string return 'text/plain'; } - protected static function discoverMediaAssetFiles(): array + protected static function discoverMediaFiles(): array { - return collect(static::getMediaAssetFiles())->mapWithKeys(function (string $path): array { + return collect(static::getMediaFiles())->mapWithKeys(function (string $path): array { $file = static::make($path); return [$file->getIdentifier() => $file]; })->all(); } - protected static function getMediaAssetFiles(): array + protected static function getMediaFiles(): array { return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: []; } diff --git a/packages/framework/src/Support/V1Compatibility.php b/packages/framework/src/Support/V1Compatibility.php new file mode 100644 index 00000000000..3d8f5b5cd0c --- /dev/null +++ b/packages/framework/src/Support/V1Compatibility.php @@ -0,0 +1,38 @@ +assertSame(Hyde::path('_media'), Hyde::mediaPath()); - } + $this->assertSame(Hyde::path('_media'), MediaFile::sourcePath()); + $this->assertSame(MediaFile::sourcePath(), MediaFile::sourcePath()); - public function testHelperForMediaPathReturnsPathToFileWithinTheDirectory() - { - $this->assertSame(Hyde::path('_media/foo.css'), Hyde::mediaPath('foo.css')); - } - - public function testGetMediaPathReturnsAbsolutePath() - { - $this->assertSame(Hyde::path('_media'), Hyde::mediaPath()); + $this->assertSame(Hyde::path('_media/foo.png'), MediaFile::sourcePath('foo.png')); + $this->assertSame(MediaFile::sourcePath('foo.png'), MediaFile::sourcePath('foo.png')); } public function testHelperForMediaOutputPath() { - $this->assertSame(Hyde::path('_site/media'), Hyde::siteMediaPath()); - } + $this->assertSame(Hyde::path('_site/media'), MediaFile::outputPath()); + $this->assertSame(MediaFile::outputPath(), MediaFile::outputPath()); - public function testHelperForMediaOutputPathReturnsPathToFileWithinTheDirectory() - { - $this->assertSame(Hyde::path('_site/media/foo.css'), Hyde::siteMediaPath('foo.css')); - } - - public function testGetMediaOutputPathReturnsAbsolutePath() - { - $this->assertSame(Hyde::path('_site/media'), Hyde::siteMediaPath()); + $this->assertSame(Hyde::path('_site/media/foo.png'), MediaFile::outputPath('foo.png')); + $this->assertSame(MediaFile::outputPath('foo.png'), MediaFile::outputPath('foo.png')); } public function testHelperForSiteOutputPath() diff --git a/packages/framework/tests/Feature/HydeKernelTest.php b/packages/framework/tests/Feature/HydeKernelTest.php index 8c1470b4206..b12a5ea19bb 100644 --- a/packages/framework/tests/Feature/HydeKernelTest.php +++ b/packages/framework/tests/Feature/HydeKernelTest.php @@ -13,6 +13,7 @@ use Hyde\Foundation\HydeKernel; use Hyde\Enums\Feature; use Hyde\Foundation\Kernel\Filesystem; +use Hyde\Support\Filesystem\MediaFile; use Hyde\Framework\HydeServiceProvider; use Hyde\Hyde; use Hyde\Pages\BladePage; @@ -262,9 +263,9 @@ public function testFluentModelSourcePathHelpers() $this->assertSame(Hyde::path('_pages'), MarkdownPage::path()); $this->assertSame(Hyde::path('_docs'), DocumentationPage::path()); - $this->assertSame(Hyde::path('_media'), Hyde::mediaPath()); + $this->assertSame(Hyde::path('_media'), MediaFile::sourcePath()); + $this->assertSame(Hyde::path('_site/media'), MediaFile::outputPath()); $this->assertSame(Hyde::path('_site'), Hyde::sitePath()); - $this->assertSame(Hyde::path('_site/media'), Hyde::siteMediaPath()); } public function testPathToRelativeHelperReturnsRelativePathForGivenPath() @@ -391,25 +392,6 @@ public function testGetMediaOutputDirectoryNameUsesTrimmedVersionOfMediaSourceDi $this->assertSame('foo', Hyde::getMediaOutputDirectory()); } - public function testCanGetSiteMediaOutputDirectory() - { - $this->assertSame(Hyde::path('_site/media'), Hyde::siteMediaPath()); - } - - public function testGetSiteMediaOutputDirectoryUsesTrimmedVersionOfMediaSourceDirectory() - { - Hyde::setMediaDirectory('_foo'); - $this->assertSame(Hyde::path('_site/foo'), Hyde::siteMediaPath()); - } - - public function testGetSiteMediaOutputDirectoryUsesConfiguredSiteOutputDirectory() - { - Hyde::setOutputDirectory(Hyde::path('foo')); - Hyde::setMediaDirectory('bar'); - - $this->assertSame(Hyde::path('foo/bar'), Hyde::siteMediaPath()); - } - public function testMediaOutputDirectoryCanBeChangedInConfiguration() { $this->assertSame('_media', Hyde::getMediaDirectory()); diff --git a/packages/framework/tests/Feature/Support/MediaFileTest.php b/packages/framework/tests/Unit/Support/MediaFileTest.php similarity index 74% rename from packages/framework/tests/Feature/Support/MediaFileTest.php rename to packages/framework/tests/Unit/Support/MediaFileTest.php index 0782bfaf3bc..44ded0474c5 100644 --- a/packages/framework/tests/Feature/Support/MediaFileTest.php +++ b/packages/framework/tests/Unit/Support/MediaFileTest.php @@ -2,19 +2,30 @@ declare(strict_types=1); -namespace Hyde\Framework\Testing\Feature\Support; +namespace Hyde\Framework\Testing\Unit\Support; use Hyde\Facades\Filesystem; use Hyde\Framework\Exceptions\FileNotFoundException; use Hyde\Hyde; use Hyde\Support\Filesystem\MediaFile; -use Hyde\Testing\TestCase; +use Hyde\Testing\UnitTestCase; +use Hyde\Testing\CreatesTemporaryFiles; /** * @covers \Hyde\Support\Filesystem\MediaFile */ -class MediaFileTest extends TestCase +class MediaFileTest extends UnitTestCase { + use CreatesTemporaryFiles; + + protected static bool $needsKernel = true; + protected static bool $needsConfig = true; + + protected function tearDown(): void + { + $this->cleanUpFilesystem(); + } + public function testCanConstruct() { $file = new MediaFile('foo'); @@ -200,4 +211,53 @@ public function testGetIdentifierWithSubdirectory() { $this->assertSame('foo/bar', MediaFile::make('foo/bar')->getIdentifier()); } + + public function testHelperForMediaPath() + { + $this->assertSame(Hyde::path('_media'), MediaFile::sourcePath()); + } + + public function testHelperForMediaPathReturnsPathToFileWithinTheDirectory() + { + $this->assertSame(Hyde::path('_media/foo.css'), MediaFile::sourcePath('foo.css')); + } + + public function testGetMediaPathReturnsAbsolutePath() + { + $this->assertSame(Hyde::path('_media'), MediaFile::sourcePath()); + } + + public function testHelperForMediaOutputPath() + { + $this->assertSame(Hyde::path('_site/media'), MediaFile::outputPath()); + } + + public function testHelperForMediaOutputPathReturnsPathToFileWithinTheDirectory() + { + $this->assertSame(Hyde::path('_site/media/foo.css'), MediaFile::outputPath('foo.css')); + } + + public function testGetMediaOutputPathReturnsAbsolutePath() + { + $this->assertSame(Hyde::path('_site/media'), MediaFile::outputPath()); + } + + public function testCanGetSiteMediaOutputDirectory() + { + $this->assertSame(Hyde::path('_site/media'), MediaFile::outputPath()); + } + + public function testGetSiteMediaOutputDirectoryUsesTrimmedVersionOfMediaSourceDirectory() + { + Hyde::setMediaDirectory('_foo'); + $this->assertSame(Hyde::path('_site/foo'), MediaFile::outputPath()); + } + + public function testGetSiteMediaOutputDirectoryUsesConfiguredSiteOutputDirectory() + { + Hyde::setOutputDirectory(Hyde::path('foo')); + Hyde::setMediaDirectory('bar'); + + $this->assertSame(Hyde::path('foo/bar'), MediaFile::outputPath()); + } } diff --git a/spec.md b/spec.md index a1ca493ac41..cb78e76674c 100644 --- a/spec.md +++ b/spec.md @@ -78,17 +78,17 @@ Asset::hasMediaFile(string $file) // Returns file_exists(Hyde::mediaPath($file)) ```php Hyde::mediaPath() // Get the absolute path to the media source directory, or a file within it. (Intended as a sibling to Hyde::path and Hyde::sitePath helpers, and the HydePage::path methods) +Hyde::siteMediaPath() // Get the absolute path to the compiled site's media directory, or a file within it. (Intended as a sibling to Hyde::sitePath) Hyde::mediaLink() // Gets a relative web link to the given file stored in the _site/media folder. (Intended as a sibling to Hyde::relativeLink, which is called by this) (A second $validate parameter will return throw an exception if the file does not exist) Hyde::asset() // Gets a relative web link to the given image stored in the _site/media folder. (But leaves remote URLs alone) (A second $preferQualifiedUrl parameter will return a fully qualified URL if a site URL is set) -Hyde::siteMediaPath() // Get the absolute path to the compiled site's media directory, or a file within it. (Intended as a sibling to Hyde::sitePath) /** * Real implementations of the facade methods: * * @see \Hyde\Foundation\HydeKernel::mediaPath {@see \Hyde\Foundation\Kernel\Filesystem::mediaPath} + * @see \Hyde\Foundation\HydeKernel::siteMediaPath {@see \Hyde\Foundation\Kernel\Filesystem::siteMediaPath} * @see \Hyde\Foundation\HydeKernel::mediaLink {@see \Hyde\Foundation\Kernel\Hyperlinks::mediaLink} * @see \Hyde\Foundation\HydeKernel::asset {@see \Hyde\Foundation\Kernel\Hyperlinks::asset} - * @see \Hyde\Foundation\HydeKernel::siteMediaPath {@see \Hyde\Foundation\Kernel\Filesystem::siteMediaPath} */ ```