Skip to content

Commit

Permalink
New storage core component
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Dec 16, 2024
1 parent e352c68 commit dc24b41
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
13 changes: 13 additions & 0 deletions config/components.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
9 changes: 9 additions & 0 deletions src/Cms/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
3 changes: 1 addition & 2 deletions src/Cms/ModelWithContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/Cms/App/AppComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));
Expand Down

0 comments on commit dc24b41

Please sign in to comment.