Skip to content

Commit b85b882

Browse files
authored
Allow blueprints and field sets to be split repository (#302)
1 parent 51ae142 commit b85b882

File tree

7 files changed

+233
-23
lines changed

7 files changed

+233
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ vendor
44
.php_cs.cache
55
.nova/*
66
.phpunit.cache
7+
.idea

config/eloquent-driver.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919
'blueprints' => [
2020
'driver' => 'file',
21-
'blueprint_model' => \Statamic\Eloquent\Fields\BlueprintModel::class,
22-
'fieldset_model' => \Statamic\Eloquent\Fields\FieldsetModel::class,
21+
'model' => \Statamic\Eloquent\Fields\BlueprintModel::class,
2322
'namespaces' => 'all',
2423
],
2524

@@ -42,6 +41,11 @@
4241
'entry' => \Statamic\Eloquent\Entries\Entry::class,
4342
],
4443

44+
'fieldsets' => [
45+
'driver' => 'file',
46+
'model' => \Statamic\Eloquent\Fields\FieldsetModel::class,
47+
],
48+
4549
'forms' => [
4650
'driver' => 'file',
4751
'model' => \Statamic\Eloquent\Forms\FormModel::class,

src/Commands/ImportBlueprints.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class ImportBlueprints extends Command
2222
*
2323
* @var string
2424
*/
25-
protected $signature = 'statamic:eloquent:import-blueprints';
25+
protected $signature = 'statamic:eloquent:import-blueprints
26+
{--force : Force the import to run, with all prompts answered "yes"}
27+
{--only-blueprints : Only import blueprints}
28+
{--only-fieldsets : Only import fieldsets}';
2629

2730
/**
2831
* The console command description.
@@ -62,6 +65,10 @@ private function useDefaultRepositories(): void
6265

6366
private function importBlueprints(): void
6467
{
68+
if (! $this->shouldImportBlueprints()) {
69+
return;
70+
}
71+
6572
$directory = resource_path('blueprints');
6673

6774
$files = File::withAbsolutePaths()
@@ -105,7 +112,7 @@ private function importBlueprints(): void
105112

106113
$lastModified = Carbon::createFromTimestamp(File::lastModified($path));
107114

108-
app('statamic.eloquent.blueprints.blueprint_model')::firstOrNew([
115+
app('statamic.eloquent.blueprints.model')::firstOrNew([
109116
'handle' => $blueprint->handle(),
110117
'namespace' => $blueprint->namespace() ?? null,
111118
])
@@ -118,6 +125,10 @@ private function importBlueprints(): void
118125

119126
private function importFieldsets(): void
120127
{
128+
if (! $this->shouldImportFieldsets()) {
129+
return;
130+
}
131+
121132
$directory = resource_path('fieldsets');
122133

123134
$files = File::withAbsolutePaths()
@@ -132,7 +143,7 @@ private function importFieldsets(): void
132143

133144
$lastModified = Carbon::createFromTimestamp(File::lastModified($path));
134145

135-
app('statamic.eloquent.blueprints.fieldset_model')::firstOrNew([
146+
app('statamic.eloquent.fieldsets.model')::firstOrNew([
136147
'handle' => $fieldset->handle(),
137148
])
138149
->fill(['data' => $fieldset->contents(), 'created_at' => $lastModified, 'updated_at' => $lastModified])
@@ -152,4 +163,18 @@ private function getNamespaceAndHandle(string $blueprint): array
152163

153164
return [$namespace, $handle];
154165
}
166+
167+
private function shouldImportBlueprints(): bool
168+
{
169+
return $this->option('only-blueprints')
170+
|| ! $this->option('only-fieldsets')
171+
&& ($this->option('force') || $this->confirm('Do you want to import blueprints?'));
172+
}
173+
174+
private function shouldImportFieldsets(): bool
175+
{
176+
return $this->option('only-fieldsets')
177+
|| ! $this->option('only-blueprints')
178+
&& ($this->option('force') || $this->confirm('Do you want to import fieldsets?'));
179+
}
155180
}

src/Fields/BlueprintRepository.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public function find($blueprint): ?Blueprint
2525
return parent::find($blueprint);
2626
}
2727

28-
$blueprintModel = ($namespace ? $this->filesIn($namespace) : app('statamic.eloquent.blueprints.blueprint_model')::whereNull('namespace'))
28+
$blueprintModel = ($namespace ? $this->filesIn($namespace) : app('statamic.eloquent.blueprints.model')::whereNull('namespace'))
2929
->where('handle', $handle)
3030
->first();
3131

3232
if (! $blueprintModel) {
3333
throw_if(
3434
$namespace === null && $handle === 'default',
35-
Exception::class,
35+
\Exception::class,
3636
'Default Blueprint is required but not found. '
3737
);
3838

@@ -96,10 +96,14 @@ public function in(string $namespace)
9696

9797
protected function filesIn($namespace)
9898
{
99+
if (! $this->isEloquentDrivenNamespace($namespace)) {
100+
return parent::filesIn($namespace);
101+
}
102+
99103
return Blink::store(self::BLINK_NAMESPACE_PATHS)->once($namespace ?? 'none', function () use ($namespace) {
100104
$namespace = str_replace('/', '.', $namespace);
101105

102-
if (count($blueprintModels = app('statamic.eloquent.blueprints.blueprint_model')::where('namespace', $namespace)->get()) == 0) {
106+
if (count($blueprintModels = app('statamic.eloquent.blueprints.model')::where('namespace', $namespace)->get()) == 0) {
103107
return collect();
104108
}
105109

@@ -138,14 +142,14 @@ protected function getNamespaceAndHandle($blueprint)
138142

139143
public function getModel($blueprint)
140144
{
141-
return app('statamic.eloquent.blueprints.blueprint_model')::where('namespace', $blueprint->namespace() ?? null)
145+
return app('statamic.eloquent.blueprints.model')::where('namespace', $blueprint->namespace() ?? null)
142146
->where('handle', $blueprint->handle())
143147
->first();
144148
}
145149

146150
public function updateModel($blueprint)
147151
{
148-
$model = app('statamic.eloquent.blueprints.blueprint_model')::firstOrNew([
152+
$model = app('statamic.eloquent.blueprints.model')::firstOrNew([
149153
'handle' => $blueprint->handle(),
150154
'namespace' => $blueprint->namespace() ?? null,
151155
]);

src/Fields/FieldsetRepository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class FieldsetRepository extends StacheRepository
1212
public function all(): Collection
1313
{
1414
return Blink::once('eloquent-fieldsets', function () {
15-
if (count($models = app('statamic.eloquent.blueprints.fieldset_model')::get() ?? collect()) === 0) {
15+
if (count($models = app('statamic.eloquent.fieldsets.model')::get() ?? collect()) === 0) {
1616
return collect();
1717
}
1818

@@ -47,7 +47,7 @@ public function delete(Fieldset $fieldset)
4747

4848
public function updateModel($fieldset)
4949
{
50-
$model = app('statamic.eloquent.blueprints.fieldset_model')::firstOrNew([
50+
$model = app('statamic.eloquent.fieldsets.model')::firstOrNew([
5151
'handle' => $fieldset->handle(),
5252
]);
5353

@@ -59,7 +59,7 @@ public function updateModel($fieldset)
5959

6060
public function deleteModel($fieldset)
6161
{
62-
$model = app('statamic.eloquent.blueprints.fieldset_model')::where('handle', $fieldset->handle())->first();
62+
$model = app('statamic.eloquent.fieldsets.model')::where('handle', $fieldset->handle())->first();
6363

6464
if ($model) {
6565
$model->delete();

src/ServiceProvider.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,12 @@ private function publishMigrations(): void
138138

139139
$this->publishes($blueprintMigrations = [
140140
__DIR__.'/../database/migrations/2024_03_07_100000_create_blueprints_table.php' => database_path('migrations/2024_03_07_100000_create_blueprints_table.php'),
141-
__DIR__.'/../database/migrations/2024_03_07_100000_create_fieldsets_table.php' => database_path('migrations/2024_03_07_100000_create_fieldsets_table.php'),
142141
], 'statamic-eloquent-blueprint-migrations');
143142

143+
$this->publishes($fieldsetMigrations = [
144+
__DIR__.'/../database/migrations/2024_03_07_100000_create_fieldsets_table.php' => database_path('migrations/2024_03_07_100000_create_fieldsets_table.php'),
145+
], 'statamic-eloquent-fieldset-migrations');
146+
144147
$this->publishes($formMigrations = [
145148
__DIR__.'/../database/migrations/2024_03_07_100000_create_forms_table.php' => database_path('migrations/2024_03_07_100000_create_forms_table.php'),
146149
], 'statamic-eloquent-form-migrations');
@@ -176,6 +179,7 @@ private function publishMigrations(): void
176179
$navigationTreeMigrations,
177180
$collectionMigrations,
178181
$blueprintMigrations,
182+
$fieldsetMigrations,
179183
$formMigrations,
180184
$formSubmissionMigrations,
181185
$assetContainerMigrations,
@@ -203,6 +207,7 @@ public function register()
203207
$this->registerCollections();
204208
$this->registerCollectionTrees();
205209
$this->registerEntries();
210+
$this->registerFieldsets();
206211
$this->registerForms();
207212
$this->registerFormSubmissions();
208213
$this->registerGlobals();
@@ -267,23 +272,19 @@ private function registerBlueprints()
267272
return;
268273
}
269274

270-
$this->app->bind('statamic.eloquent.blueprints.blueprint_model', function () {
271-
return config('statamic.eloquent-driver.blueprints.blueprint_model');
275+
$this->app->bind('statamic.eloquent.blueprints.model', function () {
276+
return config('statamic.eloquent-driver.blueprints.model', config('statamic.eloquent-driver.blueprints.blueprint_model'));
272277
});
273278

274-
$this->app->bind('statamic.eloquent.blueprints.fieldset_model', function () {
275-
return config('statamic.eloquent-driver.blueprints.fieldset_model');
279+
// @deprecated
280+
$this->app->bind('statamic.eloquent.blueprints.blueprints_model', function () {
281+
return config('statamic.eloquent-driver.blueprints.model', config('statamic.eloquent-driver.blueprints.blueprint_model'));
276282
});
277283

278284
$this->app->singleton(\Statamic\Fields\BlueprintRepository::class, function () {
279285
return (new \Statamic\Eloquent\Fields\BlueprintRepository)
280286
->setDirectory(resource_path('blueprints'));
281287
});
282-
283-
$this->app->singleton(
284-
'Statamic\Fields\FieldsetRepository',
285-
'Statamic\Eloquent\Fields\FieldsetRepository'
286-
);
287288
}
288289

289290
private function registerCollections()
@@ -345,6 +346,29 @@ private function registerEntries()
345346
Statamic::repository(EntryRepositoryContract::class, EntryRepository::class);
346347
}
347348

349+
private function registerFieldsets()
350+
{
351+
$usingOldConfigKeys = config()->has('statamic.eloquent-driver.blueprints.fieldset_model');
352+
353+
if (config($usingOldConfigKeys ? 'statamic.eloquent-driver.blueprints.driver' : 'statamic.eloquent-driver.fieldsets.driver', 'file') != 'eloquent') {
354+
return;
355+
}
356+
357+
$this->app->bind('statamic.eloquent.fieldsets.model', function () {
358+
return config('statamic.eloquent-driver.fieldsets.model', config('statamic.eloquent-driver.blueprints.fieldset_model'));
359+
});
360+
361+
// @deprecated
362+
$this->app->bind('statamic.eloquent.blueprints.fieldset_model', function () {
363+
return config('statamic.eloquent-driver.fieldsets.model', config('statamic.eloquent-driver.blueprints.fieldset_model'));
364+
});
365+
366+
$this->app->singleton(
367+
'Statamic\Fields\FieldsetRepository',
368+
'Statamic\Eloquent\Fields\FieldsetRepository'
369+
);
370+
}
371+
348372
private function registerForms()
349373
{
350374
if (config('statamic.eloquent-driver.forms.driver', 'file') != 'eloquent') {

0 commit comments

Comments
 (0)