Skip to content

Commit ebe3b1c

Browse files
Merge pull request #76 from TheDragonCode/3.x
Fixed file sorting
2 parents 899f6e4 + a5c42c7 commit ebe3b1c

File tree

17 files changed

+261
-110
lines changed

17 files changed

+261
-110
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/Helpers/Sorter.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelActions\Helpers;
6+
7+
use Closure;
8+
use DragonCode\Support\Facades\Filesystem\Path;
9+
use DragonCode\Support\Facades\Helpers\Arr;
10+
11+
class Sorter
12+
{
13+
public function byValues(array $items): array
14+
{
15+
return Arr::sort($items, $this->callback());
16+
}
17+
18+
public function byKeys(array $items): array
19+
{
20+
return Arr::ksort($items, $this->callback());
21+
}
22+
23+
protected function callback(): Closure
24+
{
25+
return function (string $a, string $b): int {
26+
$current = Path::filename($a);
27+
$next = Path::filename($b);
28+
29+
if ($current === $next) {
30+
return 0;
31+
}
32+
33+
return $current < $next ? -1 : 1;
34+
};
35+
}
36+
}

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: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
use DragonCode\LaravelActions\Contracts\Notification;
1010
use DragonCode\LaravelActions\Helpers\Config;
1111
use DragonCode\LaravelActions\Helpers\Git;
12+
use DragonCode\LaravelActions\Helpers\Sorter;
1213
use DragonCode\LaravelActions\Repositories\ActionRepository;
1314
use DragonCode\LaravelActions\Services\Migrator;
1415
use DragonCode\LaravelActions\Values\Options;
15-
use DragonCode\Support\Facades\Helpers\Arr;
1616
use DragonCode\Support\Facades\Helpers\Str;
1717
use DragonCode\Support\Filesystem\File;
18-
use DragonCode\Support\Helpers\Ables\Arrayable;
1918
use Illuminate\Console\OutputStyle;
2019
use Illuminate\Contracts\Events\Dispatcher;
2120
use Symfony\Component\Console\Input\InputInterface;
@@ -36,42 +35,25 @@ public function __construct(
3635
protected File $file,
3736
protected Migrator $migrator,
3837
protected Notification $notification,
39-
protected Dispatcher $events
38+
protected Dispatcher $events,
39+
protected Sorter $sorter
4040
) {
4141
$this->notification->setOutput($this->output);
4242
$this->repository->setConnection($this->options->connection);
4343
$this->migrator->setConnection($this->options->connection)->setOutput($this->output);
4444
}
4545

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

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;
50+
if ($this->file->exists($file) && $this->file->isFile($file)) {
51+
return [$file];
7252
}
7353

74-
return $path;
54+
return $this->sorter->byValues(
55+
$this->file->names($path, $filter, true)
56+
);
7557
}
7658

7759
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
}

0 commit comments

Comments
 (0)