Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New storage core component #6867

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 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 @@ -230,15 +233,15 @@
$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
) {
$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;
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
8 changes: 4 additions & 4 deletions src/Cms/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
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
8 changes: 4 additions & 4 deletions src/Cms/PagePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Cms/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 4 additions & 4 deletions src/Cms/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Panel/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parsley/Parsley.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function mergeOrAppend(array $block): void
) {
$this->blocks[$lastIndex]['content']['text'] .= ' ' . $block['content']['text'];

// append
// append
} else {
$this->blocks[] = $block;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Toolkit/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,15 @@ 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 &&
is_array($merged[$key]) === true
) {
$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;
}
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