Skip to content

Commit e795621

Browse files
Refactored path
1 parent 16addca commit e795621

File tree

14 files changed

+129
-114
lines changed

14 files changed

+129
-114
lines changed

src/Console/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ protected function container(): Container
5151

5252
protected function getOptionsDto(): OptionsDto
5353
{
54-
return OptionsDto::fromArray(array_merge($this->options(), $this->arguments()));
54+
return OptionsDto::fromArray(array_merge($this->options(), $this->arguments()))->resolvePath();
5555
}
5656
}

src/Processors/Fresh.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function migrate(): void
3636
$this->runCommand(Names::MIGRATE, [
3737
'--' . Options::CONNECTION => $this->options->connection,
3838
'--' . Options::PATH => $this->options->path,
39-
'--' . Options::REALPATH => $this->options->realpath,
39+
'--' . Options::REALPATH => true,
4040
]);
4141
}
4242
}

src/Processors/Install.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace DragonCode\LaravelActions\Processors;
66

77
use DragonCode\Support\Facades\Filesystem\Directory;
8+
use DragonCode\Support\Facades\Filesystem\Path;
9+
use DragonCode\Support\Facades\Helpers\Str;
810

911
class Install extends Processor
1012
{
@@ -34,8 +36,13 @@ protected function create(): void
3436

3537
protected function ensureDirectory(): void
3638
{
37-
Directory::ensureDirectory(
38-
$this->getActionsPath(realpath: $this->options->realpath)
39-
);
39+
$this->isFile($this->options->path)
40+
? Directory::ensureDirectory(Path::dirname($this->options->path))
41+
: Directory::ensureDirectory($this->options->path);
42+
}
43+
44+
protected function isFile(string $path): bool
45+
{
46+
return Str::of($path)->lower()->endsWith('.php');
4047
}
4148
}

src/Processors/Make.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class Make extends Processor
1212
{
13-
protected string $fallbackName = 'auto';
13+
protected string $fallback = 'auto';
1414

1515
protected string $stub = __DIR__ . '/../../resources/stubs/action.stub';
1616

@@ -22,9 +22,9 @@ public function handle(): void
2222
protected function run(): void
2323
{
2424
$name = $this->getName();
25-
$path = $this->getActionsPath($name, realpath: $this->options->realpath);
25+
$path = $this->getPath();
2626

27-
$this->create($path);
27+
$this->create($path . '/' . $name);
2828
}
2929

3030
protected function create(string $path): void
@@ -34,20 +34,27 @@ protected function create(string $path): void
3434

3535
protected function getName(): string
3636
{
37-
$branch = $this->getBranchName();
38-
$filename = $this->getFilename($branch);
37+
$branch = $this->getBranchName();
3938

40-
return Path::dirname($branch) . DIRECTORY_SEPARATOR . $filename;
39+
return $this->getFilename($branch);
40+
}
41+
42+
protected function getPath(): string
43+
{
44+
return $this->options->path;
4145
}
4246

4347
protected function getFilename(string $branch): string
4448
{
45-
return Str::of(Path::filename($branch))->prepend($this->getTime())->finish('.php')->toString();
49+
$directory = Path::dirname($branch);
50+
$filename = Path::filename($branch);
51+
52+
return Str::of($filename)->prepend($this->getTime())->finish('.php')->prepend($directory . '/')->toString();
4653
}
4754

4855
protected function getBranchName(): string
4956
{
50-
return $this->options->name ?? $this->git->currentBranch() ?? $this->fallbackName;
57+
return $this->options->name ?? $this->git->currentBranch() ?? $this->fallback;
5158
}
5259

5360
protected function getTime(): string

src/Processors/Migrate.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Migrate extends Processor
1818
public function handle(): void
1919
{
2020
$this->ensureRepository();
21-
$this->runActions();
21+
$this->runActions($this->getCompleted());
2222
}
2323

2424
protected function ensureRepository(): void
@@ -29,10 +29,10 @@ protected function ensureRepository(): void
2929
]);
3030
}
3131

