Skip to content

Commit af62dbe

Browse files
Merge pull request #9 from TheDragonCode/1.x
Fixed file search error
2 parents 5cec732 + befc917 commit af62dbe

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/Service/Dumper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
class Dumper
1919
{
20+
public function __construct(
21+
protected readonly Files $files
22+
) {}
23+
2024
public function dump(Connection $connection, string $path, array $tables): void
2125
{
2226
foreach ($tables as $table => $params) {
@@ -44,7 +48,7 @@ protected function deleteFiles(Connection $connection, string $table, string $co
4448
fn (Builder $query) => $query->lazyById(),
4549
fn (Builder $query) => $query->lazyById(column: $column),
4650
)->each(
47-
fn ($item) => Files::delete($path, $item->{$column})
51+
fn ($item) => $this->files->delete($path, $item->{$column})
4852
);
4953
}
5054

src/Service/Files.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@
44

55
namespace DragonCode\LaravelDataDumper\Service;
66

7+
use Closure;
78
use DragonCode\Support\Facades\Filesystem\File;
89
use DragonCode\Support\Facades\Helpers\Str;
910

1011
class Files
1112
{
12-
public static function delete(string $path, string $filename): void
13+
public function delete(string $path, string $filename): void
1314
{
14-
if (! $dir = static::directory($path)) {
15+
if (! $dir = $this->directory($path)) {
1516
return;
1617
}
1718

1819
if (! file_exists($dir . '/' . $filename)) {
19-
$filename = static::findFile($dir, $filename);
20+
$filename = $this->findFile($dir, $filename);
2021
}
2122

2223
File::ensureDelete($dir . '/' . $filename);
2324
}
2425

25-
protected static function directory(string $path): false|string
26+
protected function directory(string $path): false|string
2627
{
2728
if (realpath($path) && is_dir($path)) {
2829
return rtrim($path, '\\/');
@@ -31,15 +32,20 @@ protected static function directory(string $path): false|string
3132
return realpath(base_path($path));
3233
}
3334

34-
protected static function findFile(string $path, string $filename): string
35+
protected function findFile(string $path, string $filename): string
3536
{
36-
return File::names(
37-
$path,
38-
fn (string $name) => Str::contains(
39-
str_replace('\\', '/', $name),
40-
str_replace('\\', '/', $filename)
41-
),
42-
recursive: true
43-
)[0];
37+
return $this->find($path, function (string $name) use ($filename) {
38+
return Str::contains($this->resolvePath($name), $this->resolvePath($filename));
39+
}) ?? $filename;
40+
}
41+
42+
protected function find(string $path, Closure $when): ?string
43+
{
44+
return File::names($path, $when, true)[0] ?? null;
45+
}
46+
47+
protected function resolvePath(string $path): string
48+
{
49+
return str_replace('\\', '/', $path);
4450
}
4551
}

tests/Unit/Dumps/DataTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
->toBeDataContains('bar')
1919
->toBeDataContains('articles')
2020
->toBeDataContains('qwerty1', fn (int $i) => "qwerty1File$i.stub", fn (int $i) => $i % 2 === 0)
21-
->toBeDataContains('qwerty2', fn (int $i) => "sub/qwerty2File$i.stub")
21+
->toBeDataContains('qwerty2', fn (int $i) => "sub/qwerty2File$i")
2222
->notToBeDataContains('baz');
2323
})->group('SQLite', 'MySQL', 'Postgres');

0 commit comments

Comments
 (0)