Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
96b1856
Add short and concise PHPDoc annotations to the public methods
emmadesilva Aug 4, 2024
ed428cb
Narrow down mixed return type
emmadesilva Aug 4, 2024
7f53ba7
Use array shape annotation to match parent
emmadesilva Aug 4, 2024
7c6739f
Specify hash type
emmadesilva Aug 4, 2024
687b119
Remove redundant PHPDoc comments
emmadesilva Aug 4, 2024
5c77557
Add new helper to get a relative web link to the media file
emmadesilva Aug 4, 2024
6f5e537
Make protected method public static internal
emmadesilva Aug 4, 2024
13affca
Convert string interpolation to concatenation
emmadesilva Aug 4, 2024
b588a88
Access kernel using facade
emmadesilva Aug 4, 2024
cb98c50
Revert "Access kernel using facade"
emmadesilva Aug 4, 2024
78c02f8
Revert "Convert string interpolation to concatenation"
emmadesilva Aug 4, 2024
7aa2d6a
Deprecate the media link helper
emmadesilva Aug 5, 2024
ab9e7aa
Convert string interpolation to concatenation
emmadesilva Aug 5, 2024
1814937
Access kernel using facade
emmadesilva Aug 5, 2024
1deffbd
Access methods using facade
emmadesilva Aug 5, 2024
6c350f0
Call class name instead of static
emmadesilva Aug 5, 2024
20a7f11
Copy the asset logic to the media file class
emmadesilva Aug 5, 2024
eefd67f
Remove unreachable remote check from method
emmadesilva Aug 5, 2024
c48a731
Begin adding back support for fileless media file instances
emmadesilva Aug 20, 2024
d0212c1
Revert "Begin adding back support for fileless media file instances"
emmadesilva Aug 20, 2024
b7fc928
Document why we must validate file existence
emmadesilva Aug 20, 2024
ad58ca7
Merge branch 'normalize-the-asset-api' into improve-the-media-file-in…
emmadesilva Aug 20, 2024
9355134
Resolve asset links using container to throw when they don't exist
emmadesilva Aug 20, 2024
f64bf84
Reorder methods
emmadesilva Aug 20, 2024
23a49e2
Make the media file class stringable
emmadesilva Aug 20, 2024
438bbd4
Merge branch 'normalize-the-asset-api' into improve-the-media-file-in…
emmadesilva Sep 1, 2024
be2e212
Remove qualified URL normalization from the asset helper
emmadesilva Sep 2, 2024
0f9cd05
Fix wrong order of expected and actual
emmadesilva Sep 2, 2024
32d5dc9
Update outdated test names
emmadesilva Sep 2, 2024
36a2343
Major: Change the asset helper to return MediaFile instances
emmadesilva Sep 2, 2024
9bb8e6c
Create AssetAPIFeatureTest.php
emmadesilva Sep 2, 2024
001cd3a
Test Asset API usages in Blade views
emmadesilva Sep 2, 2024
d7bdc49
Swap method declaration order
emmadesilva Sep 2, 2024
09243b8
Add `MediaFile::get()` method
emmadesilva Sep 2, 2024
e89b7d5
Fall back to make the instance
emmadesilva Sep 2, 2024
17edb9b
Inject language reference
emmadesilva Sep 2, 2024
da1941e
Test Asset API examples
emmadesilva Sep 2, 2024
7504248
Extract helper method
emmadesilva Sep 2, 2024
5a4030f
Test with all Kernel accessors
emmadesilva Sep 2, 2024
861b254
Provide autocomplete for media file asset getters
emmadesilva Sep 2, 2024
ff439e1
Provide autocomplete for media file asset accessors
emmadesilva Sep 2, 2024
bcacc4d
Update AssetAPIFeatureTest.php
emmadesilva Sep 2, 2024
a6cbbb2
Clarify method documentation
emmadesilva Sep 2, 2024
14a792f
Override parent method to get custom child documentation
emmadesilva Sep 2, 2024
81c387e
Normalize parameter name
emmadesilva Sep 2, 2024
e9baf28
Document base path relation
emmadesilva Sep 2, 2024
48a9551
Normalize exception message
emmadesilva Sep 2, 2024
e80e7f3
Update the kernel asset helper to use the media file helper
emmadesilva Sep 2, 2024
85bfcc9
Replace the `mediaLink` helpers with MediaFile getter
emmadesilva Sep 2, 2024
29ba2ca
Revert "Replace the `mediaLink` helpers with MediaFile getter"
emmadesilva Sep 3, 2024
0ffe14f
Move internal method to more suitable location
emmadesilva Sep 3, 2024
dd6366c
Deprecate internal static method for refactor
emmadesilva Sep 3, 2024
2c24add
Create new instance helper to consistently add cache busting keys
emmadesilva Sep 3, 2024
34d16d9
Remove the `validate` parameter from the `mediaLink` method
emmadesilva Jul 26, 2024
d789fda
Revert "Remove the `validate` parameter from the `mediaLink` method"
emmadesilva Sep 4, 2024
824ccfb
Add new method to the Filesystem facade to get mime types
emmadesilva Sep 7, 2024
78fb6d2
Use the new Filesystem helper for mime type detection
emmadesilva Sep 7, 2024
2284cfa
Improve method documentation
emmadesilva Sep 7, 2024
1da49a2
Test all cases of the mime type detection method
emmadesilva Sep 7, 2024
2d4c2d9
Extract unit test
emmadesilva Sep 7, 2024
6ef5d18
Split out to two test methods
emmadesilva Sep 7, 2024
935a718
Use dataproviders for the extensions to test
emmadesilva Sep 7, 2024
ebd7be6
Move more assertions to the unit test
emmadesilva Sep 7, 2024
d7a3a28
Split out each assertion group to its own test method
emmadesilva Sep 7, 2024
1a5ed00
Add two initial high level tests
emmadesilva Sep 7, 2024
a329652
Update the RSS feed generator to use the new Filesystem facade
emmadesilva Sep 7, 2024
fdf4341
Get Assets via the Asset API instead of using deprecated media links
emmadesilva Sep 7, 2024
86715b0
Migrate tests using deprecated media link helper to use new Asset API
emmadesilva Sep 7, 2024
5ff2550
Breaking: Remove the `mediaLink` helper replaced by the Asset API
emmadesilva Sep 7, 2024
ff18348
Remove deprecated withCacheBusting method
emmadesilva Sep 7, 2024
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-hyperlink-methods">

