Skip to content

Commit 4622237

Browse files
authored
Merge pull request #1904 from hydephp/normalize-the-asset-api
[2.x] Rewrite the Asset API
2 parents 0cacea4 + 40cffc6 commit 4622237

File tree

70 files changed

+2468
-916
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2468
-916
lines changed

RELEASE_NOTES.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ This serves two purposes:
2323
- You can now add custom posts to the blog post feed component when including it directly in https://github.com/hydephp/develop/pull/1893
2424
- Added a `Feature::fromName()` enum helper in https://github.com/hydephp/develop/pull/1895
2525
- Added support for specifying features in the YAML configuration in https://github.com/hydephp/develop/pull/1896
26+
- **Added a new consolidated Asset API to better handle media files.**
27+
- Added several new fluent methods to the `MediaFile` class, like `getLink()`, `getLength()`, `getMimeType()`, etc.
28+
- Added new `HydeFront` facade to handle CDN links and Tailwind config injection.
29+
- Added method `Asset::exists()` has to check if a media file exists.
30+
- Added a `Hyde::assets()` method to get all media file instancess in the site.
31+
2632

2733
### Changed
2834
- **Breaking:** The internals of the navigation system has been rewritten into a new Navigation API. This change is breaking for custom navigation implementations. For more information, see below.
@@ -33,6 +39,7 @@ This serves two purposes:
3339
- **Breaking:** The `Author::get()` method now returns `null` if an author is not found, rather than creating a new instance. For more information, see below.
3440
- **Breaking:** The custom navigation item configuration now uses array inputs instead of the previous format. For more information, see the upgrade guide below.
3541
- **Breaking:** Renamed the `hyde.navigation.subdirectories` configuration option to `hyde.navigation.subdirectory_display`.
42+
- **Breaking:** Renamed the `hyde.enable_cache_busting` configuration option to `hyde.cache_busting` in https://github.com/hydephp/develop/pull/1980
3643
- Medium: The `route` function will now throw a `RouteNotFoundException` if the route does not exist in https://github.com/hydephp/develop/pull/1741
3744
- Minor: Navigation menu items are now no longer filtered by duplicates (meaning two items with the same label can now exist in the same menu) in https://github.com/hydephp/develop/pull/1573
3845
- Minor: Due to changes in the navigation system, it is possible that existing configuration files will need to be adjusted in order for menus to look the same (in terms of ordering etc.)
@@ -60,6 +67,21 @@ This serves two purposes:
6067
- The build command now groups together all `InMemoryPage` instances under one progress bar group in https://github.com/hydephp/develop/pull/1897
6168
- The `Markdown::render()` method will now always render Markdown using the custom HydePHP Markdown service (thus getting smart features like our Markdown processors) in https://github.com/hydephp/develop/pull/1900
6269
- Improved how the `MarkdownService` class is accessed, by binding it into the service container, in https://github.com/hydephp/develop/pull/1922
70+
- Improved the media asset transfer build task to have better output in https://github.com/hydephp/develop/pull/1904
71+
- **Many MediaFile related helpers has been changed or completely rewritten** to provide a simplified API for interacting with media files.
72+
- **Note:** For most end users, the changes will have minimal direct impact, but if you have custom code that interacts with media files, you may need to update it.
73+
- The `Asset` facade has been e restructured to be more scoped and easier to use, splitting out a separate `HydeFront` facade and inlining the `AssetService` class.
74+
- All asset retrieval methods now return a `MediaFile` instance, which can be fluently interacted with, or cast to a string to get the link (which was the previous behavior).
75+
- The `Hyde::asset()` method and `asset()` function now return `MediaFile` instances instead of strings, and will throw an exception if the asset does not exist.
76+
- Renamed method `Asset::hasMediaFile` to `Asset::exists` in https://github.com/hydephp/develop/pull/1957
77+
- Renamed method `MediaFile::getContentLength` to `MediaFile::getLength` in https://github.com/hydephp/develop/pull/1904
78+
- Replaced method `Hyde::mediaPath` with `MediaFile::sourcePath` in https://github.com/hydephp/develop/pull/1911
79+
- Replaced method `Hyde::siteMediaPath` with `MediaFile::outputPath` in https://github.com/hydephp/develop/pull/1911
80+
- We will now throw an exception if you try to get a media file that does not exist in order to prevent missing assets from going unnoticed in https://github.com/hydephp/develop/pull/1932
81+
- **MediaFile performance improvements:**
82+
- Media assets are now cached in the HydeKernel, giving a massive performance boost and making it easier to access the instances in https://github.com/hydephp/develop/pull/1917
83+
- Media file metadata is now lazy loaded and then cached in memory, providing performance improvements for files that may not be used in a build in https://github.com/hydephp/develop/pull/1933
84+
- We now use the much faster `CRC32` hashing algoritm instead of `MD5` for cache busting keys in https://github.com/hydephp/develop/pull/1918
6385

