Skip to content

Commit 3ff7277

Browse files
authored
Merge pull request #2293 from hydephp/feat/ensure-parent-directory-exists-method
Add a `Filesystem::ensureParentDirectoryExists` method
2 parents 7ad0e39 + 870ec04 commit 3ff7277

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ This serves two purposes:
6666
- You can now add custom posts to the blog post feed component when including it directly in [#1893](https://github.com/hydephp/develop/pull/1893)
6767
- You can now specify sidebar item priorities by adding a numeric prefix to documentation page source file names in [#1709](https://github.com/hydephp/develop/pull/1709)
6868
- You can now forward method calls to the underlying `DateTime` instance in `DateString` instances in [#2235](https://github.com/hydephp/develop/pull/2235)
69+
- Added `Filesystem::ensureParentDirectoryExists()` method to reduce repetitive code when ensuring parent directories exist in [#2293](https://github.com/hydephp/develop/pull/2293)
6970

7071
### Changed
7172

packages/framework/src/Console/Helpers/InteractivePublishCommandHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getBaseDirectory(): string
6060
public function publishFiles(): void
6161
{
6262
foreach ($this->publishableFilesMap as $source => $target) {
63-
Filesystem::ensureDirectoryExists(dirname($target));
63+
Filesystem::ensureParentDirectoryExists($target);
6464
Filesystem::copy($source, $target);
6565
}
6666
}

packages/framework/src/Facades/Filesystem.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ public static function unlinkIfExists(string $path): bool
117117
return self::kernel()->filesystem()->unlinkIfExists($path);
118118
}
119119

120+
/**
121+
* Ensure that the parent directory of the given file path exists.
122+
*
123+
* @param string $path
124+
* @param int $mode
125+
* @param bool $recursive
126+
* @return void
127+
*/
128+
public static function ensureParentDirectoryExists(string $path, int $mode = 0755, bool $recursive = true): void
129+
{
130+
self::ensureDirectoryExists(self::dirname($path), $mode, $recursive);
131+
}
132+
120133
/**
121134
* Get the contents of a file.
122135
*

packages/framework/src/Pages/Concerns/BaseMarkdownPage.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Hyde\Markdown\Models\Markdown;
1111
use Illuminate\Support\Facades\View;
1212

13-
use function dirname;
1413
use function ltrim;
1514
use function trim;
1615

@@ -65,7 +64,7 @@ public function compile(): string
6564
*/
6665
public function save(): static
6766
{
68-
Filesystem::ensureDirectoryExists(dirname($this->getSourcePath()));
67+
Filesystem::ensureParentDirectoryExists($this->getSourcePath());
6968

7069
Filesystem::putContents($this->getSourcePath(), ltrim(trim("$this->matter\n$this->markdown")."\n"));
7170

packages/framework/tests/Unit/FilesystemFacadeUnitTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,23 @@ public function testEnsureDirectoryExists()
333333
Filesystem::ensureDirectoryExists('path');
334334
}
335335

336+
public function testEnsureParentDirectoryExists()
337+
{
338+
$mock = $this->mockFilesystem();
339+
340+
$mock->shouldReceive('dirname')
341+
->with(Hyde::path('file.txt'))
342+
->once()
343+
->andReturn(Hyde::path('parent'));
344+
345+
$mock->shouldReceive('ensureDirectoryExists')
346+
->with(Hyde::path('parent'), 0755, true)
347+
->once()
348+
->andReturnNull();
349+
350+
Filesystem::ensureParentDirectoryExists('file.txt');
351+
}
352+
336353
public function testMakeDirectory()
337354
{
338355
$this->createExpectation('makeDirectory', true, Hyde::path('path'));

packages/framework/tests/Unit/InteractivePublishCommandHelperTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function testOnlyFiltersPublishableFiles(): void
6767

6868
public function testPublishFiles(): void
6969
{
70+
$this->filesystem->shouldReceive('dirname')->times(3)->andReturn(Hyde::path('resources/views/vendor/hyde/layouts'));
7071
$this->filesystem->shouldReceive('ensureDirectoryExists')->times(3);
7172
$this->filesystem->shouldReceive('copy')->times(3);
7273

@@ -78,7 +79,8 @@ public function testPublishFiles(): void
7879

7980
$helper->publishFiles();
8081

81-
$this->filesystem->shouldHaveReceived('ensureDirectoryExists')->with(Hyde::path('resources/views/vendor/hyde/layouts'))->times(3);
82+
$this->filesystem->shouldHaveReceived('dirname')->times(3);
83+
$this->filesystem->shouldHaveReceived('ensureDirectoryExists')->with(Hyde::path('resources/views/vendor/hyde/layouts'), 0755, true)->times(3);
8284

8385
$this->filesystem->shouldHaveReceived('copy')->with(
8486
Hyde::path('packages/framework/resources/views/layouts/app.blade.php'),

0 commit comments

Comments
 (0)