Skip to content

Commit 1964f17

Browse files
authored
Support mixed repository blueprints (#300)
* Support mixed repository blueprints * Fix tests * Use correct config key * Add some test coverage * Ensure we set the file directory
1 parent 4999869 commit 1964f17

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

config/eloquent-driver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'driver' => 'file',
2121
'blueprint_model' => \Statamic\Eloquent\Fields\BlueprintModel::class,
2222
'fieldset_model' => \Statamic\Eloquent\Fields\FieldsetModel::class,
23+
'namespaces' => 'all',
2324
],
2425

2526
'collections' => [

src/Fields/BlueprintRepository.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public function find($blueprint): ?Blueprint
2121
return null;
2222
}
2323

24+
if (! $this->isEloquentDrivenNamespace($namespace)) {
25+
return parent::find($blueprint);
26+
}
27+
2428
$blueprintModel = ($namespace ? $this->filesIn($namespace) : app('statamic.eloquent.blueprints.blueprint_model')::whereNull('namespace'))
2529
->where('handle', $handle)
2630
->first();
@@ -43,13 +47,21 @@ public function save(Blueprint $blueprint)
4347
{
4448
$this->clearBlinkCaches();
4549

50+
if (! $this->isEloquentDrivenNamespace($blueprint->namespace())) {
51+
return parent::save($blueprint);
52+
}
53+
4654
$this->updateModel($blueprint);
4755
}
4856

4957
public function delete(Blueprint $blueprint)
5058
{
5159
$this->clearBlinkCaches();
5260

61+
if (! $this->isEloquentDrivenNamespace($blueprint->namespace())) {
62+
return parent::delete($blueprint);
63+
}
64+
5365
$this->deleteModel($blueprint);
5466
}
5567

@@ -62,6 +74,10 @@ private function clearBlinkCaches()
6274

6375
public function in(string $namespace)
6476
{
77+
if (! $this->isEloquentDrivenNamespace($namespace)) {
78+
return parent::in($namespace);
79+
}
80+
6581
return $this
6682
->filesIn($namespace)
6783
->map(function ($file) {
@@ -202,4 +218,21 @@ private function updateOrderFromBlueprintSections($contents)
202218

203219
return $contents;
204220
}
221+
222+
private function isEloquentDrivenNamespace(?string $namespace)
223+
{
224+
if (! $namespace) {
225+
return true;
226+
}
227+
228+
$eloquentNamespaces = config('statamic.eloquent-driver.blueprints.namespaces', 'all');
229+
230+
if ($eloquentNamespaces !== 'all') {
231+
if (! in_array($namespace, Arr::wrap($eloquentNamespaces))) {
232+
return false;
233+
}
234+
}
235+
236+
return true;
237+
}
205238
}

src/ServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,10 @@ private function registerBlueprints()
275275
return config('statamic.eloquent-driver.blueprints.fieldset_model');
276276
});
277277

278-
$this->app->singleton(
279-
'Statamic\Fields\BlueprintRepository',
280-
'Statamic\Eloquent\Fields\BlueprintRepository'
281-
);
278+
$this->app->singleton(\Statamic\Fields\BlueprintRepository::class, function () {
279+
return (new \Statamic\Eloquent\Fields\BlueprintRepository)
280+
->setDirectory(resource_path('blueprints'));
281+
});
282282

283283
$this->app->singleton(
284284
'Statamic\Fields\FieldsetRepository',

tests/Data/Fields/BlueprintTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Data\Fields;
44

55
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Statamic\Eloquent\Fields\BlueprintModel;
67
use Statamic\Facades\Blueprint;
78
use Tests\TestCase;
89

@@ -14,10 +15,10 @@ public function setUp(): void
1415
{
1516
parent::setUp();
1617

17-
$this->app->singleton(
18-
'Statamic\Fields\BlueprintRepository',
19-
'Statamic\Eloquent\Fields\BlueprintRepository'
20-
);
18+
$this->app->singleton(\Statamic\Fields\BlueprintRepository::class, function () {
19+
return (new \Statamic\Eloquent\Fields\BlueprintRepository)
20+
->setDirectory(resource_path('blueprints'));
21+
});
2122

2223
$this->app->singleton(
2324
'Statamic\Fields\FieldsetRepository',
@@ -70,4 +71,20 @@ public function it_deletes_the_model_when_the_blueprint_is_deleted()
7071

7172
$this->assertNull($model);
7273
}
74+
75+
/** @test */
76+
public function it_uses_file_based_namespaces()
77+
{
78+
config()->set('statamic.eloquent-driver.blueprints.namespaces', ['collections']);
79+
80+
$this->assertCount(1, BlueprintModel::all());
81+
82+
$blueprint = Blueprint::make()
83+
->setNamespace('forms')
84+
->setHandle('test')
85+
->setHidden(true)
86+
->save();
87+
88+
$this->assertCount(1, BlueprintModel::all()); // we check theres no new database entries, ie its been handled by files
89+
}
7390
}

0 commit comments

Comments
 (0)