From dc24b41fe0f867b3f71b79b2d687ac63bc1ae421 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 16 Dec 2024 11:47:36 +0100 Subject: [PATCH] New storage core component --- config/components.php | 13 ++++++++++++ src/Cms/App.php | 9 ++++++++ src/Cms/ModelWithContent.php | 3 +-- tests/Cms/App/AppComponentsTest.php | 33 +++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/config/components.php b/config/components.php index c149ba135e..4114cb18e7 100644 --- a/config/components.php +++ b/config/components.php @@ -4,8 +4,11 @@ use Kirby\Cms\Collection; use Kirby\Cms\File; use Kirby\Cms\FileVersion; +use Kirby\Cms\ModelWithContent; use Kirby\Cms\Page; use Kirby\Cms\User; +use Kirby\Content\PlainTextStorage; +use Kirby\Content\Storage; use Kirby\Data\Data; use Kirby\Email\PHPMailer as Emailer; use Kirby\Exception\NotFoundException; @@ -301,6 +304,16 @@ return Snippet::factory($name, $data, $slots); }, + /** + * Create a new storage object for the given model + */ + 'storage' => function ( + App $kirby, + ModelWithContent $model + ): Storage { + return new PlainTextStorage(model: $model); + }, + /** * Add your own template engine * diff --git a/src/Cms/App.php b/src/Cms/App.php index 7fc621ca62..88a9084211 100644 --- a/src/Cms/App.php +++ b/src/Cms/App.php @@ -4,6 +4,7 @@ use Closure; use Generator; +use Kirby\Content\Storage; use Kirby\Data\Data; use Kirby\Email\Email as BaseEmail; use Kirby\Exception\ErrorPageException; @@ -1616,6 +1617,14 @@ public function snippet( return null; } + /** + * Returns the default storage instance for a given Model + */ + public function storage(ModelWithContent $model): Storage + { + return $this->component('storage')($this, $model); + } + /** * System check class */ diff --git a/src/Cms/ModelWithContent.php b/src/Cms/ModelWithContent.php index 21b89815ad..87a532de35 100644 --- a/src/Cms/ModelWithContent.php +++ b/src/Cms/ModelWithContent.php @@ -6,7 +6,6 @@ use Kirby\Content\Content; use Kirby\Content\ContentTranslation; use Kirby\Content\Lock; -use Kirby\Content\PlainTextStorage; use Kirby\Content\Storage; use Kirby\Content\Version; use Kirby\Content\VersionId; @@ -547,7 +546,7 @@ public function site(): Site */ public function storage(): Storage { - return $this->storage ??= new PlainTextStorage(model: $this); + return $this->storage ??= $this->kirby()->storage($this); } /** diff --git a/tests/Cms/App/AppComponentsTest.php b/tests/Cms/App/AppComponentsTest.php index ed9038871a..db7de52797 100644 --- a/tests/Cms/App/AppComponentsTest.php +++ b/tests/Cms/App/AppComponentsTest.php @@ -3,6 +3,8 @@ namespace Kirby\Cms; use Kirby\Content\Field; +use Kirby\Content\MemoryStorage; +use Kirby\Content\PlainTextStorage; use Kirby\Email\Email; use Kirby\Exception\NotFoundException; use Kirby\Filesystem\F; @@ -346,6 +348,37 @@ public function testSnippet() $app->snippet('variable', ['message' => 'test'], false); } + public function testStorage() + { + $this->app = $this->app->clone([ + 'site' => [ + 'children' => [ + ['slug' => 'test'] + ] + ] + ]); + + $this->assertInstanceOf(PlainTextStorage::class, $this->app->storage($this->app->page('test'))); + } + + public function testStorageWithModifiedComponent() + { + $this->app = $this->app->clone([ + 'components' => [ + 'storage' => function (App $app, ModelWithContent $model) { + return new MemoryStorage($model); + } + ], + 'site' => [ + 'children' => [ + ['slug' => 'test'] + ] + ] + ]); + + $this->assertInstanceOf(MemoryStorage::class, $this->app->storage($this->app->page('test'))); + } + public function testTemplate() { $this->assertInstanceOf(Template::class, $this->app->template('default'));