6486
### Deprecated
6587
- for soon-to-be removed features.
@@ -72,6 +94,13 @@ This serves two purposes:
7294
- Removed: The deprecated `PostAuthor::getName()` method is now removed (use `$author->name`) in https://github.com/hydephp/develop/pull/1782
7395
- Internal: Removed the internal `DocumentationSearchPage::generate()` method as it was unused in https://github.com/hydephp/develop/pull/1569
7496
- Removed the deprecated `FeaturedImage::isRemote()` method in https://github.com/hydephp/develop/pull/1883. Use `Hyperlinks::isRemote()` instead.
97+
- **With the new Asset API, a few features had to be moved/removed:**
98+
- `AssetService` class has been removed (was merged into the `Asset` facade) in https://github.com/hydephp/develop/pull/1908
99+
- Removed HydeFront methods from the `Asset` facade (moved to the new HydeFront facade) in https://github.com/hydephp/develop/pull/1907
100+
- The config options `hyde.hydefront_version` and `hyde.hydefront_cdn_url` have been removed in https://github.com/hydephp/develop/pull/1909 (as changing these could lead to incompatible asset versions, defeating the feature's purpose)
101+
- Removed `Hyde::mediaLink()` method replaced by `Hyde::asset()` in https://github.com/hydephp/develop/pull/1932
102+
- Removed `Hyde::mediaPath()` method replaced by `MediaFile::sourcePath()` in https://github.com/hydephp/develop/pull/1911
103+
- Removed `Hyde::siteMediaPath()` method replaced by `MediaFile::outputPath()` in https://github.com/hydephp/develop/pull/1911
75104

76105
### Fixed
77106
- Added missing collection key types in Hyde facade method annotations in https://github.com/hydephp/develop/pull/1784
@@ -240,6 +269,53 @@ The following methods in the `Features` class have been renamed to follow a more
240269

241270
Note that this class was previously marked as internal in v1, but the change is logged here in case it was used in configuration files or custom code.
242271

