Skip to content

Commit

Permalink
Normalize includes paths using same rules as other helpers
Browse files Browse the repository at this point in the history
This means that nested directories are no longer supported, as you should use a data collection for that. See https://github.com/hydephp/develop/pull/1738/files#r1655369910
  • Loading branch information
caendesilva committed Jun 26, 2024
1 parent c77d1fc commit 11a6015
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This serves two purposes:
- Minor: Changed the default build task message to make it more concise in https://github.com/hydephp/develop/pull/1659
- Minor: Data collection files are now validated for syntax errors during discovery in https://github.com/hydephp/develop/pull/1732
- Minor: Methods in the `Includes` facade now return `HtmlString` objects instead of `string` in https://github.com/hydephp/develop/pull/1738. For more information, see below.
- Minor: `Includes::path()` and `Includes::get()` methods now normalizes paths to be basenames to match the behaviour of the other include methods in https://github.com/hydephp/develop/pull/1738. This means that nested directories are no longer supported, as you should use a data collection for that.
- The `hasFeature` method on the Hyde facade and HydeKernel now only accepts a Feature enum value instead of a string for its parameter.
- Changed how the documentation search is generated, to be an `InMemoryPage` instead of a post-build task.
- Media asset files are now copied using the new build task instead of the deprecated `BuildService::transferMediaAssets()` method.
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/Support/Includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function path(?string $filename = null): string
{
return $filename === null
? Hyde::path(static::$includesDirectory)
: Hyde::path(static::$includesDirectory.'/'.$filename);
: Hyde::path(static::$includesDirectory.'/'.static::normalizePath($filename));
}

/**
Expand All @@ -48,7 +48,7 @@ public static function path(?string $filename = null): string
*/
public static function get(string $filename, ?string $default = null): ?string
{
$path = static::path($filename);
$path = static::path(static::normalizePath($filename));

if (! Filesystem::exists($path)) {
return $default;
Expand Down
19 changes: 19 additions & 0 deletions packages/framework/tests/Unit/IncludesFacadeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function testPathReturnsAPartialWithinTheIncludesDirectory()
$this->assertSame(Hyde::path('resources/includes/partial.html'), Includes::path('partial.html'));
}

public function testPathNormalizesNestedIdentifiers()
{
$this->assertSame(Hyde::path('resources/includes/partial.html'), Includes::path('foo/partial.html'));
}

public function testGetReturnsPartial()
{
$filename = 'foo.txt';
Expand All @@ -73,6 +78,20 @@ public function testGetReturnsDefaultValueWhenNotFound()
$this->assertSame($default, Includes::get($filename, $default));
}

public function testGetNormalizesNestedIdentifiers()
{
$filename = 'foo/bar.txt';
$normalized = 'bar.txt';
$expected = 'foo bar';

$this->mockFilesystem(function ($filesystem) use ($expected, $normalized) {
$filesystem->shouldReceive('exists')->with($this->includesPath($normalized))->andReturn(true);
$filesystem->shouldReceive('get')->with($this->includesPath($normalized))->andReturn($expected);
});

$this->assertSame($expected, Includes::get($filename));
}

public function testHtmlReturnsRenderedPartial()
{
$filename = 'foo.html';
Expand Down

0 comments on commit 11a6015

Please sign in to comment.