Skip to content

Commit c38d041

Browse files
authored
Fix and improve asset and collection export commands (#548)
* Support `--only-asset-containers` and `--only-assets` in ExportAssets command * Fix export assets step in ExportAssets * Support `--only-collections` and `--only-collection-trees` in ExportCollections * Allow exporting collection trees independently of collections * Add `source_preset` to asset container export * Support `--only-navs` and `--only-nav-trees` in ExportNavs
1 parent 3a96c72 commit c38d041

File tree

3 files changed

+108
-41
lines changed

3 files changed

+108
-41
lines changed

src/Commands/ExportAssets.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Closure;
66
use Illuminate\Console\Command;
77
use Illuminate\Support\Facades\Facade;
8-
use Illuminate\Support\Str;
98
use Statamic\Assets\Asset;
109
use Statamic\Assets\AssetContainer;
1110
use Statamic\Assets\AssetRepository;
@@ -30,7 +29,11 @@ class ExportAssets extends Command
3029
*
3130
* @var string
3231
*/
33-
protected $signature = 'statamic:eloquent:export-assets {--force : Force the export to run, with all prompts answered "yes"}';
32+
protected $signature = 'statamic:eloquent:export-assets
33+
{--force : Force the export to run, with all prompts answered "yes"}
34+
{--only-asset-containers : Only export asset containers}
35+
{--only-assets : Only export assets}
36+
';
3437

3538
/**
3639
* The console command description.
@@ -41,20 +44,18 @@ class ExportAssets extends Command
4144

4245
/**
4346
* Execute the console command.
44-
*
45-
* @return int
4647
*/
47-
public function handle()
48+
public function handle(): int
4849
{
4950
$this->usingDefaultRepositories(function () {
5051
$this->exportAssetContainers();
5152
$this->exportAssets();
5253
});
5354

54-
return 0;
55+
return self::SUCCESS;
5556
}
5657

57-
private function usingDefaultRepositories(Closure $callback)
58+
private function usingDefaultRepositories(Closure $callback): void
5859
{
5960
Facade::clearResolvedInstance(AssetContainerRepositoryContract::class);
6061
Facade::clearResolvedInstance(AssetRepositoryContract::class);
@@ -68,9 +69,9 @@ private function usingDefaultRepositories(Closure $callback)
6869
$callback();
6970
}
7071

71-
private function exportAssetContainers()
72+
private function exportAssetContainers(): void
7273
{
73-
if (! $this->option('force') && ! $this->confirm('Do you want to export asset containers?')) {
74+
if (! $this->shouldExportAssetContainers()) {
7475
return;
7576
}
7677

@@ -82,32 +83,44 @@ private function exportAssetContainers()
8283
->handle($model->handle)
8384
->disk($model->disk ?? config('filesystems.default'))
8485
->searchIndex($model->settings['search_index'] ?? null)
86+
->sourcePreset($model->settings['source_preset'] ?? null)
8587
->save();
8688
});
8789

8890
$this->newLine();
8991
$this->info('Asset containers imported');
9092
}
9193

92-
private function exportAssets()
94+
private function exportAssets(): void
9395
{
94-
if (! $this->option('force') && ! $this->confirm('Do you want to export assets?')) {
96+
if (! $this->shouldExportAssets()) {
9597
return;
9698
}
9799

98100
$assets = AssetModel::all();
99101

100102
$this->withProgressBar($assets, function ($model) {
101-
$container = Str::before($model->handle, '::');
102-
$path = Str::after($model->handle, '::');
103-
104103
AssetFacade::make()
105-
->container($container)
106-
->path($path)
107-
->writeMeta($model->data);
104+
->container($model->container)
105+
->path($model->path)
106+
->writeMeta($model->meta);
108107
});
109108

110109
$this->newLine();
111110
$this->info('Assets imported');
112111
}
112+
113+
private function shouldExportAssetContainers(): bool
114+
{
115+
return $this->option('only-asset-containers')
116+
|| ! $this->option('only-assets')
117+
&& ($this->option('force') || $this->confirm('Do you want to export asset containers?'));
118+
}
119+
120+
private function shouldExportAssets(): bool
121+
{
122+
return $this->option('only-assets')
123+
|| ! $this->option('only-asset-containers')
124+
&& ($this->option('force') || $this->confirm('Do you want to export assets?'));
125+
}
113126
}

