diff --git a/src/Console/Commands/InstallEloquentDriver.php b/src/Console/Commands/InstallEloquentDriver.php index aefeaaed74..3f41b2450c 100644 --- a/src/Console/Commands/InstallEloquentDriver.php +++ b/src/Console/Commands/InstallEloquentDriver.php @@ -28,7 +28,10 @@ class InstallEloquentDriver extends Command * * @var string */ - protected $signature = 'statamic:install:eloquent-driver'; + protected $signature = 'statamic:install:eloquent-driver + { --all : Configures all repositories to use the database } + { --import : Whether existing data should be imported } + { --without-messages : Disables output messages }'; /** * The console command description. @@ -50,12 +53,12 @@ public function handle() message: 'Installing the statamic/eloquent-driver package...' ); - $this->components->info('Installed statamic/eloquent-driver package'); + $this->infoMessage('Installed statamic/eloquent-driver package'); } if (! File::exists(config_path('statamic/eloquent-driver.php'))) { $this->runArtisanCommand('vendor:publish --tag=statamic-eloquent-config'); - $this->components->info('Config file [config/statamic/eloquent-driver.php] published successfully.'); + $this->infoMessage('Config file [config/statamic/eloquent-driver.php] published successfully.'); } try { @@ -69,14 +72,7 @@ public function handle() return $this->components->warn("No repositories left to migrate. You're already using the Eloquent Driver for all repositories."); } - $repositories = multiselect( - label: 'Which repositories would you like to migrate?', - hint: 'You can always import other repositories later.', - options: $this->availableRepositories()->all(), - validate: fn (array $values) => count($values) === 0 - ? 'You must select at least one repository to migrate.' - : null - ); + $repositories = $this->repositories(); foreach ($repositories as $repository) { $method = 'migrate'.Str::studly($repository); @@ -84,6 +80,22 @@ public function handle() } } + protected function repositories(): array + { + if ($this->option('all')) { + return $this->availableRepositories()->keys()->all(); + } + + return multiselect( + label: 'Which repositories would you like to migrate?', + options: $this->availableRepositories()->all(), + validate: fn (array $values) => count($values) === 0 + ? 'You must select at least one repository to migrate.' + : null, + hint: 'You can always import other repositories later.' + ); + } + protected function availableRepositories(): Collection { return collect([ @@ -177,15 +189,15 @@ protected function migrateAssetContainers(): void message: 'Migrating asset containers...' ); - $this->components->info('Configured asset containers'); + $this->infoMessage('Configured asset containers'); - if (confirm('Would you like to import existing asset containers?')) { + if ($this->shouldImport('asset containers')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-assets --force --only-asset-containers'), message: 'Importing existing asset containers...' ); - $this->components->info('Imported existing asset containers'); + $this->infoMessage('Imported existing asset containers'); } } @@ -201,15 +213,15 @@ protected function migrateAssets(): void message: 'Migrating assets...' ); - $this->components->info('Configured assets'); + $this->infoMessage('Configured assets'); - if (confirm('Would you like to import existing assets?')) { + if ($this->shouldImport('assets')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-assets --force --only-assets'), message: 'Importing existing assets...' ); - $this->components->info('Imported existing assets'); + $this->infoMessage('Imported existing assets'); } } @@ -225,15 +237,15 @@ protected function migrateBlueprints(): void message: 'Migrating blueprints...' ); - $this->components->info('Configured blueprints'); + $this->infoMessage('Configured blueprints'); - if (confirm('Would you like to import existing blueprints?')) { + if ($this->shouldImport('blueprints')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-blueprints --force --only-blueprints'), message: 'Importing existing blueprints...' ); - $this->components->info('Imported existing blueprints'); + $this->infoMessage('Imported existing blueprints'); } } @@ -249,15 +261,15 @@ protected function migrateCollections(): void message: 'Migrating collections...' ); - $this->components->info('Configured collections'); + $this->infoMessage('Configured collections'); - if (confirm('Would you like to import existing collections?')) { + if ($this->shouldImport('collections')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-collections --force --only-collections'), message: 'Importing existing collections...' ); - $this->components->info('Imported existing collections'); + $this->infoMessage('Imported existing collections'); } } @@ -273,21 +285,21 @@ protected function migrateCollectionTrees(): void message: 'Migrating collection trees...' ); - $this->components->info('Configured collection trees'); + $this->infoMessage('Configured collection trees'); - if (confirm('Would you like to import existing collection trees?')) { + if ($this->shouldImport('collection trees')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-collections --force --only-collection-trees'), message: 'Importing existing collections...' ); - $this->components->info('Imported existing collection trees'); + $this->infoMessage('Imported existing collection trees'); } } protected function migrateEntries(): void { - $shouldImportEntries = confirm('Would you like to import existing entries?'); + $shouldImportEntries = $this->shouldImport('entries'); spin( callback: function () use ($shouldImportEntries) { @@ -325,7 +337,7 @@ protected function migrateEntries(): void : 'Migrating and importing entries...' ); - $this->components->info( + $this->infoMessage( $shouldImportEntries ? 'Configured & imported existing entries' : 'Configured entries' @@ -344,15 +356,15 @@ protected function migrateFieldsets(): void message: 'Migrating fieldsets...' ); - $this->components->info('Configured fieldsets'); + $this->infoMessage('Configured fieldsets'); - if (confirm('Would you like to import existing fieldsets?')) { + if ($this->shouldImport('fieldsets')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-blueprints --force --only-fieldsets'), message: 'Importing existing fieldsets...' ); - $this->components->info('Imported existing fieldsets'); + $this->infoMessage('Imported existing fieldsets'); } } @@ -368,15 +380,15 @@ protected function migrateForms(): void message: 'Migrating forms...' ); - $this->components->info('Configured forms'); + $this->infoMessage('Configured forms'); - if (confirm('Would you like to import existing forms?')) { + if ($this->shouldImport('forms')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-forms --only-forms'), message: 'Importing existing forms...' ); - $this->components->info('Imported existing forms'); + $this->infoMessage('Imported existing forms'); } } @@ -392,15 +404,15 @@ protected function migrateFormSubmissions(): void message: 'Migrating form submissions...' ); - $this->components->info('Configured form submissions'); + $this->infoMessage('Configured form submissions'); - if (confirm('Would you like to import existing form submissions?')) { + if ($this->shouldImport('form submissions')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-forms --only-form-submissions'), message: 'Importing existing form submissions...' ); - $this->components->info('Imported existing form submissions'); + $this->infoMessage('Imported existing form submissions'); } } @@ -416,15 +428,15 @@ protected function migrateGlobals(): void message: 'Migrating globals...' ); - $this->components->info('Configured globals'); + $this->infoMessage('Configured globals'); - if (confirm('Would you like to import existing globals?')) { + if ($this->shouldImport('globals')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-globals --only-global-sets'), message: 'Importing existing globals...' ); - $this->components->info('Imported existing globals'); + $this->infoMessage('Imported existing globals'); } } @@ -440,15 +452,15 @@ protected function migrateGlobalVariables(): void message: 'Migrating global variables...' ); - $this->components->info('Configured global variables'); + $this->infoMessage('Configured global variables'); - if (confirm('Would you like to import existing global variables?')) { + if ($this->shouldImport('global variables')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-globals --only-global-variables'), message: 'Importing existing global variables...' ); - $this->components->info('Imported existing global variables'); + $this->infoMessage('Imported existing global variables'); } } @@ -464,15 +476,15 @@ protected function migrateNavs(): void message: 'Migrating navs...' ); - $this->components->info('Configured navs'); + $this->infoMessage('Configured navs'); - if (confirm('Would you like to import existing navs?')) { + if ($this->shouldImport('navs')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-navs --force --only-navs'), message: 'Importing existing navs...' ); - $this->components->info('Imported existing navs'); + $this->infoMessage('Imported existing navs'); } } @@ -488,15 +500,15 @@ protected function migrateNavTrees(): void message: 'Migrating nav trees...' ); - $this->components->info('Configured nav trees'); + $this->infoMessage('Configured nav trees'); - if (confirm('Would you like to import existing nav trees?')) { + if ($this->shouldImport('nav trees')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-navs --force --only-nav-trees'), message: 'Importing existing nav trees...' ); - $this->components->info('Imported existing navs trees'); + $this->infoMessage('Imported existing navs trees'); } } @@ -511,15 +523,15 @@ protected function migrateRevisions(): void }, ); - $this->components->info('Configured revisions'); + $this->infoMessage('Configured revisions'); - if (confirm('Would you like to import existing revisions?')) { + if ($this->shouldImport('revisions')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-revisions'), message: 'Importing existing revisions...' ); - $this->components->info('Imported existing revisions'); + $this->infoMessage('Imported existing revisions'); } } @@ -537,7 +549,7 @@ protected function migrateSites(): void message: 'Migrating sites...' ); - $this->components->info('Configured & imported sites'); + $this->infoMessage('Configured & imported sites'); } protected function migrateTaxonomies(): void @@ -552,15 +564,15 @@ protected function migrateTaxonomies(): void message: 'Migrating taxonomies...' ); - $this->components->info('Configured taxonomies'); + $this->infoMessage('Configured taxonomies'); - if (confirm('Would you like to import existing taxonomies?')) { + if ($this->shouldImport('taxonomies')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-taxonomies --force --only-taxonomies'), message: 'Importing existing taxonomies...' ); - $this->components->info('Imported existing taxonomies'); + $this->infoMessage('Imported existing taxonomies'); } } @@ -576,15 +588,15 @@ protected function migrateTerms(): void message: 'Migrating terms...' ); - $this->components->info('Configured terms'); + $this->infoMessage('Configured terms'); - if (confirm('Would you like to import existing terms?')) { + if ($this->shouldImport('terms')) { spin( callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-taxonomies --force --only-terms'), message: 'Importing existing terms...' ); - $this->components->info('Imported existing terms'); + $this->infoMessage('Imported existing terms'); } } @@ -600,7 +612,21 @@ protected function migrateTokens(): void message: 'Migrating tokens...' ); - $this->components->info('Configured tokens'); + $this->infoMessage('Configured tokens'); + } + + private function shouldImport(string $repository): bool + { + return $this->option('import') || confirm("Would you like to import existing {$repository}?"); + } + + private function infoMessage(string $message): void + { + if ($this->option('without-messages')) { + return; + } + + $this->components->info('Configured asset containers'); } private function switchToEloquentDriver(string $repository): void