272+
### Asset API Changes
273+
274+
#### Overview
275+
For most end users, the changes to the Asset API in HydePHP 2.x will have minimal direct impact. However, if you have custom code that interacts with media files, you may need to update it.
276+
277+
The most important thing to note is that all asset retrieval methods now return a `MediaFile` instance, which can be fluently interacted with, or cast to a string to get the link (which was the previous behavior).
278+
279+
#### Side effects to consider
280+
281+
Regardless of if you need to make changes to your code, there are a few side effects to consider:
282+
283+
- All cache busting keys will have changed since we changed the hashing algorithm from `MD5` to `CRC32`.
284+
- Media file getters now return MediaFile instances instead of strings. But these can still be used the same way in Blade `{{ }}` tags, as they can be cast to strings.
285+
- Due to the internal normalizations, we will consistently use cache busting keys and use qualified paths when site URLs are set.
286+
- An exception will be thrown if you try to get a media file that does not exist in order to prevent missing assets from going unnoticed.
287+
288+
These side effects should not have any negative impact on your site, but may cause the generated HTML to look slightly different.
289+
290+
#### Impact on Your Code
291+
292+
If you are using strict type declarations, you may need to update your code to expect a `MediaFile` instance instead of a string path; or you should cast the `MediaFile` instance to a string when needed.
293+
294+
Most changes were made in https://github.com/hydephp/develop/pull/1904 which contains extra information and the reasoning behind the changes.
295+
296+
#### Updating Your Code
297+
298+
Once you have determined that you need to update your code, here are the steps you should take:
299+
300+
1. Update calls to renamed methods:
301+
```php
302+
// Replace this: With this:
303+
Hyde::mediaLink('image.png') => Hyde::asset('image.png');
304+
Asset::mediaLink('image.png') => Asset::get('image.png');
305+
Asset::hasMediaFile('image.png') => Asset::exists('image.png');
306+
Asset::cdnLink('app.css') => HydeFront::cdnLink('app.css');
307+
Asset::injectTailwindConfig() => HydeFront::injectTailwindConfig();
308+
FeaturedImage::isRemote($source) => Hyperlinks::isRemote($source);
309+
```
310+
311+
2. Rename the option `hyde.enable_cache_busting` to `hyde.cache_busting` in your configuration file.
312+
313+
3. Remove any references to `hyde.hydefront_version` and `hyde.hydefront_cdn_url` in your config files as these options have been removed.
314+
315+
4. If you were using `AssetService` directly, refactor your code to use the new `Asset` facade, `MediaFile` class, or `HydeFront` facade as appropriate.
316+
317+
These changes simplify the Asset API and provide more robust handling of media files. The new `MediaFile` class offers additional functionality for working with assets.
318+
243319
## Low impact
244320

245321
### Navigation internal changes

_ide_helper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
class Hyde extends \Hyde\Hyde {}
3434
class Site extends \Hyde\Facades\Site {}
3535
class Meta extends \Hyde\Facades\Meta {}
36-
/** @mixin \Hyde\Framework\Services\AssetService */
3736
class Asset extends \Hyde\Facades\Asset {}
3837
class Author extends \Hyde\Facades\Author {}
3938
class Features extends \Hyde\Facades\Features {}

app/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
'Meta' => \Hyde\Facades\Meta::class,
9595
'Asset' => \Hyde\Facades\Asset::class,
9696
'Author' => \Hyde\Facades\Author::class,
97+
'HydeFront' => \Hyde\Facades\HydeFront::class,
9798
'Features' => \Hyde\Facades\Features::class,
9899
'Config' => \Hyde\Facades\Config::class,
99100
'Filesystem' => \Hyde\Facades\Filesystem::class,
@@ -104,6 +105,7 @@
104105
'MarkdownPage' => \Hyde\Pages\MarkdownPage::class,
105106
'MarkdownPost' => \Hyde\Pages\MarkdownPost::class,
106107
'DocumentationPage' => \Hyde\Pages\DocumentationPage::class,
108+
'MediaFile' => \Hyde\Support\Filesystem\MediaFile::class,
107109
'DataCollection' => \Hyde\Support\DataCollection::class,
108110
'Includes' => \Hyde\Support\Includes::class,
109111
'Feature' => \Hyde\Enums\Feature::class,

config/hyde.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -355,18 +355,16 @@
355355
| Cache Busting
356356
|--------------------------------------------------------------------------
357357
|
358-
| Any assets loaded using the Asset::mediaLink() helper will automatically
359-
| have a cache busting query string appended to the URL. This is useful
358+
| Any assets loaded using the Hyde Asset helpers will automatically have
359+
| a "cache busting" query string appended to the URL. This is useful
360360
| when you want to force browsers to load a new version of an asset.
361+
| All included Blade templates use this feature to load assets.
361362
|
362-
| The mediaLink helper is used in the built-in views to load the
363-
| default stylesheets and scripts, and thus use this feature.
364-
|
365-
| To disable cache busting, set this setting to false.
363+
| To disable the cache busting, set this setting to false.
366364
|
367365
*/
368366

