Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ protected function container(): Container

protected function getOptionsDto(): OptionsDto
{
return OptionsDto::fromArray(array_merge($this->options(), $this->arguments()));
return OptionsDto::fromArray(array_merge($this->options(), $this->arguments()))->resolvePath();
}
}
36 changes: 36 additions & 0 deletions src/Helpers/Sorter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelActions\Helpers;

use Closure;
use DragonCode\Support\Facades\Filesystem\Path;
use DragonCode\Support\Facades\Helpers\Arr;

class Sorter
{
public function byValues(array $items): array
{
return Arr::sort($items, $this->callback());
}

public function byKeys(array $items): array
{
return Arr::ksort($items, $this->callback());
}

protected function callback(): Closure
{
return function (string $a, string $b): int {
$current = Path::filename($a);
$next = Path::filename($b);

if ($current === $next) {
return 0;
}

return $current < $next ? -1 : 1;
};
}
}
2 changes: 1 addition & 1 deletion src/Processors/Fresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function migrate(): void
$this->runCommand(Names::MIGRATE, [
'--' . Options::CONNECTION => $this->options->connection,
'--' . Options::PATH => $this->options->path,
'--' . Options::REALPATH => $this->options->realpath,
'--' . Options::REALPATH => true,
]);
}
}
13 changes: 10 additions & 3 deletions src/Processors/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace DragonCode\LaravelActions\Processors;

use DragonCode\Support\Facades\Filesystem\Directory;
use DragonCode\Support\Facades\Filesystem\Path;
use DragonCode\Support\Facades\Helpers\Str;

class Install extends Processor
{
Expand Down Expand Up @@ -34,8 +36,13 @@ protected function create(): void

protected function ensureDirectory(): void
{
Directory::ensureDirectory(
$this->getActionsPath(realpath: $this->options->realpath)
);
$this->isFile($this->options->path)
? Directory::ensureDirectory(Path::dirname($this->options->path))
: Directory::ensureDirectory($this->options->path);
}

protected function isFile(string $path): bool
{
return Str::of($path)->lower()->endsWith('.php');
}
}
23 changes: 15 additions & 8 deletions src/Processors/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Make extends Processor
{
protected string $fallbackName = 'auto';
protected string $fallback = 'auto';

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

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

$this->create($path);
$this->create($path . '/' . $name);
}

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

protected function getName(): string
{
$branch = $this->getBranchName();
$filename = $this->getFilename($branch);
$branch = $this->getBranchName();

return Path::dirname($branch) . DIRECTORY_SEPARATOR . $filename;
return $this->getFilename($branch);
}

protected function getPath(): string
{
return $this->options->path;
}

protected function getFilename(string $branch): string
{
return Str::of(Path::filename($branch))->prepend($this->getTime())->finish('.php')->toString();
$directory = Path::dirname($branch);
$filename = Path::filename($branch);

return Str::of($filename)->prepend($this->getTime())->finish('.php')->prepend($directory . '/')->toString();
}

protected function getBranchName(): string
{
return $this->options->name ?? $this->git->currentBranch() ?? $this->fallbackName;
return $this->options->name ?? $this->git->currentBranch() ?? $this->fallback;
}

protected function getTime(): string
Expand Down
24 changes: 13 additions & 11 deletions src/Processors/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Migrate extends Processor
public function handle(): void
{
$this->ensureRepository();
$this->runActions();
$this->runActions($this->getCompleted());
}

protected function ensureRepository(): void
Expand All @@ -29,10 +29,10 @@ protected function ensureRepository(): void
]);
}

