diff --git a/config/one-time-operations.php b/config/one-time-operations.php index 492a611..fb5a142 100644 --- a/config/one-time-operations.php +++ b/config/one-time-operations.php @@ -11,4 +11,6 @@ // Database Connection Name - Change the model connection, support for Multitenancy // Only change when you want to deviate from your system default repository 'connection' => null, + + 'model' => \TimoKoerber\LaravelOneTimeOperations\Models\Operation::class, ]; diff --git a/src/Commands/OneTimeOperationShowCommand.php b/src/Commands/OneTimeOperationShowCommand.php index 33312ac..0381c44 100644 --- a/src/Commands/OneTimeOperationShowCommand.php +++ b/src/Commands/OneTimeOperationShowCommand.php @@ -5,7 +5,7 @@ use Illuminate\Support\Collection; use Throwable; use TimoKoerber\LaravelOneTimeOperations\Commands\Utils\OperationsLineElement; -use TimoKoerber\LaravelOneTimeOperations\Models\Operation; +use TimoKoerber\LaravelOneTimeOperations\Models\ModelFactory; use TimoKoerber\LaravelOneTimeOperations\OneTimeOperationFile; use TimoKoerber\LaravelOneTimeOperations\OneTimeOperationManager; @@ -75,7 +75,7 @@ protected function shouldDisplayByFilter(string $filterName): bool protected function getOperationLinesForOutput(): Collection { - $operationModels = Operation::all(); + $operationModels = ModelFactory::instance()->all(); $operationFiles = OneTimeOperationManager::getAllOperationFiles(); $operationOutputLines = collect(); diff --git a/src/Commands/OneTimeOperationsProcessCommand.php b/src/Commands/OneTimeOperationsProcessCommand.php index ee26589..7181c9d 100644 --- a/src/Commands/OneTimeOperationsProcessCommand.php +++ b/src/Commands/OneTimeOperationsProcessCommand.php @@ -3,10 +3,11 @@ namespace TimoKoerber\LaravelOneTimeOperations\Commands; use Illuminate\Contracts\Console\Isolatable; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use TimoKoerber\LaravelOneTimeOperations\Jobs\OneTimeOperationProcessJob; -use TimoKoerber\LaravelOneTimeOperations\Models\Operation; +use TimoKoerber\LaravelOneTimeOperations\Models\ModelFactory; use TimoKoerber\LaravelOneTimeOperations\OneTimeOperationFile; use TimoKoerber\LaravelOneTimeOperations\OneTimeOperationManager; @@ -90,7 +91,7 @@ protected function processOperationFile(OneTimeOperationFile $operationFile): in return self::SUCCESS; } - protected function processOperationModel(Operation $operationModel): int + protected function processOperationModel(Model $operationModel): int { if (! $this->components->confirm('Operation was processed before. Process it again?')) { $this->components->info('Operation aborted'); @@ -155,7 +156,7 @@ protected function storeOperation(OneTimeOperationFile $operationFile): void return; } - Operation::storeOperation($operationFile->getOperationName(), $this->isAsyncMode($operationFile)); + ModelFactory::instance()->storeOperation($operationFile->getOperationName(), $this->isAsyncMode($operationFile)); } protected function dispatchOperationJob(OneTimeOperationFile $operationFile) diff --git a/src/Models/ModelFactory.php b/src/Models/ModelFactory.php new file mode 100644 index 0000000..a2ac280 --- /dev/null +++ b/src/Models/ModelFactory.php @@ -0,0 +1,32 @@ +model = $app['config']->get('one-time-operations.model', Operation::class); + } + + /** + * @return Operation + */ + public function model() + { + return new $this->model; + } + + /** + * @return Operation + */ + public static function instance() + { + return app(self::class)->model(); + } +} \ No newline at end of file diff --git a/src/OneTimeOperationFile.php b/src/OneTimeOperationFile.php index 61648d3..84ed8b5 100644 --- a/src/OneTimeOperationFile.php +++ b/src/OneTimeOperationFile.php @@ -2,10 +2,11 @@ namespace TimoKoerber\LaravelOneTimeOperations; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\File; use Illuminate\Support\Str; use SplFileInfo; -use TimoKoerber\LaravelOneTimeOperations\Models\Operation; +use TimoKoerber\LaravelOneTimeOperations\Models\ModelFactory; class OneTimeOperationFile { @@ -40,8 +41,8 @@ public function getClassObject(): OneTimeOperation return $this->classObject; } - public function getModel(): ?Operation + public function getModel(): ?Model { - return Operation::whereName($this->getOperationName())->first(); + return ModelFactory::instance()->whereName($this->getOperationName())->first(); } } diff --git a/src/OneTimeOperationManager.php b/src/OneTimeOperationManager.php index 106eac4..c546eb4 100644 --- a/src/OneTimeOperationManager.php +++ b/src/OneTimeOperationManager.php @@ -3,6 +3,7 @@ namespace TimoKoerber\LaravelOneTimeOperations; use Illuminate\Contracts\Filesystem\FileNotFoundException; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Config; @@ -10,7 +11,7 @@ use Illuminate\Support\Str; use SplFileInfo; use Symfony\Component\Finder\Exception\DirectoryNotFoundException; -use TimoKoerber\LaravelOneTimeOperations\Models\Operation; +use TimoKoerber\LaravelOneTimeOperations\Models\ModelFactory; class OneTimeOperationManager { @@ -44,7 +45,7 @@ public static function getUnprocessedFiles(): Collection return $allOperationFiles->filter(function (SplFileInfo $operationFilepath) { $operation = self::getOperationNameFromFilename($operationFilepath->getBasename()); - return Operation::whereName($operation)->doesntExist(); + return ModelFactory::instance()->whereName($operation)->doesntExist(); }); } @@ -67,12 +68,12 @@ public static function getClassObjectByName(string $operationName): OneTimeOpera return File::getRequire($filepath); } - public static function getModelByName(string $operationName): ?Operation + public static function getModelByName(string $operationName): ?Model { - return Operation::whereName($operationName)->first(); + return ModelFactory::instance()->whereName($operationName)->first(); } - public static function getOperationFileByModel(Operation $operationModel): OneTimeOperationFile + public static function getOperationFileByModel(Model $operationModel): OneTimeOperationFile { $filepath = self::pathToFileByName($operationModel->name); diff --git a/src/Providers/OneTimeOperationsServiceProvider.php b/src/Providers/OneTimeOperationsServiceProvider.php index 74c0d6d..61427f1 100644 --- a/src/Providers/OneTimeOperationsServiceProvider.php +++ b/src/Providers/OneTimeOperationsServiceProvider.php @@ -6,15 +6,20 @@ use TimoKoerber\LaravelOneTimeOperations\Commands\OneTimeOperationShowCommand; use TimoKoerber\LaravelOneTimeOperations\Commands\OneTimeOperationsMakeCommand; use TimoKoerber\LaravelOneTimeOperations\Commands\OneTimeOperationsProcessCommand; +use TimoKoerber\LaravelOneTimeOperations\Models\ModelFactory; class OneTimeOperationsServiceProvider extends ServiceProvider { + public array $singletons = [ + ModelFactory::class + ]; + public function boot(): void { - $this->loadMigrationsFrom([__DIR__.'/../../database/migrations']); + $this->loadMigrationsFrom([__DIR__ . '/../../database/migrations']); $this->publishes([ - __DIR__.'/../../config/one-time-operations.php' => config_path('one-time-operations.php'), + __DIR__ . '/../../config/one-time-operations.php' => config_path('one-time-operations.php'), ]); if ($this->app->runningInConsole()) { @@ -27,7 +32,7 @@ public function boot(): void public function register() { $this->mergeConfigFrom( - __DIR__.'/../../config/one-time-operations.php', 'one-time-operations' + __DIR__ . '/../../config/one-time-operations.php', 'one-time-operations' ); } }