From dc24b41fe0f867b3f71b79b2d687ac63bc1ae421 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 16 Dec 2024 11:47:36 +0100 Subject: [PATCH 1/2] 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')); From 134b04e50af65251179c710adb1ab91dd4069c57 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 16 Dec 2024 11:55:04 +0100 Subject: [PATCH 2/2] Fix CS issues --- config/components.php | 4 ++-- src/Cms/Files.php | 8 ++++---- src/Cms/PagePicker.php | 8 ++++---- src/Cms/Pages.php | 8 ++++---- src/Cms/Users.php | 8 ++++---- src/Http/Environment.php | 4 ++-- src/Panel/Panel.php | 2 +- src/Parsley/Parsley.php | 2 +- src/Toolkit/A.php | 4 ++-- 9 files changed, 24 insertions(+), 24 deletions(-) diff --git a/config/components.php b/config/components.php index 4114cb18e7..2b316ddf82 100644 --- a/config/components.php +++ b/config/components.php @@ -233,7 +233,7 @@ $scoring['score'] += 16 * $score; $scoring['hits'] += 1; - // check for exact beginning matches + // check for exact beginning matches } elseif ( $options['words'] === false && Str::startsWith($lowerValue, $query) === true @@ -241,7 +241,7 @@ $scoring['score'] += 8 * $score; $scoring['hits'] += 1; - // check for exact query matches + // check for exact query matches } elseif ($matches = preg_match_all('!' . $exact . '!ui', $value, $r)) { $scoring['score'] += 2 * $score; $scoring['hits'] += $matches; diff --git a/src/Cms/Files.php b/src/Cms/Files.php index d75b1f26dd..f935bf9303 100644 --- a/src/Cms/Files.php +++ b/src/Cms/Files.php @@ -51,19 +51,19 @@ public function add($object): static if ($object instanceof self) { $this->data = [...$this->data, ...$object->data]; - // add a file by id + // add a file by id } elseif ( is_string($object) === true && $file = App::instance()->file($object) ) { $this->__set($file->id(), $file); - // add a file object + // add a file object } elseif ($object instanceof File) { $this->__set($object->id(), $object); - // give a useful error message on invalid input; - // silently ignore "empty" values for compatibility with existing setups + // give a useful error message on invalid input; + // silently ignore "empty" values for compatibility with existing setups } elseif (in_array($object, [null, false, true], true) !== true) { throw new InvalidArgumentException( message: 'You must pass a Files or File object or an ID of an existing file to the Files collection' diff --git a/src/Cms/PagePicker.php b/src/Cms/PagePicker.php index c0d40de3ab..7c4dda8fac 100644 --- a/src/Cms/PagePicker.php +++ b/src/Cms/PagePicker.php @@ -127,13 +127,13 @@ public function items(): Pages|null if (empty($this->options['query']) === true) { $items = $this->itemsForParent(); - // when subpage navigation is enabled, a parent - // might be passed in addition to the query. - // The parent then takes priority. + // when subpage navigation is enabled, a parent + // might be passed in addition to the query. + // The parent then takes priority. } elseif ($this->options['subpages'] === true && empty($this->options['parent']) === false) { $items = $this->itemsForParent(); - // search by query + // search by query } else { $items = $this->itemsForQuery(); } diff --git a/src/Cms/Pages.php b/src/Cms/Pages.php index e24871854e..4443e431ac 100644 --- a/src/Cms/Pages.php +++ b/src/Cms/Pages.php @@ -64,19 +64,19 @@ public function add($object): static if ($object instanceof self) { $this->data = [...$this->data, ...$object->data]; - // add a page by id + // add a page by id } elseif ( is_string($object) === true && $page = $site->find($object) ) { $this->__set($page->id(), $page); - // add a page object + // add a page object } elseif ($object instanceof Page) { $this->__set($object->id(), $object); - // give a useful error message on invalid input; - // silently ignore "empty" values for compatibility with existing setups + // give a useful error message on invalid input; + // silently ignore "empty" values for compatibility with existing setups } elseif (in_array($object, [null, false, true], true) !== true) { throw new InvalidArgumentException( message: 'You must pass a Pages or Page object or an ID of an existing page to the Pages collection' diff --git a/src/Cms/Users.php b/src/Cms/Users.php index 08d9e93dd2..c67d1fe11f 100644 --- a/src/Cms/Users.php +++ b/src/Cms/Users.php @@ -51,19 +51,19 @@ public function add($object): static if ($object instanceof self) { $this->data = [...$this->data, ...$object->data]; - // add a user by id + // add a user by id } elseif ( is_string($object) === true && $user = App::instance()->user($object) ) { $this->__set($user->id(), $user); - // add a user object + // add a user object } elseif ($object instanceof User) { $this->__set($object->id(), $object); - // give a useful error message on invalid input; - // silently ignore "empty" values for compatibility with existing setups + // give a useful error message on invalid input; + // silently ignore "empty" values for compatibility with existing setups } elseif (in_array($object, [null, false, true], true) !== true) { throw new InvalidArgumentException( message: 'You must pass a Users or User object or an ID of an existing user to the Users collection' diff --git a/src/Http/Environment.php b/src/Http/Environment.php index 6509ffd71b..2339cb5aca 100644 --- a/src/Http/Environment.php +++ b/src/Http/Environment.php @@ -178,11 +178,11 @@ public function detect( if ($options['allowed'] === '*' || $options['allowed'] === ['*']) { $this->detectAuto(true); - // fixed environments + // fixed environments } elseif (empty($options['allowed']) === false) { $this->detectAllowed($options['allowed']); - // secure auto-detection + // secure auto-detection } else { $this->detectAuto(); } diff --git a/src/Panel/Panel.php b/src/Panel/Panel.php index de979365c7..40c10b86d2 100644 --- a/src/Panel/Panel.php +++ b/src/Panel/Panel.php @@ -274,7 +274,7 @@ public static function response($result, array $options = []): Response message: 'The data could not be found' ); - // interpret strings as errors + // interpret strings as errors } elseif (is_string($result) === true) { $result = new Exception($result); } diff --git a/src/Parsley/Parsley.php b/src/Parsley/Parsley.php index a819ac41f9..1ddb7d4604 100644 --- a/src/Parsley/Parsley.php +++ b/src/Parsley/Parsley.php @@ -214,7 +214,7 @@ public function mergeOrAppend(array $block): void ) { $this->blocks[$lastIndex]['content']['text'] .= ' ' . $block['content']['text']; - // append + // append } else { $this->blocks[] = $block; } diff --git a/src/Toolkit/A.php b/src/Toolkit/A.php index be9053e84f..b69c071d87 100644 --- a/src/Toolkit/A.php +++ b/src/Toolkit/A.php @@ -510,7 +510,7 @@ public static function merge(array|int ...$arrays): array ) { $merged[] = $value; - // recursively merge the two array values + // recursively merge the two array values } elseif ( is_array($value) === true && isset($merged[$key]) === true && @@ -518,7 +518,7 @@ public static function merge(array|int ...$arrays): array ) { $merged[$key] = static::merge($merged[$key], $value, $mode); - // simply overwrite with the value from the second array + // simply overwrite with the value from the second array } else { $merged[$key] = $value; }