<!-- Start generated docs for Hyde\Foundation\Concerns\ForwardsHyperlinks -->
<!-- Generated by HydePHP DocGen script at 2024-07-26 18:20:22 in 0.13ms -->
<!-- Generated by HydePHP DocGen script at 2024-09-04 19:44:45 in 0.10ms -->

#### `formatLink()`

Expand All @@ -19,20 +19,12 @@ No description provided.
Hyde::relativeLink(string $destination): string
```

#### `mediaLink()`

No description provided.

```php
Hyde::mediaLink(string $destination, bool $validate): string
```

#### `asset()`

No description provided.

```php
Hyde::asset(string $name): string
Hyde::asset(string $name): Hyde\Support\Filesystem\MediaFile
```

#### `url()`
Expand Down
8 changes: 0 additions & 8 deletions docs/architecture-concepts/the-hydekernel.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,6 @@ No description provided.
Hyde::relativeLink(string $destination): string
```

#### `mediaLink()`

No description provided.

```php
Hyde::mediaLink(string $destination, bool $validate): string
```

#### `asset()`

No description provided.
Expand Down
29 changes: 29 additions & 0 deletions ide.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@
}
]
},
{
"complete": "directoryFiles",
"options": {
"directory": "/_media"
},
"condition": [
{
"methodNames": ["get", "hasMediaFile"],
"place": "parameter",
"classFqn": ["\\Hyde\\Facades\\Asset"],
"parameters": [1]
},
{
"methodNames": ["__construct", "get", "make", "sourcePath", "outputPath"],
"place": "parameter",
"classFqn": ["Hyde\\Support\\Filesystem\\MediaFile"],
"parameters": [1]
}, {
"methodNames": ["asset"],
"place": "parameter",
"classFqn": ["Hyde\\Foundation\\HydeKernel", "\\Hyde\\Hyde", "Hyde"],
"parameters": [1]
}, {
"functionNames": ["asset"],
"place": "parameter",
"parameters": [1]
}
]
},
{
"complete": "configKey",
"condition": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{-- The compiled Laravel Mix scripts --}}
@if(Asset::hasMediaFile('app.js'))
<script defer src="{{ Asset::mediaLink('app.js') }}"></script>
<script defer src="{{ Asset::get('app.js') }}"></script>
@endif

