From 3f8cf1b5de28d5a25b910c2c1cb103cc55734860 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Wed, 12 Jun 2024 11:49:31 +0200 Subject: [PATCH 1/5] Support mixed repository blueprints --- config/eloquent-driver.php | 1 + src/Fields/BlueprintRepository.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/config/eloquent-driver.php b/config/eloquent-driver.php index 33c0dbb0..a7a6baf2 100644 --- a/config/eloquent-driver.php +++ b/config/eloquent-driver.php @@ -20,6 +20,7 @@ 'driver' => 'file', 'blueprint_model' => \Statamic\Eloquent\Fields\BlueprintModel::class, 'fieldset_model' => \Statamic\Eloquent\Fields\FieldsetModel::class, + 'namespaces' => 'all', ], 'collections' => [ diff --git a/src/Fields/BlueprintRepository.php b/src/Fields/BlueprintRepository.php index 25ba29bb..0b6c095a 100644 --- a/src/Fields/BlueprintRepository.php +++ b/src/Fields/BlueprintRepository.php @@ -21,6 +21,10 @@ public function find($blueprint): ?Blueprint return null; } + if ($this->isEloquentDrivenNamespace($namespace)) { + return parent::find($blueprint); + } + $blueprintModel = ($namespace ? $this->filesIn($namespace) : app('statamic.eloquent.blueprints.blueprint_model')::whereNull('namespace')) ->where('handle', $handle) ->first(); @@ -43,6 +47,10 @@ public function save(Blueprint $blueprint) { $this->clearBlinkCaches(); + if ($this->isEloquentDrivenNamespace($blueprint->namespace())) { + return parent::save($blueprint); + } + $this->updateModel($blueprint); } @@ -50,6 +58,10 @@ public function delete(Blueprint $blueprint) { $this->clearBlinkCaches(); + if ($this->isEloquentDrivenNamespace($blueprint->namespace())) { + return parent::delete($blueprint); + } + $this->deleteModel($blueprint); } @@ -62,6 +74,10 @@ private function clearBlinkCaches() public function in(string $namespace) { + if ($this->isEloquentDrivenNamespace($namespace)) { + return parent::in($namespace); + } + return $this ->filesIn($namespace) ->map(function ($file) { @@ -202,4 +218,17 @@ private function updateOrderFromBlueprintSections($contents) return $contents; } + + private function isEloquentDrivenNamespace(string $namespace) + { + $eloquentNamespaces = config('statamic.eloquent-driver.namespaces', 'all'); + + if ($eloquentNamespaces !== 'all') { + if (! in_array($namespace, Arr::wrap($eloquentNamespaces))) { + return false; + } + } + + return true; + } } From 32507803423fa3e993a920cec1f13755b32d0d1a Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Wed, 12 Jun 2024 11:57:45 +0200 Subject: [PATCH 2/5] Fix tests --- src/Fields/BlueprintRepository.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Fields/BlueprintRepository.php b/src/Fields/BlueprintRepository.php index 0b6c095a..8a83cbef 100644 --- a/src/Fields/BlueprintRepository.php +++ b/src/Fields/BlueprintRepository.php @@ -21,7 +21,7 @@ public function find($blueprint): ?Blueprint return null; } - if ($this->isEloquentDrivenNamespace($namespace)) { + if (! $this->isEloquentDrivenNamespace($namespace)) { return parent::find($blueprint); } @@ -47,7 +47,7 @@ public function save(Blueprint $blueprint) { $this->clearBlinkCaches(); - if ($this->isEloquentDrivenNamespace($blueprint->namespace())) { + if (! $this->isEloquentDrivenNamespace($blueprint->namespace())) { return parent::save($blueprint); } @@ -58,7 +58,7 @@ public function delete(Blueprint $blueprint) { $this->clearBlinkCaches(); - if ($this->isEloquentDrivenNamespace($blueprint->namespace())) { + if (! $this->isEloquentDrivenNamespace($blueprint->namespace())) { return parent::delete($blueprint); } @@ -74,7 +74,7 @@ private function clearBlinkCaches() public function in(string $namespace) { - if ($this->isEloquentDrivenNamespace($namespace)) { + if (! $this->isEloquentDrivenNamespace($namespace)) { return parent::in($namespace); } @@ -219,8 +219,12 @@ private function updateOrderFromBlueprintSections($contents) return $contents; } - private function isEloquentDrivenNamespace(string $namespace) + private function isEloquentDrivenNamespace(?string $namespace) { + if (! $namespace) { + return true; + } + $eloquentNamespaces = config('statamic.eloquent-driver.namespaces', 'all'); if ($eloquentNamespaces !== 'all') { From 39714591db6443f3f9e165bc2e81643ab0153044 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 13 Jun 2024 07:40:27 +0200 Subject: [PATCH 3/5] Use correct config key --- src/Fields/BlueprintRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fields/BlueprintRepository.php b/src/Fields/BlueprintRepository.php index 8a83cbef..c6f45716 100644 --- a/src/Fields/BlueprintRepository.php +++ b/src/Fields/BlueprintRepository.php @@ -225,7 +225,7 @@ private function isEloquentDrivenNamespace(?string $namespace) return true; } - $eloquentNamespaces = config('statamic.eloquent-driver.namespaces', 'all'); + $eloquentNamespaces = config('statamic.eloquent-driver.blueprints.namespaces', 'all'); if ($eloquentNamespaces !== 'all') { if (! in_array($namespace, Arr::wrap($eloquentNamespaces))) { From 6309550f9ff097ced2c0297fe640a21722ebf82e Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 13 Jun 2024 07:41:59 +0200 Subject: [PATCH 4/5] Add some test coverage --- tests/Data/Fields/BlueprintTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Data/Fields/BlueprintTest.php b/tests/Data/Fields/BlueprintTest.php index 44953f6b..cdf366a2 100644 --- a/tests/Data/Fields/BlueprintTest.php +++ b/tests/Data/Fields/BlueprintTest.php @@ -3,6 +3,7 @@ namespace Tests\Data\Fields; use Illuminate\Foundation\Testing\RefreshDatabase; +use Statamic\Eloquent\Fields\BlueprintModel; use Statamic\Facades\Blueprint; use Tests\TestCase; @@ -70,4 +71,20 @@ public function it_deletes_the_model_when_the_blueprint_is_deleted() $this->assertNull($model); } + + /** @test */ + public function it_uses_file_based_namespaces() + { + config()->set('statamic.eloquent-driver.blueprints.namespaces', ['collections']); + + $this->assertCount(1, BlueprintModel::all()); + + $blueprint = Blueprint::make() + ->setNamespace('forms') + ->setHandle('test') + ->setHidden(true) + ->save(); + + $this->assertCount(1, BlueprintModel::all()); // we check theres no new database entries, ie its been handled by files + } } From 22a5dcae9a09cb92aa2ab5028b4197217f1cced0 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 13 Jun 2024 07:50:34 +0200 Subject: [PATCH 5/5] Ensure we set the file directory --- src/ServiceProvider.php | 8 ++++---- tests/Data/Fields/BlueprintTest.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 359021cd..122cab4c 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -275,10 +275,10 @@ private function registerBlueprints() return config('statamic.eloquent-driver.blueprints.fieldset_model'); }); - $this->app->singleton( - 'Statamic\Fields\BlueprintRepository', - 'Statamic\Eloquent\Fields\BlueprintRepository' - ); + $this->app->singleton(\Statamic\Fields\BlueprintRepository::class, function () { + return (new \Statamic\Eloquent\Fields\BlueprintRepository) + ->setDirectory(resource_path('blueprints')); + }); $this->app->singleton( 'Statamic\Fields\FieldsetRepository', diff --git a/tests/Data/Fields/BlueprintTest.php b/tests/Data/Fields/BlueprintTest.php index cdf366a2..beac000b 100644 --- a/tests/Data/Fields/BlueprintTest.php +++ b/tests/Data/Fields/BlueprintTest.php @@ -15,10 +15,10 @@ public function setUp(): void { parent::setUp(); - $this->app->singleton( - 'Statamic\Fields\BlueprintRepository', - 'Statamic\Eloquent\Fields\BlueprintRepository' - ); + $this->app->singleton(\Statamic\Fields\BlueprintRepository::class, function () { + return (new \Statamic\Eloquent\Fields\BlueprintRepository) + ->setDirectory(resource_path('blueprints')); + }); $this->app->singleton( 'Statamic\Fields\FieldsetRepository',