Skip to content

Commit 2aaa836

Browse files
authored
Merge pull request #753 from hydephp/add-unlinkIfExists-helper
Add Filesystem::unlinkIfExists helper
2 parents ce5bc3f + b504b71 commit 2aaa836

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

packages/framework/src/Facades/Filesystem.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ public static function unlink(string|array $path): bool
9292
return self::kernel()->filesystem()->unlink($path);
9393
}
9494

95+
/**
96+
* Unlink a file in the project's directory, but only if it exists.
97+
*
98+
* @param string $path
99+
* @return bool True if the file was unlinked, false if it did not exist or failed to unlink.
100+
*/
101+
public static function unlinkIfExists(string $path): bool
102+
{
103+
return self::kernel()->filesystem()->unlinkIfExists($path);
104+
}
105+
95106
/**
96107
* Get the contents of a file.
97108
*

packages/framework/src/Foundation/Filesystem.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
/**
2424
* File helper methods, bound to the HydeKernel instance, and is an integral part of the framework.
2525
*
26-
* All paths are relative to the root of the application.
26+
* All paths arguments are relative to the root of the application,
27+
* and will be automatically resolved to absolute paths.
2728
*
2829
* @see \Hyde\Framework\Testing\Feature\Foundation\FilesystemTest
2930
*/
@@ -146,6 +147,18 @@ public function unlink(string|array $path): bool
146147
return true;
147148
}
148149

150+
/**
151+
* Unlink a file in the project's directory, but only if it exists.
152+
*/
153+
public function unlinkIfExists(string $path): bool
154+
{
155+
if (file_exists($this->path($path))) {
156+
return unlink($this->path($path));
157+
}
158+
159+
return false;
160+
}
161+
149162
/**
150163
* Fluent file helper methods.
151164
*

packages/framework/tests/Feature/FilesystemFacadeTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public function testUnlink()
6666
$this->assertFileDoesNotExist(Hyde::path('foo'));
6767
}
6868

69+
public function testUnlinkIfExists()
70+
{
71+
touch(Hyde::path('foo'));
72+
Filesystem::unlinkIfExists('foo');
73+
74+
$this->assertFileDoesNotExist(Hyde::path('foo'));
75+
}
76+
6977
public function testGetContents()
7078
{
7179
$this->createExpectation('get', 'string', Hyde::path('path'), false);

packages/framework/tests/Feature/Foundation/FilesystemTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ public function test_unlink_helper_deletes_multiple_files_at_given_paths()
152152
$this->assertFileDoesNotExist(Hyde::path('bar'));
153153
}
154154

155+
public function test_unlinkIfExists_helper_deletes_file_at_given_path()
156+
{
157+
touch(Hyde::path('foo'));
158+
$this->assertTrue($this->filesystem->unlinkIfExists('foo'));
159+
$this->assertFileDoesNotExist(Hyde::path('foo'));
160+
}
161+
162+
public function test_unlinkIfExists_handles_non_existent_files_gracefully()
163+
{
164+
$this->assertFalse($this->filesystem->unlinkIfExists('foo'));
165+
}
166+
155167
public function test_get_model_source_path_method_returns_path_for_model_classes()
156168
{
157169
$this->assertEquals(

packages/testing/src/TestCase.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Hyde\Facades\Features;
66
use Hyde\Facades\Filesystem;
77
use Hyde\Framework\Actions\ConvertsArrayToFrontMatter;
8-
use Hyde\Hyde;
98
use Hyde\Pages\Concerns\HydePage;
109
use Hyde\Pages\MarkdownPage;
1110
use Hyde\Support\Facades\Render;
@@ -96,9 +95,9 @@ protected function mockCurrentPage(string $currentPage)
9695
protected function file(string $path, ?string $contents = null): void
9796
{
9897
if ($contents) {
99-
file_put_contents(Hyde::path($path), $contents);
98+
Filesystem::put($path, $contents);
10099
} else {
101-
Hyde::touch($path);
100+
Filesystem::touch($path);
102101
}
103102

104103
$this->cleanUpWhenDone($path);
@@ -128,15 +127,16 @@ protected function cleanUpFilesystem(): void
128127
if (sizeof($this->fileMemory) > 0) {
129128
foreach ($this->fileMemory as $file) {
130129
if (Filesystem::isDirectory($file)) {
131-
$dontDelete = ['_site', '_media', '_pages', '_posts', '_docs', 'app', 'config', 'storage', 'vendor', 'node_modules'];
130+
$keep = ['_site', '_media', '_pages', '_posts', '_docs', 'app', 'config', 'storage', 'vendor', 'node_modules'];
132131

133-
if (! in_array($file, $dontDelete)) {
132+
if (! in_array($file, $keep)) {
134133
Filesystem::deleteDirectory($file);
135134
}
136135
} else {
137-
Filesystem::unlink($file);
136+
Filesystem::unlinkIfExists($file);
138137
}
139138
}
139+
140140
$this->fileMemory = [];
141141
}
142142
}

0 commit comments

Comments
 (0)