Skip to content

Commit 24e7f80

Browse files
authored
Respect configured namespaces for blueprint import (#470)
* feat: use configured namespaces for import of blueprints * chore: add namespaces to docs * feat: add tests * fix: better names
1 parent c811a01 commit 24e7f80

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ By default, the Eloquent Driver stores all data in a single `data` column. Howev
150150

151151
6. And that's it! Statamic will now read and write data to the new columns in the `entries` table, rather than the `data` column.
152152

153+
### Selecting specific blueprint types for Eloquent
154+
155+
By default, setting the driver for blueprints to `eloquent` will apply to *all* blueprints. However, if you only wish to move certain groups of blueprint over, you can do so by setting an array of namespaces, such as:
156+
157+
```php
158+
// config/statamic/eloquent-driver.php
159+
160+
'blueprints' => [
161+
'driver' => 'eloquent',
162+
'model' => \Statamic\Eloquent\Fields\BlueprintModel::class,
163+
'namespaces' => ['forms', 'navigation'],
164+
],
165+
```
166+
167+
The above example will set all forms and navigation blueprints to use eloquent, while keeping the rest as files.
168+
153169
## Upgrading
154170

155171
After updating to a new version of the Eloquent Driver, please ensure you run `php artisan migrate` to update your database to the latest schema.

src/Commands/ImportBlueprints.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class ImportBlueprints extends Command
2525
protected $signature = 'statamic:eloquent:import-blueprints
2626
{--force : Force the import to run, with all prompts answered "yes"}
2727
{--only-blueprints : Only import blueprints}
28-
{--only-fieldsets : Only import fieldsets}';
28+
{--only-fieldsets : Only import fieldsets}
29+
{--all-blueprints : Import all blueprints, regardless of configured namespaces}';
2930

3031
/**
3132
* The console command description.
@@ -79,6 +80,10 @@ private function importBlueprints(): void
7980
Str::after(Str::before($path, '.yaml'), $directory.'/')
8081
);
8182

83+
if (! $this->shouldImportBlueprint($namespace)) {
84+
return;
85+
}
86+
8287
$contents = YAML::file($path)->parse();
8388
// Ensure sections are ordered correctly.
8489
if (isset($contents['tabs']) && is_array($contents['tabs'])) {
@@ -173,6 +178,16 @@ private function getNamespaceAndHandle(string $blueprint): array
173178
return [$namespace, $handle];
174179
}
175180

181+
private function shouldImportBlueprint($namespace = null): bool
182+
{
183+
$eloquentNamespaces = config('statamic.eloquent-driver.blueprints.namespaces', 'all');
184+
if ($this->option('all-blueprints') || $eloquentNamespaces === 'all') {
185+
return true;
186+
}
187+
188+
return in_array($namespace, $eloquentNamespaces);
189+
}
190+
176191
private function shouldImportBlueprints(): bool
177192
{
178193
return $this->option('only-blueprints')

tests/Commands/ImportBlueprintsTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,64 @@ public function it_imports_blueprints_with_console_question()
106106
$this->assertCount(0, FieldsetModel::all());
107107
}
108108

109+
#[Test]
110+
public function it_imports_only_the_configured_namespaces()
111+
{
112+
config()->set('statamic.eloquent-driver.blueprints.namespaces', ['forms']);
113+
114+
BlueprintFacade::make('contact')->setNamespace('forms')->setContents([
115+
'fields' => [
116+
['handle' => 'name', 'field' => ['type' => 'text']],
117+
['handle' => 'email', 'field' => ['type' => 'text'], 'validate' => 'required'],
118+
],
119+
])->save();
120+
121+
BlueprintFacade::make('images')->setNamespace('assets')->setContents([
122+
'fields' => [
123+
['handle' => 'title', 'field' => ['type' => 'text']],
124+
['handle' => 'email', 'field' => ['type' => 'text'], 'validate' => 'required'],
125+
],
126+
])->save();
127+
128+
$this->artisan('statamic:eloquent:import-blueprints')
129+
->expectsQuestion('Do you want to import blueprints?', true)
130+
->expectsQuestion('Do you want to import fieldsets?', false)
131+
->expectsOutputToContain('Blueprints imported successfully.')
132+
->assertExitCode(0);
133+
134+
$this->assertCount(1, BlueprintModel::query()->where('namespace', 'forms')->get());
135+
$this->assertCount(0, BlueprintModel::query()->where('namespace', 'assets')->get());
136+
}
137+
138+
#[Test]
139+
public function it_imports_everything_when_all_namespaces_flag_is_passed()
140+
{
141+
config()->set('statamic.eloquent-driver.blueprints.namespaces', ['forms']);
142+
143+
BlueprintFacade::make('contact')->setNamespace('forms')->setContents([
144+
'fields' => [
145+
['handle' => 'name', 'field' => ['type' => 'text']],
146+
['handle' => 'email', 'field' => ['type' => 'text'], 'validate' => 'required'],
147+
],
148+
])->save();
149+
150+
BlueprintFacade::make('images')->setNamespace('assets')->setContents([
151+
'fields' => [
152+
['handle' => 'title', 'field' => ['type' => 'text']],
153+
['handle' => 'email', 'field' => ['type' => 'text'], 'validate' => 'required'],
154+
],
155+
])->save();
156+
157+
$this->artisan('statamic:eloquent:import-blueprints', ['--all-blueprints' => true])
158+
->expectsQuestion('Do you want to import blueprints?', true)
159+
->expectsQuestion('Do you want to import fieldsets?', false)
160+
->expectsOutputToContain('Blueprints imported successfully.')
161+
->assertExitCode(0);
162+
163+
$this->assertCount(1, BlueprintModel::query()->where('namespace', 'forms')->get());
164+
$this->assertCount(1, BlueprintModel::query()->where('namespace', 'assets')->get());
165+
}
166+
109167
#[Test]
110168
public function it_imports_fieldsets_with_console_question()
111169
{

0 commit comments

Comments
 (0)