Skip to content

Commit

Permalink
Merge pull request #9 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
Fixed file search error
  • Loading branch information
andrey-helldar authored Dec 12, 2024
2 parents 5cec732 + befc917 commit af62dbe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/Service/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

class Dumper
{
public function __construct(
protected readonly Files $files
) {}

public function dump(Connection $connection, string $path, array $tables): void
{
foreach ($tables as $table => $params) {
Expand Down Expand Up @@ -44,7 +48,7 @@ protected function deleteFiles(Connection $connection, string $table, string $co
fn (Builder $query) => $query->lazyById(),
fn (Builder $query) => $query->lazyById(column: $column),
)->each(
fn ($item) => Files::delete($path, $item->{$column})
fn ($item) => $this->files->delete($path, $item->{$column})
);
}

Expand Down
32 changes: 19 additions & 13 deletions src/Service/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@

namespace DragonCode\LaravelDataDumper\Service;

use Closure;
use DragonCode\Support\Facades\Filesystem\File;
use DragonCode\Support\Facades\Helpers\Str;

class Files
{
public static function delete(string $path, string $filename): void
public function delete(string $path, string $filename): void
{
if (! $dir = static::directory($path)) {
if (! $dir = $this->directory($path)) {
return;
}

if (! file_exists($dir . '/' . $filename)) {
$filename = static::findFile($dir, $filename);
$filename = $this->findFile($dir, $filename);
}

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

protected static function directory(string $path): false|string
protected function directory(string $path): false|string
{
if (realpath($path) && is_dir($path)) {
return rtrim($path, '\\/');
Expand All @@ -31,15 +32,20 @@ protected static function directory(string $path): false|string
return realpath(base_path($path));
}

protected static function findFile(string $path, string $filename): string
protected function findFile(string $path, string $filename): string
{
return File::names(
$path,
fn (string $name) => Str::contains(
str_replace('\\', '/', $name),
str_replace('\\', '/', $filename)
),
recursive: true
)[0];
return $this->find($path, function (string $name) use ($filename) {
return Str::contains($this->resolvePath($name), $this->resolvePath($filename));
}) ?? $filename;
}

protected function find(string $path, Closure $when): ?string
{
return File::names($path, $when, true)[0] ?? null;
}

protected function resolvePath(string $path): string
{
return str_replace('\\', '/', $path);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Dumps/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
->toBeDataContains('bar')
->toBeDataContains('articles')
->toBeDataContains('qwerty1', fn (int $i) => "qwerty1File$i.stub", fn (int $i) => $i % 2 === 0)
->toBeDataContains('qwerty2', fn (int $i) => "sub/qwerty2File$i.stub")
->toBeDataContains('qwerty2', fn (int $i) => "sub/qwerty2File$i")
->notToBeDataContains('baz');
})->group('SQLite', 'MySQL', 'Postgres');

0 comments on commit af62dbe

Please sign in to comment.