diff --git a/config/deploy-operations.php b/config/deploy-operations.php index 23078e3b..3a3cc0ce 100644 --- a/config/deploy-operations.php +++ b/config/deploy-operations.php @@ -39,11 +39,12 @@ */ 'transactions' => [ - // | Determines whether the use of database transactions is enabled. + // Determines whether the use of database transactions is enabled. 'enabled' => false, - // | The number of attempts to execute a request within a transaction before throwing an error. + // The number of attempts to execute a request within a transaction before throwing an error. + 'attempts' => 1, ], @@ -146,6 +147,6 @@ | */ - 'full_path' => env('DEPLOY_OPERATIONS_SHOW_FULL_PATH', false), + 'full_path' => (bool) env('DEPLOY_OPERATIONS_SHOW_FULL_PATH', false), ], ]; diff --git a/database/migrations/2022_08_18_180137_change_migration_actions_table.php b/database/migrations/2022_08_18_180137_change_migration_actions_table.php index c598de0f..28e8c882 100644 --- a/database/migrations/2022_08_18_180137_change_migration_actions_table.php +++ b/database/migrations/2022_08_18_180137_change_migration_actions_table.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use DragonCode\LaravelDeployOperations\Helpers\ConfigHelper; +use DragonCode\LaravelDeployOperations\Data\Config\ConfigData; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; @@ -49,6 +49,6 @@ protected function doesntHaveColumn(string $column): bool protected function table(): string { - return app(ConfigHelper::class)->table(); + return app(ConfigData::class)->table; } }; diff --git a/database/migrations/2023_01_21_172923_rename_migrations_actions_table_to_actions.php b/database/migrations/2023_01_21_172923_rename_migrations_actions_table_to_actions.php index 233533c4..6db8df15 100644 --- a/database/migrations/2023_01_21_172923_rename_migrations_actions_table_to_actions.php +++ b/database/migrations/2023_01_21_172923_rename_migrations_actions_table_to_actions.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use DragonCode\LaravelDeployOperations\Helpers\ConfigHelper; +use DragonCode\LaravelDeployOperations\Data\Config\ConfigData; use Illuminate\Database\Migrations\Migration; use Illuminate\Support\Facades\Schema; @@ -39,6 +39,6 @@ protected function doesntSame(string $first, string $second): bool protected function table(): string { - return app(ConfigHelper::class)->table(); + return app(ConfigData::class)->table; } }; diff --git a/database/migrations/2024_05_21_112438_rename_actions_table_to_operations.php b/database/migrations/2024_05_21_112438_rename_actions_table_to_operations.php index 2959b679..48d6fa04 100644 --- a/database/migrations/2024_05_21_112438_rename_actions_table_to_operations.php +++ b/database/migrations/2024_05_21_112438_rename_actions_table_to_operations.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use DragonCode\LaravelDeployOperations\Helpers\ConfigHelper; +use DragonCode\LaravelDeployOperations\Data\Config\ConfigData; use Illuminate\Database\Migrations\Migration; use Illuminate\Support\Facades\Schema; @@ -39,6 +39,6 @@ protected function doesntSame(string $first, string $second): bool protected function table(): string { - return app(ConfigHelper::class)->table(); + return app(ConfigData::class)->table; } }; diff --git a/database/migrations/2024_05_21_114318_rename_column_in_operations_table.php b/database/migrations/2024_05_21_114318_rename_column_in_operations_table.php index 7ad24c46..6636c5c2 100644 --- a/database/migrations/2024_05_21_114318_rename_column_in_operations_table.php +++ b/database/migrations/2024_05_21_114318_rename_column_in_operations_table.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use DragonCode\LaravelDeployOperations\Helpers\ConfigHelper; +use DragonCode\LaravelDeployOperations\Data\Config\ConfigData; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; @@ -27,6 +27,6 @@ protected function rename(string $from, string $to): void protected function table(): string { - return app(ConfigHelper::class)->table(); + return app(ConfigData::class)->table; } }; diff --git a/src/Data/Casts/Config/ExcludeCast.php b/src/Data/Casts/Config/ExcludeCast.php new file mode 100644 index 00000000..8b10d669 --- /dev/null +++ b/src/Data/Casts/Config/ExcludeCast.php @@ -0,0 +1,22 @@ +map(static fn (string $path) => Str::replace(['\\', '/'], DIRECTORY_SEPARATOR, $path)) + ->filter() + ->all(); + } +} diff --git a/src/Data/Casts/Config/PathCast.php b/src/Data/Casts/Config/PathCast.php new file mode 100644 index 00000000..e9acdc1c --- /dev/null +++ b/src/Data/Casts/Config/PathCast.php @@ -0,0 +1,19 @@ +config()->basePath((string) $value); + $path = $this->config()->path . $value; if ($properties['realpath'] ?? false) { return $value ?: $path; @@ -31,8 +31,8 @@ protected function filename(string $path): false|string return realpath(Str::finish($path, '.php')); } - protected function config(): ConfigHelper + protected function config(): ConfigData { - return app(ConfigHelper::class); + return app(ConfigData::class); } } diff --git a/src/Data/Config/ConfigData.php b/src/Data/Config/ConfigData.php new file mode 100644 index 00000000..1cc18d05 --- /dev/null +++ b/src/Data/Config/ConfigData.php @@ -0,0 +1,31 @@ +config->get('app.env', 'production'); - } - - public function connection(): ?string - { - return $this->config->get('deploy-operations.connection'); - } - - public function transactionAttempts(): int - { - return $this->config->get('deploy-operations.transactions.attempts', 1); - } - - public function table(): string - { - return $this->config->get('deploy-operations.table'); - } - - public function exclude(): array - { - return Arr::of((array) $this->config->get('deploy-operations.exclude')) - ->map(fn (string $path) => str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path)) - ->filter() - ->toArray(); - } - - public function basePath(string $path = ''): string - { - return rtrim($this->directory(), '\\/') . DIRECTORY_SEPARATOR . ltrim($path, '\\/'); - } - - public function gitPath(): string - { - return base_path(); - } - - public function showFullPath(): bool - { - return (bool) $this->config->get('deploy-operations.show.full_path'); - } - - protected function directory(): string - { - return $this->config->get('deploy-operations.path', base_path('operations')); - } -} diff --git a/src/Helpers/GitHelper.php b/src/Helpers/GitHelper.php index ced0bd29..6feb9bc0 100644 --- a/src/Helpers/GitHelper.php +++ b/src/Helpers/GitHelper.php @@ -6,6 +6,7 @@ use DragonCode\Support\Facades\Filesystem\Directory; +use function base_path; use function exec; use function realpath; use function rtrim; @@ -13,10 +14,6 @@ class GitHelper { - public function __construct( - protected ConfigHelper $config - ) {} - public function currentBranch(?string $path = null): ?string { if ($this->hasGitDirectory($path)) { @@ -42,6 +39,6 @@ protected function hasGitDirectory(?string $path = null): bool protected function resolvePath(?string $path = null): string { - return realpath($path ?: $this->config->gitPath()); + return realpath($path ?: base_path()); } } diff --git a/src/Jobs/OperationJob.php b/src/Jobs/OperationJob.php index 425b937c..e58a939e 100644 --- a/src/Jobs/OperationJob.php +++ b/src/Jobs/OperationJob.php @@ -6,6 +6,7 @@ use DragonCode\LaravelDeployOperations\Constants\Names; use DragonCode\LaravelDeployOperations\Constants\Options; +use DragonCode\LaravelDeployOperations\Data\Config\ConfigData; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; @@ -14,7 +15,7 @@ use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Artisan; -use function config; +use function app; class OperationJob implements ShouldBeUnique, ShouldQueue { @@ -24,10 +25,10 @@ class OperationJob implements ShouldBeUnique, ShouldQueue use SerializesModels; public function __construct( - public string $filename + public string $filename, ) { - $this->setQueueConnection(); - $this->setQueueName(); + $this->onConnection($this->config()->queue->connection); + $this->onQueue($this->config()->queue->name); } public function handle(): void @@ -43,13 +44,8 @@ public function uniqueId(): string return $this->filename; } - protected function setQueueConnection(): void + protected function config(): ConfigData { - $this->onConnection(config('deploy-operations.queue.connection')); - } - - protected function setQueueName(): void - { - $this->onQueue(config('deploy-operations.queue.name')); + return app(ConfigData::class); } } diff --git a/src/Operation.php b/src/Operation.php index cca3a7ad..064b56b6 100644 --- a/src/Operation.php +++ b/src/Operation.php @@ -5,6 +5,9 @@ namespace DragonCode\LaravelDeployOperations; use DragonCode\LaravelDeployOperations\Concerns\HasArtisan; +use DragonCode\LaravelDeployOperations\Data\Config\ConfigData; + +use function app; abstract class Operation { @@ -26,7 +29,7 @@ public function shouldOnce(): bool */ public function withinTransactions(): bool { - return (bool) config('deploy-operations.transactions.enabled'); + return app(ConfigData::class)->transactions->enabled; } /** @@ -50,7 +53,7 @@ public function needBefore(): bool */ public function shouldBeAsync(): bool { - return (bool) config('deploy-operations.async'); + return app(ConfigData::class)->async; } /** diff --git a/src/Processors/MakeProcessor.php b/src/Processors/MakeProcessor.php index 98bd8760..70c8cbef 100644 --- a/src/Processors/MakeProcessor.php +++ b/src/Processors/MakeProcessor.php @@ -119,6 +119,6 @@ protected function stubPath(): string protected function showFullPath(): bool { - return $this->config->showFullPath(); + return $this->config->show->fullPath; } } diff --git a/src/Processors/Processor.php b/src/Processors/Processor.php index 92681bb9..5bea7231 100644 --- a/src/Processors/Processor.php +++ b/src/Processors/Processor.php @@ -6,9 +6,9 @@ use Closure; use DragonCode\LaravelDeployOperations\Concerns\HasArtisan; +use DragonCode\LaravelDeployOperations\Data\Config\ConfigData; use DragonCode\LaravelDeployOperations\Data\OptionsData; use DragonCode\LaravelDeployOperations\Enums\MethodEnum; -use DragonCode\LaravelDeployOperations\Helpers\ConfigHelper; use DragonCode\LaravelDeployOperations\Helpers\GitHelper; use DragonCode\LaravelDeployOperations\Helpers\SorterHelper; use DragonCode\LaravelDeployOperations\Notifications\Notification; @@ -33,7 +33,7 @@ public function __construct( protected OptionsData $options, protected InputInterface $input, protected OutputStyle $output, - protected ConfigHelper $config, + protected ConfigData $config, protected OperationsRepository $repository, protected GitHelper $git, protected File $file, @@ -55,7 +55,7 @@ protected function getFiles(string $path, ?Closure $filter = null): array $files = Arr::filter( $files, - fn (string $path) => Str::endsWith($path, '.php') && ! Str::contains($path, $this->config->exclude()) + fn (string $path) => Str::endsWith($path, '.php') && ! Str::contains($path, $this->config->exclude) ); return Arr::of($this->sorter->byValues($files)) diff --git a/src/Repositories/OperationsRepository.php b/src/Repositories/OperationsRepository.php index 3961eb50..9a0aa818 100644 --- a/src/Repositories/OperationsRepository.php +++ b/src/Repositories/OperationsRepository.php @@ -5,7 +5,7 @@ namespace DragonCode\LaravelDeployOperations\Repositories; use DragonCode\LaravelDeployOperations\Constants\Order; -use DragonCode\LaravelDeployOperations\Helpers\ConfigHelper; +use DragonCode\LaravelDeployOperations\Data\Config\ConfigData; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionResolverInterface as Resolver; use Illuminate\Database\Query\Builder as Query; @@ -21,7 +21,7 @@ class OperationsRepository public function __construct( protected Resolver $resolver, - protected ConfigHelper $config + protected ConfigData $config, ) {} public function getCompleted(): Collection @@ -67,7 +67,7 @@ public function delete(string $operation): void public function createRepository(): void { - $this->schema()->create($this->config->table(), function (Blueprint $table) { + $this->schema()->create($this->config->table, function (Blueprint $table) { $table->bigIncrements('id'); $table->string('operation'); @@ -78,12 +78,12 @@ public function createRepository(): void public function repositoryExists(): bool { - return $this->schema()->hasTable($this->config->table()); + return $this->schema()->hasTable($this->config->table); } public function deleteRepository(): void { - $this->schema()->dropIfExists($this->config->table()); + $this->schema()->dropIfExists($this->config->table); } /** @@ -112,13 +112,13 @@ protected function schema(): Builder protected function table(): Query { - return $this->getConnection()->table($this->config->table())->useWritePdo(); + return $this->getConnection()->table($this->config->table)->useWritePdo(); } protected function getConnection(): ConnectionInterface { return $this->resolver->connection( - $this->connection ?: $this->config->connection() + $this->connection ?: $this->config->connection ); } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index c26d7bfa..f63c5839 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -5,17 +5,23 @@ namespace DragonCode\LaravelDeployOperations; use DragonCode\LaravelDeployOperations\Concerns\HasAbout; +use DragonCode\LaravelDeployOperations\Data\Config\ConfigData; use DragonCode\LaravelDeployOperations\Listeners\MigrationEndedListener; use Illuminate\Database\Events\MigrationEnded; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider as BaseServiceProvider; +use function config; + class ServiceProvider extends BaseServiceProvider { use HasAbout; public function boot(): void { + $this->registerEvents(); + $this->bootConfig(); + if ($this->app->runningInConsole()) { $this->publishConfig(); $this->publishStub(); @@ -24,8 +30,6 @@ public function boot(): void $this->registerCommands(); $this->registerMigrations(); } - - $this->registerEvents(); } public function register(): void @@ -77,4 +81,11 @@ protected function registerConfig(): void { $this->mergeConfigFrom(__DIR__ . '/../config/deploy-operations.php', 'deploy-operations'); } + + protected function bootConfig(): void + { + $this->app->bind(ConfigData::class, static fn () => ConfigData::from( + config('deploy-operations') + )); + } } diff --git a/src/Services/MigratorService.php b/src/Services/MigratorService.php index 97ccbbf3..a73fecec 100644 --- a/src/Services/MigratorService.php +++ b/src/Services/MigratorService.php @@ -4,9 +4,9 @@ namespace DragonCode\LaravelDeployOperations\Services; +use DragonCode\LaravelDeployOperations\Data\Config\ConfigData; use DragonCode\LaravelDeployOperations\Data\OptionsData; use DragonCode\LaravelDeployOperations\Enums\StatusEnum; -use DragonCode\LaravelDeployOperations\Helpers\ConfigHelper; use DragonCode\LaravelDeployOperations\Jobs\OperationJob; use DragonCode\LaravelDeployOperations\Notifications\Notification; use DragonCode\LaravelDeployOperations\Operation; @@ -30,7 +30,7 @@ public function __construct( protected File $file, protected Notification $notification, protected OperationsRepository $repository, - protected ConfigHelper $config, + protected ConfigData $config, protected Container $container ) {} @@ -121,7 +121,7 @@ protected function runMethod(Operation $operation, string $method, bool $transac { $callback = fn () => $this->container->call([$operation, $method]); - $transactions ? DB::transaction($callback, $this->config->transactionAttempts()) : $callback(); + $transactions ? DB::transaction($callback, $this->config->transactions->attempts) : $callback(); } protected function log(string $name, int $batch): void @@ -175,7 +175,7 @@ protected function resolveOperation(string $path): Operation protected function resolveOperationName(string $path): string { return Str::of(realpath($path)) - ->after(realpath($this->config->basePath()) . DIRECTORY_SEPARATOR) + ->after(realpath($this->config->path) . DIRECTORY_SEPARATOR) ->replace(['\\', '/'], '/') ->before('.php') ->toString(); diff --git a/tests/Commands/OperationsTest.php b/tests/Commands/OperationsTest.php index d5045488..69d5ea78 100644 --- a/tests/Commands/OperationsTest.php +++ b/tests/Commands/OperationsTest.php @@ -5,6 +5,7 @@ namespace Tests\Commands; use DragonCode\LaravelDeployOperations\Constants\Names; +use DragonCode\LaravelDeployOperations\Data\Config\ConfigData; use DragonCode\LaravelDeployOperations\Jobs\OperationJob; use Exception; use Illuminate\Database\Console\Migrations\RollbackCommand; @@ -569,6 +570,8 @@ public function testDirectoryExclusion(): void $this->app['config']->set('deploy-operations.exclude', 'sub_path'); + $this->app->forgetInstance(ConfigData::class); + $table = 'every_time'; $this->artisan(Names::Install)->assertExitCode(0); @@ -619,6 +622,8 @@ public function testFileExclusion(): void $this->app['config']->set('deploy-operations.exclude', 'sub_path/2021_12_15_205804_baz'); + $this->app->forgetInstance(ConfigData::class); + $table = 'every_time'; $this->artisan(Names::Install)->assertExitCode(0); diff --git a/tests/fixtures/app/stubs/2021_02_15_124237_test_success_transactions.stub b/tests/fixtures/app/stubs/2021_02_15_124237_test_success_transactions.stub index 161eaf06..3c60f111 100644 --- a/tests/fixtures/app/stubs/2021_02_15_124237_test_success_transactions.stub +++ b/tests/fixtures/app/stubs/2021_02_15_124237_test_success_transactions.stub @@ -1,6 +1,7 @@