32-
protected function runActions(): void
32+
protected function runActions(array $completed): void
3333
{
3434
try {
35-
if ($files = $this->getNewFiles()) {
35+
if ($files = $this->getNewFiles($completed)) {
3636
$this->fireEvent(ActionStarted::class, 'up');
3737

3838
$this->runEach($files, $this->getBatch());
@@ -58,22 +58,24 @@ protected function runEach(array $files, int $batch): void
5858
}
5959
}
6060

61-
protected function run(string $file, int $batch): void
61+
protected function run(string $filename, int $batch): void
6262
{
63-
$this->migrator->runUp($file, $batch, $this->options);
63+
$this->migrator->runUp($filename, $batch, $this->options);
6464
}
6565

66-
protected function getNewFiles(): array
66+
protected function getNewFiles(array $completed): array
6767
{
68-
$completed = $this->repository->getCompleted()->pluck('action')->toArray();
69-
7068
return $this->getFiles(
71-
filter : fn (string $file) => ! Str::of($file)->replace('\\', '/')->contains($completed),
72-
path : $this->options->path,
73-
fullpath: true
69+
path: $this->options->path,
70+
filter: fn (string $file) => ! Str::of($file)->replace('\\', '/')->contains($completed)
7471
);
7572
}
7673

74+
protected function getCompleted(): array
75+
{
76+
return $this->repository->getCompleted()->pluck('action')->toArray();
77+
}
78+
7779
protected function getBatch(): int
7880
{
7981
return $this->repository->getNextBatchNumber();

src/Processors/Processor.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
use DragonCode\LaravelActions\Repositories\ActionRepository;
1313
use DragonCode\LaravelActions\Services\Migrator;
1414
use DragonCode\LaravelActions\Values\Options;
15-
use DragonCode\Support\Facades\Helpers\Arr;
1615
use DragonCode\Support\Facades\Helpers\Str;
1716
use DragonCode\Support\Filesystem\File;
18-
use DragonCode\Support\Helpers\Ables\Arrayable;
1917
use Illuminate\Console\OutputStyle;
2018
use Illuminate\Contracts\Events\Dispatcher;
2119
use Symfony\Component\Console\Input\InputInterface;
@@ -43,35 +41,15 @@ public function __construct(
4341
$this->migrator->setConnection($this->options->connection)->setOutput($this->output);
4442
}
4543

46-
protected function getFiles(?Closure $filter = null, ?string $path = null, bool $realpath = false, bool $fullpath = false, bool $withExtension = true): array
44+
protected function getFiles(string $path, ?Closure $filter = null): array
4745
{
48-
$path = $this->getActionsPath($path, $realpath);
46+
$file = Str::finish($path, '.php');
4947

50-
$names = $this->file->exists($path) ? [$path] : $this->file->allPaths($path, $filter, true);
51-
52-
return Arr::of($names)
53-
->when(
54-
! $fullpath,
55-
fn (Arrayable $array) => $array
56-
->map(fn (string $value) => Str::of(realpath($value))->after(realpath($path))->ltrim('\\/')->toString())
57-
)
58-
->when(
59-
! $withExtension,
60-
fn (Arrayable $array) => $array
61-
->map(fn (string $value) => Str::before($value, '.php'))
62-
)
63-
->toArray();
64-
}
65-
66-
protected function getActionsPath(?string $path = null, bool $realpath = false): string
67-
{
68-
$path = $realpath ? $path : $this->config->path($path);
69-
70-
if (! is_dir($path) && ! Str::endsWith($path, '.php')) {
71-
return $this->file->exists($path . '.php') ? $path . '.php' : $path;
48+
if ($this->file->exists($file) && $this->file->isFile($file)) {
49+
return [$file];
7250
}
7351

74-
return $path;
52+
return $this->file->names($path, $filter, true);
7553
}
7654

7755
protected function runCommand(string $command, array $options = []): void

src/Processors/Refresh.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ public function handle(): void
1313
{
1414
$connection = $this->options->connection;
1515
$path = $this->options->path;
16-
$realPath = $this->options->realpath;
1716

18-
$this->runReset($connection, $path, $realPath);
19-
$this->runMigrate($connection, $path, $realPath);
17+
$this->runReset($connection, $path);
18+
$this->runMigrate($connection, $path);
2019
}
2120

22-
protected function runReset(?string $connection, ?string $path, bool $realPath): void
21+
protected function runReset(?string $connection, ?string $path, bool $realPath = true): void
2322
{
2423
$this->runCommand(Names::RESET, [
2524
'--' . Options::CONNECTION => $connection,
@@ -29,7 +28,7 @@ protected function runReset(?string $connection, ?string $path, bool $realPath):
2928
]);
3029
}
3130

32-
protected function runMigrate(?string $connection, ?string $path, bool $realPath): void
31+
protected function runMigrate(?string $connection, ?string $path, bool $realPath = true): void
3332
{
3433
$this->runCommand(Names::MIGRATE, [
3534
'--' . Options::CONNECTION => $connection,

src/Processors/Reset.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@ public function handle(): void
1414
$this->rollback(
1515
$this->options->connection,
1616
$this->options->path,
17-
$this->options->realpath,
1817
$this->count()
1918
);
2019
}
2120

22-
protected function rollback(?string $connection, ?string $path, ?bool $realPath, int $step): void
21+
protected function rollback(?string $connection, ?string $path, int $step): void
2322
{
2423
$this->runCommand(Names::ROLLBACK, [
2524
'--' . Options::CONNECTION => $connection,
2625
'--' . Options::PATH => $path,
27-
'--' . Options::REALPATH => $realPath,
26+
'--' . Options::REALPATH => true,
2827
'--' . Options::STEP => $step,
2928
'--' . Options::FORCE => true,
3029
]);

src/Processors/Rollback.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use DragonCode\LaravelActions\Events\ActionEnded;
88
use DragonCode\LaravelActions\Events\ActionStarted;
99
use DragonCode\LaravelActions\Events\NoPendingActions;
10-
use DragonCode\Support\Facades\Helpers\Str;
1110

1211
class Rollback extends Processor
1312
{
@@ -35,9 +34,7 @@ public function handle(): void
3534
protected function run(array $actions): void
3635
{
3736
foreach ($actions as $row) {
38-
$this->rollbackAction(
39-
$this->resolveFilename($row->action)
40-
);
37+
$this->rollbackAction($row->action);
4138
}
4239
}
4340

@@ -50,9 +47,7 @@ protected function getActions(?int $step): array
5047

5148
protected function rollbackAction(string $action): void
5249
{
53-
$this->migrator->runDown(
54-
$this->getActionsPath($action, realpath: $this->options->realpath)
55-
);
50+
$this->migrator->runDown($action, $this->options);
5651
}
5752

5853
protected function nothingToRollback(): bool
@@ -70,9 +65,4 @@ protected function count(): int
7065
{
7166
return $this->repository->getLastBatchNumber();
7267
}
73-
74-
protected function resolveFilename(string $name): string
75-
{
76-
return Str::finish($name, '.php');
77-
}
7868
}

src/Processors/Status.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected function showStatus(array $actions, array $completed): void
5050

5151
protected function getData(): array
5252
{
53-
$files = $this->getFiles(withExtension: false);
53+
$files = $this->getFiles($this->options->path);
5454
$completed = $this->getCompleted();
5555

5656
return [$files, $completed];

0 commit comments

Comments
 (0)