src/Commands/ExportCollections.php

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ class ExportCollections extends Command
2929
*
3030
* @var string
3131
*/
32-
protected $signature = 'statamic:eloquent:export-collections {--force : Force the export to run, with all prompts answered "yes"}';
32+
protected $signature = 'statamic:eloquent:export-collections
33+
{--force : Force the export to run, with all prompts answered "yes"}
34+
{--only-collections : Only export collections}
35+
{--only-collection-trees : Only export collection trees}';
3336

3437
/**
3538
* The console command description.
@@ -40,37 +43,59 @@ class ExportCollections extends Command
4043

4144
/**
4245
* Execute the console command.
43-
*
44-
* @return int
4546
*/
46-
public function handle()
47+
public function handle(): int
4748
{
48-
$this->usingDefaultRepositories(function () {
49+
$this->usingDefaultCollectionRepositories(function () {
4950
$this->exportCollections();
51+
});
52+
53+
$this->usingDefaultCollectionTreeRepositories(function () {
5054
$this->exportCollectionTrees();
5155
});
5256

5357
$this->newLine();
5458
$this->info('Collections exported');
5559

56-
return 0;
60+
return self::SUCCESS;
5761
}
5862

59-
private function usingDefaultRepositories(Closure $callback)
63+
private function usingDefaultCollectionRepositories(Closure $callback): void
6064
{
65+
$originalRepo = get_class(app()->make(CollectionRepositoryContract::class));
66+
$originalCollection = get_class(app()->make(CollectionContract::class));
67+
6168
Facade::clearResolvedInstance(CollectionRepositoryContract::class);
62-
Facade::clearResolvedInstance(CollectionTreeRepositoryContract::class);
6369

6470
Statamic::repository(CollectionRepositoryContract::class, CollectionRepository::class);
65-
Statamic::repository(CollectionTreeRepositoryContract::class, CollectionTreeRepository::class);
6671
app()->bind(CollectionContract::class, EloquentCollection::class);
6772

6873
$callback();
74+
75+
Statamic::repository(CollectionRepositoryContract::class, $originalRepo);
76+
app()->bind(CollectionContract::class, $originalCollection);
77+
78+
Facade::clearResolvedInstance(CollectionRepositoryContract::class);
6979
}
7080

71-
private function exportCollections()
81+
private function usingDefaultCollectionTreeRepositories(Closure $callback): void
7282
{
73-
if (! $this->option('force') && ! $this->confirm('Do you want to export collections?')) {
83+
$originalTreeRepo = get_class(app()->make(CollectionTreeRepositoryContract::class));
84+
85+
Facade::clearResolvedInstance(CollectionTreeRepositoryContract::class);
86+
87+
Statamic::repository(CollectionTreeRepositoryContract::class, CollectionTreeRepository::class);
88+
89+
$callback();
90+
91+
Statamic::repository(CollectionTreeRepositoryContract::class, $originalTreeRepo);
92+
93+
Facade::clearResolvedInstance(CollectionTreeRepositoryContract::class);
94+
}
95+
96+
private function exportCollections(): void
97+
{
98+
if (! $this->shouldExportCollections()) {
7499
return;
75100
}
76101

@@ -109,9 +134,9 @@ private function exportCollections()
109134
$this->info('Collections exported');
110135
}
111136

112-
private function exportCollectionTrees()
137+
private function exportCollectionTrees(): void
113138
{
114-
if (! $this->option('force') && ! $this->confirm('Do you want to export collection trees?')) {
139+
if (! $this->shouldExportCollectionTrees()) {
115140
return;
116141
}
117142

@@ -130,4 +155,18 @@ private function exportCollectionTrees()
130155
$this->newLine();
131156
$this->info('Collection trees exported');
132157
}
158+
159+
private function shouldExportCollections(): bool
160+
{
161+
return $this->option('only-collections')
162+
|| ! $this->option('only-collection-trees')
163+
&& ($this->option('force') || $this->confirm('Do you want to export collections?'));
164+
}
165+
166+
private function shouldExportCollectionTrees(): bool
167+
{
168+
return $this->option('only-collection-trees')
169+
|| ! $this->option('only-collections')
170+
&& ($this->option('force') || $this->confirm('Do you want to export collections trees?'));
171+
}
133172
}

src/Commands/ExportNavs.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class ExportNavs extends Command
2828
*
2929
* @var string
3030
*/
31-
protected $signature = 'statamic:eloquent:export-navs {--force : Force the export to run, with all prompts answered "yes"}';
31+
protected $signature = 'statamic:eloquent:export-navs
32+
{--force : Force the export to run, with all prompts answered "yes"}
33+
{--only-navs : Only export navigations}
34+
{--only-nav-trees : Only export navigation trees}';
3235

3336
/**
3437
* The console command description.
@@ -39,20 +42,18 @@ class ExportNavs extends Command
3942

4043
/**
4144
* Execute the console command.
42-
*
43-
* @return int
4445
*/
45-
public function handle()
46+
public function handle(): int
4647
{
4748
$this->usingDefaultRepositories(function () {
4849
$this->exportNavs();
4950
$this->exportNavTrees();
5051
});
5152

52-
return 0;
53+
return self::SUCCESS;
5354
}
5455

55-
private function usingDefaultRepositories(Closure $callback)
56+
private function usingDefaultRepositories(Closure $callback): void
5657
{
5758
Facade::clearResolvedInstance(NavigationRepositoryContract::class);
5859
Facade::clearResolvedInstance(NavTreeRepositoryContract::class);
@@ -66,16 +67,16 @@ private function usingDefaultRepositories(Closure $callback)
6667
$callback();
6768
}
6869

69-
private function exportNavs()
70+
private function exportNavs(): void
7071
{
71-
if (! $this->option('force') && ! $this->confirm('Do you want to export navs?')) {
72+
if (! $this->shouldExportNavigations()) {
7273
return;
7374
}
7475

7576
$navs = NavModel::all();
7677

7778
$this->withProgressBar($navs, function ($model) {
78-
$nav = NavFacade::make()
79+
NavFacade::make()
7980
->handle($model->handle)
8081
->title($model->title)
8182
->collections($model->settings['collections'] ?? null)
@@ -88,9 +89,9 @@ private function exportNavs()
8889
$this->info('Navs exported');
8990
}
9091

91-
private function exportNavTrees()
92+
private function exportNavTrees(): void
9293
{
93-
if (! $this->option('force') && ! $this->confirm('Do you want to export nav trees?')) {
94+
if (! $this->shouldExportNavigationTrees()) {
9495
return;
9596
}
9697

@@ -113,4 +114,18 @@ private function exportNavTrees()
113114
$this->newLine();
114115
$this->info('Nav trees exported');
115116
}
117+
118+
private function shouldExportNavigations(): bool
119+
{
120+
return $this->option('only-navs')
121+
|| ! $this->option('only-nav-trees')
122+
&& ($this->option('force') || $this->confirm('Do you want to export navs?'));
123+
}
124+
125+
private function shouldExportNavigationTrees(): bool
126+
{
127+
return $this->option('only-nav-trees')
128+
|| ! $this->option('only-navs')
129+
&& ($this->option('force') || $this->confirm('Do you want to export nav trees?'));
130+
}
116131
}

0 commit comments

Comments
 (0)