protected function runActions(): void
protected function runActions(array $completed): void
{
try {
if ($files = $this->getNewFiles()) {
if ($files = $this->getNewFiles($completed)) {
$this->fireEvent(ActionStarted::class, 'up');

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

protected function run(string $file, int $batch): void
protected function run(string $filename, int $batch): void
{
$this->migrator->runUp($file, $batch, $this->options);
$this->migrator->runUp($filename, $batch, $this->options);
}

protected function getNewFiles(): array
protected function getNewFiles(array $completed): array
{
$completed = $this->repository->getCompleted()->pluck('action')->toArray();

return $this->getFiles(
filter : fn (string $file) => ! Str::of($file)->replace('\\', '/')->contains($completed),
path : $this->options->path,
fullpath: true
path: $this->options->path,
filter: fn (string $file) => ! Str::of($file)->replace('\\', '/')->contains($completed)
);
}

protected function getCompleted(): array
{
return $this->repository->getCompleted()->pluck('action')->toArray();
}

protected function getBatch(): int
{
return $this->repository->getNextBatchNumber();
Expand Down
38 changes: 10 additions & 28 deletions src/Processors/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
use DragonCode\LaravelActions\Contracts\Notification;
use DragonCode\LaravelActions\Helpers\Config;
use DragonCode\LaravelActions\Helpers\Git;
use DragonCode\LaravelActions\Helpers\Sorter;
use DragonCode\LaravelActions\Repositories\ActionRepository;
use DragonCode\LaravelActions\Services\Migrator;
use DragonCode\LaravelActions\Values\Options;
use DragonCode\Support\Facades\Helpers\Arr;
use DragonCode\Support\Facades\Helpers\Str;
use DragonCode\Support\Filesystem\File;
use DragonCode\Support\Helpers\Ables\Arrayable;
use Illuminate\Console\OutputStyle;
use Illuminate\Contracts\Events\Dispatcher;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -36,42 +35,25 @@ public function __construct(
protected File $file,
protected Migrator $migrator,
protected Notification $notification,
protected Dispatcher $events
protected Dispatcher $events,
protected Sorter $sorter
) {
$this->notification->setOutput($this->output);
$this->repository->setConnection($this->options->connection);
$this->migrator->setConnection($this->options->connection)->setOutput($this->output);
}

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

$names = $this->file->exists($path) ? [$path] : $this->file->allPaths($path, $filter, true);

return Arr::of($names)
->when(
! $fullpath,
fn (Arrayable $array) => $array
->map(fn (string $value) => Str::of(realpath($value))->after(realpath($path))->ltrim('\\/')->toString())
)
->when(
! $withExtension,
fn (Arrayable $array) => $array
->map(fn (string $value) => Str::before($value, '.php'))
)
->toArray();
}

protected function getActionsPath(?string $path = null, bool $realpath = false): string
{
$path = $realpath ? $path : $this->config->path($path);

if (! is_dir($path) && ! Str::endsWith($path, '.php')) {
return $this->file->exists($path . '.php') ? $path . '.php' : $path;
if ($this->file->exists($file) && $this->file->isFile($file)) {
return [$file];
}

return $path;
return $this->sorter->byValues(
$this->file->names($path, $filter, true)
);
}

protected function runCommand(string $command, array $options = []): void
Expand Down
9 changes: 4 additions & 5 deletions src/Processors/Refresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ public function handle(): void
{
$connection = $this->options->connection;
$path = $this->options->path;
$realPath = $this->options->realpath;

$this->runReset($connection, $path, $realPath);
$this->runMigrate($connection, $path, $realPath);
$this->runReset($connection, $path);
$this->runMigrate($connection, $path);
}

protected function runReset(?string $connection, ?string $path, bool $realPath): void
protected function runReset(?string $connection, ?string $path, bool $realPath = true): void
{
$this->runCommand(Names::RESET, [
'--' . Options::CONNECTION => $connection,
Expand All @@ -29,7 +28,7 @@ protected function runReset(?string $connection, ?string $path, bool $realPath):
]);
}

protected function runMigrate(?string $connection, ?string $path, bool $realPath): void
protected function runMigrate(?string $connection, ?string $path, bool $realPath = true): void
{
$this->runCommand(Names::MIGRATE, [
'--' . Options::CONNECTION => $connection,
Expand Down
5 changes: 2 additions & 3 deletions src/Processors/Reset.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ public function handle(): void
$this->rollback(
$this->options->connection,
$this->options->path,
$this->options->realpath,
$this->count()
);
}

protected function rollback(?string $connection, ?string $path, ?bool $realPath, int $step): void
protected function rollback(?string $connection, ?string $path, int $step): void
{
$this->runCommand(Names::ROLLBACK, [
'--' . Options::CONNECTION => $connection,
'--' . Options::PATH => $path,
'--' . Options::REALPATH => $realPath,
'--' . Options::REALPATH => true,
'--' . Options::STEP => $step,
'--' . Options::FORCE => true,
]);
Expand Down
14 changes: 2 additions & 12 deletions src/Processors/Rollback.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use DragonCode\LaravelActions\Events\ActionEnded;
use DragonCode\LaravelActions\Events\ActionStarted;
use DragonCode\LaravelActions\Events\NoPendingActions;
use DragonCode\Support\Facades\Helpers\Str;

class Rollback extends Processor
{
Expand Down Expand Up @@ -35,9 +34,7 @@ public function handle(): void
protected function run(array $actions): void
{
foreach ($actions as $row) {
$this->rollbackAction(
$this->resolveFilename($row->action)
);
$this->rollbackAction($row->action);
}
}

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

protected function rollbackAction(string $action): void
{
$this->migrator->runDown(
$this->getActionsPath($action, realpath: $this->options->realpath)
);
$this->migrator->runDown($action, $this->options);
}

protected function nothingToRollback(): bool
Expand All @@ -70,9 +65,4 @@ protected function count(): int
{
return $this->repository->getLastBatchNumber();
}

protected function resolveFilename(string $name): string
{
return Str::finish($name, '.php');
}
}
2 changes: 1 addition & 1 deletion src/Processors/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function showStatus(array $actions, array $completed): void

protected function getData(): array
{
$files = $this->getFiles(withExtension: false);
$files = $this->getFiles($this->options->path);
$completed = $this->getCompleted();

return [$files, $completed];
Expand Down
Loading