369-
'enable_cache_busting' => true,
367+
'cache_busting' => true,
370368

371369
/*
372370
|--------------------------------------------------------------------------
@@ -470,11 +468,6 @@
470468
// Where should the build manifest be saved? (Relative to project root, for example _site/build-manifest.json)
471469
'build_manifest_path' => 'app/storage/framework/cache/build-manifest.json',
472470

473-
// Here you can specify HydeFront version and URL for when loading app.css from the CDN.
474-
// Only change these if you know what you're doing as some versions may be incompatible with your Hyde version.
475-
'hydefront_version' => \Hyde\Framework\Services\AssetService::HYDEFRONT_VERSION,
476-
'hydefront_cdn_url' => \Hyde\Framework\Services\AssetService::HYDEFRONT_CDN_URL,
477-
478471
// Should the theme toggle buttons be displayed in the layouts?
479472
'theme_toggle_buttons' => true,
480473

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

Lines changed: 7 additions & 15 deletions
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 2023-03-11 11:17:34 in 0.10ms -->
4+
<!-- Generated by HydePHP DocGen script at 2024-08-01 10:01:06 in 0.13ms -->
55

66
#### `filesystem()`
77

@@ -27,14 +27,6 @@ No description provided.
2727
Hyde::vendorPath(string $path, string $package): string
2828
```
2929

30-
#### `mediaPath()`
31-
32-
No description provided.
33-
34-
```php
35-
Hyde::mediaPath(string $path): string
36-
```
37-
3830
#### `sitePath()`
3931

4032
No description provided.
@@ -43,28 +35,28 @@ No description provided.
4335
Hyde::sitePath(string $path): string
4436
```
4537

46-
#### `siteMediaPath()`
38+
#### `pathToAbsolute()`
4739

4840
No description provided.
4941

5042
```php
51-
Hyde::siteMediaPath(string $path): string
43+
Hyde::pathToAbsolute(array|string $path): array|string
5244
```
5345

54-
#### `pathToAbsolute()`
46+
#### `pathToRelative()`
5547

5648
No description provided.
5749

5850
```php
59-
Hyde::pathToAbsolute(array|string $path): array|string
51+
Hyde::pathToRelative(string $path): string
6052
```
6153

62-
#### `pathToRelative()`
54+
#### `assets()`
6355

6456
No description provided.
6557

6658
```php
67-
Hyde::pathToRelative(string $path): string
59+
Hyde::assets(): \Illuminate\Support\Collection<string, \Hyde\Support\Filesystem\MediaFile>
6860
```
6961

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

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<section id="hyde-kernel-hyperlink-methods">
22

33
<!-- Start generated docs for Hyde\Foundation\Concerns\ForwardsHyperlinks -->
4-
<!-- Generated by HydePHP DocGen script at 2024-02-25 19:02:29 in 0.09ms -->
4+
<!-- Generated by HydePHP DocGen script at 2024-09-08 10:25:34 in 0.11ms -->
55

66
#### `formatLink()`
77

@@ -19,22 +19,16 @@ No description provided.
1919
Hyde::relativeLink(string $destination): string
2020
```
2121

22-
#### `mediaLink()`
23-
24-
No description provided.
25-
26-
```php
27-
Hyde::mediaLink(string $destination, bool $validate): string
28-
```
29-
3022
#### `asset()`
3123

3224
No description provided.
3325

3426
```php
35-
Hyde::asset(string $name, bool $preferQualifiedUrl): string
27+
Hyde::asset(string $name): Hyde\Support\Filesystem\MediaFile
3628
```
3729

30+
- **Throws:** \Hyde\Framework\Exceptions\FileNotFoundException If the file does not exist in the `_media` source directory.
31+
3832
#### `url()`
3933

4034
No description provided.

docs/architecture-concepts/the-hydekernel.md

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ Hyde::markdown(string $text, bool $normalizeIndentation): Illuminate\Support\Htm
189189
<section id="hyde-kernel-hyperlink-methods">
190190

191191
<!-- Start generated docs for Hyde\Foundation\Concerns\ForwardsHyperlinks -->
192-
<!-- Generated by HydePHP DocGen script at 2024-02-25 19:02:29 in 0.09ms -->
192+
<!-- Generated by HydePHP DocGen script at 2024-09-08 10:25:34 in 0.11ms -->
193193

194194
#### `formatLink()`
195195

@@ -207,22 +207,16 @@ No description provided.
207207
Hyde::relativeLink(string $destination): string
208208
```
209209

210-
#### `mediaLink()`
211-
212-
No description provided.
213-
214-
```php
215-
Hyde::mediaLink(string $destination, bool $validate): string
216-
```
217-
218210
#### `asset()`
219211

220212
No description provided.
221213

222214
```php
223-
Hyde::asset(string $name, bool $preferQualifiedUrl): string
215+
Hyde::asset(string $name): Hyde\Support\Filesystem\MediaFile
224216
```
225217

218+
- **Throws:** \Hyde\Framework\Exceptions\FileNotFoundException If the file does not exist in the `_media` source directory.
219+
226220
#### `url()`
227221

228222
No description provided.
@@ -254,7 +248,7 @@ Hyde::hasSiteUrl(): bool
254248
<section id="hyde-kernel-filesystem-methods">
255249

256250
<!-- Start generated docs for Hyde\Foundation\Concerns\ForwardsFilesystem -->
257-
<!-- Generated by HydePHP DocGen script at 2023-03-11 11:17:34 in 0.10ms -->
251+
<!-- Generated by HydePHP DocGen script at 2024-08-01 10:01:06 in 0.13ms -->
258252

259253
#### `filesystem()`
260254

@@ -280,14 +274,6 @@ No description provided.
280274
Hyde::vendorPath(string $path, string $package): string
281275
```
282276

283-
#### `mediaPath()`
284-
285-
No description provided.
286-
287-
```php
288-
Hyde::mediaPath(string $path): string
289-
```
290-
291277
#### `sitePath()`
292278

293279
No description provided.
@@ -296,28 +282,28 @@ No description provided.
296282
Hyde::sitePath(string $path): string
297283
```
298284

299-
#### `siteMediaPath()`
285+
#### `pathToAbsolute()`
300286

301287
No description provided.
302288

303289
```php
304-
Hyde::siteMediaPath(string $path): string
290+
Hyde::pathToAbsolute(array|string $path): array|string
305291
```
306292

307-
#### `pathToAbsolute()`
293+
#### `pathToRelative()`
308294

309295
No description provided.
310296

311297
```php
312-
Hyde::pathToAbsolute(array|string $path): array|string
298+
Hyde::pathToRelative(string $path): string
313299
```
314300

315-
#### `pathToRelative()`
301+
#### `assets()`
316302

317303
No description provided.
318304

319305
```php
320-
Hyde::pathToRelative(string $path): string
306+
Hyde::assets(): \Illuminate\Support\Collection<string, \Hyde\Support\Filesystem\MediaFile>
321307
```
322308

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

docs/digging-deeper/customization.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,6 @@ Specifies the path where the build manifest should be saved, relative to the pro
342342
'build_manifest_path' => 'app/storage/framework/cache/build-manifest.json',
343343
```
344344

345-
### `hydefront_version` and `hydefront_cdn_url`
346-
347-
These options allow you to specify the HydeFront version and CDN URL when loading `app.css` from the CDN.
348-
349-
Only change these if you know what you're doing as some versions may be incompatible with your Hyde version.
350-
351-
```php
352-
// filepath config/hyde.php
353-
use \Hyde\Framework\Services\AssetService;
354-
355-
'hydefront_version' => AssetService::HYDEFRONT_VERSION,
356-
'hydefront_cdn_url' => AssetService::HYDEFRONT_CDN_URL,
357-
```
358-
359345
### `theme_toggle_buttons`
360346

361347
>info This feature was added in HydePHP v1.7.0

0 commit comments

Comments
 (0)