{{-- Alpine.js --}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@if(config('hyde.load_app_styles_from_cdn', false))
<link rel="stylesheet" href="{{ HydeFront::cdnLink('app.css') }}">
@elseif(Asset::hasMediaFile('app.css'))
<link rel="stylesheet" href="{{ Asset::mediaLink('app.css') }}">
<link rel="stylesheet" href="{{ Asset::get('app.css') }}">
@endif

{{-- Dynamic TailwindCSS Play CDN --}}
Expand Down
7 changes: 1 addition & 6 deletions packages/framework/src/Facades/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@
*/
class Asset
{
public static function get(string $file): string
public static function get(string $file): MediaFile
{
return hyde()->asset($file);
}

public static function mediaLink(string $file): string
{
return hyde()->mediaLink($file);
}

public static function hasMediaFile(string $file): bool
{
return file_exists(MediaFile::sourcePath($file));
Expand Down
36 changes: 36 additions & 0 deletions packages/framework/src/Facades/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,42 @@ public static function putContents(string $path, string $contents, bool $lock =
return self::put($path, $contents, $lock);
}

/**
* Improved mime type detection for the given path that can be relative to the project root,
* or a remote URL where this will try to guess the mime type based on the file extension.
*
* @param string $path The path to the file, relative to the project root or a remote URL.
*/
public static function findMimeType(string $path): string
{
$extension = self::extension($path);

$lookup = [
'txt' => 'text/plain',
'md' => 'text/markdown',
'html' => 'text/html',
'css' => 'text/css',
'svg' => 'image/svg+xml',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'json' => 'application/json',
'js' => 'application/javascript',
'xml' => 'application/xml',
];

if (isset($lookup[$extension])) {
return $lookup[$extension];
}

if (extension_loaded('fileinfo') && self::exists($path)) {
return self::mimeType($path);
}

return 'text/plain';
}

protected static function filesystem(): \Illuminate\Filesystem\Filesystem
{
return app(\Illuminate\Filesystem\Filesystem::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Foundation\Concerns;

use Hyde\Support\Models\Route;
use Hyde\Support\Filesystem\MediaFile;

/**
* @internal Single-use trait for the HydeKernel class.
Expand All @@ -23,12 +24,7 @@ public function relativeLink(string $destination): string
return $this->hyperlinks->relativeLink($destination);
}

public function mediaLink(string $destination, bool $validate = false): string
{
return $this->hyperlinks->mediaLink($destination, $validate);
}

public function asset(string $name): string
public function asset(string $name): MediaFile
{
return $this->hyperlinks->asset($name);
}
Expand Down
46 changes: 7 additions & 39 deletions packages/framework/src/Foundation/Kernel/Hyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
use Hyde\Foundation\HydeKernel;
use Hyde\Support\Filesystem\MediaFile;
use Hyde\Framework\Exceptions\FileNotFoundException;
use Illuminate\Support\Str;

use function str_ends_with;
use function str_starts_with;
use function substr_count;
use function file_exists;
use function str_replace;
use function str_repeat;
use function substr;
Expand Down Expand Up @@ -89,39 +87,17 @@ public function relativeLink(string $destination): string
}

/**
* Gets a relative web link to the given file stored in the _site/media folder.
*
* An exception will be thrown if the file does not exist in the _media directory,
* and the second argument is set to true.
*/
public function mediaLink(string $destination, bool $validate = false): string
{
if ($validate && ! file_exists($sourcePath = "{$this->kernel->getMediaDirectory()}/$destination")) {
throw new FileNotFoundException($sourcePath);
}

return $this->withCacheBusting($this->relativeLink("{$this->kernel->getMediaOutputDirectory()}/$destination"), $destination);
}

/**
* Gets a relative web link to the given file stored in the `_site/media` folder.
* If the image is already qualified (starts with `http`) it will be returned as is.
* Gets a MediaAsset instance for the given file stored in the `_site/media` folder.
* The returned value can be cast into a string in Blade views to resole the URL.
*
* If a base URL is configured, the image will be returned with a qualified absolute URL.
* Otherwise, a relative path will be returned based on the rendered page's location.
*
* @throws FileNotFoundException If the file does not exist in the `_media` directory in order to make the issue clear.
*/
public function asset(string $name): string
public function asset(string $name): MediaFile
{
if (static::isRemote($name)) {
return $name;
}

$name = Str::start($name, "{$this->kernel->getMediaOutputDirectory()}/");

if ($this->hasSiteUrl()) {
return $this->withCacheBusting($this->url($name), $name);
}

return $this->withCacheBusting($this->relativeLink($name), $name);
return MediaFile::get($name);
}

/**
Expand Down Expand Up @@ -181,12 +157,4 @@ public static function isRemote(string $url): bool
{
return str_starts_with($url, 'http') || str_starts_with($url, '//');
}

/**
* Apply cache to the URL if enabled in the configuration.
*/
protected function withCacheBusting(string $url, string $file): string
{
return $url.MediaFile::getCacheBustKey($file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getSource(): string
{
if ($this->type === self::TYPE_LOCAL) {
// Return value is always resolvable from a compiled page in the _site directory.
return Hyde::mediaLink($this->source);
return (string) Hyde::asset($this->source);
}

return $this->source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Hyde\Facades\Site;
use Hyde\Facades\Config;
use Hyde\Pages\MarkdownPost;
use Hyde\Support\Filesystem\MediaFile;
use Hyde\Facades\Filesystem;
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;

use function date;
Expand Down Expand Up @@ -108,7 +108,7 @@ protected function addAtomLinkItem(): void

protected function getImageType(FeaturedImage $image): string
{
return (new MediaFile($image->getSource()))->getMimeType();
return Filesystem::findMimeType($image->getSource());
}

/** @return numeric-string */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected static function assetMap(): array

protected static function assetPath(MediaFile $mediaFile): string
{
return Hyde::asset(Str::after($mediaFile->getPath(), '_media/'));
return Hyde::asset(Str::after($mediaFile->getPath(), '_media/'))->getLink();
}

/** @internal Testing helper to reset the asset map cache. */
Expand Down
Loading