Skip to content

Commit

Permalink
New Version::save method to simplify various parts of the Model
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Jun 7, 2024
1 parent 345d277 commit 54a615f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 18 deletions.
50 changes: 48 additions & 2 deletions src/Content/ContentStorageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Kirby\Cms\ModelWithContent;
use Kirby\Cms\Page;
use Kirby\Exception\NotFoundException;
use Kirby\Uuid\Uuids;

/**
* Abstract for content storage handlers;
Expand Down Expand Up @@ -53,7 +54,15 @@ public function all(): Generator
*/
public function create(VersionId $versionId, Language $language, array $fields): void
{
$this->write($versionId, $language, $fields);
$this->write(
versionId: $versionId,
language: $language,
fields: $this->normalizeFields(
versionId: $versionId,
language: $language,
fields: $fields
)
);
}

/**
Expand Down Expand Up @@ -154,6 +163,35 @@ public function moveLanguage(Language $fromLanguage, Language $toLanguage): void
}
}

/**
* Remove all fields that should not be stored
* in the given version or for the given language
*/
protected function normalizeFields(
VersionId $versionId,
Language $language,
array $fields
): array {
// keep all fields for the default language
if ($language->isDefault() === true) {
return $fields;
}

// remove all fields that should not be translatable
foreach ($this->model->blueprint()->fields() as $field) {
if (($field['translate'] ?? true) === false) {
$fields[strtolower($field['name'])] = null;
}
}

// remove UUID for non-default languages
if (Uuids::enabled() === true && isset($fields['uuid']) === true) {
$fields['uuid'] = null;
}

return $fields;
}

/**
* Returns the stored content fields
*
Expand Down Expand Up @@ -193,7 +231,15 @@ public function touchLanguage(Language $language): void
public function update(VersionId $versionId, Language $language, array $fields): void
{
$this->ensure($versionId, $language);
$this->write($versionId, $language, $fields);
$this->write(
versionId: $versionId,
language: $language,
fields: $this->normalizeFields(
versionId: $versionId,
language: $language,
fields: $fields
)
);
}

/**
Expand Down
56 changes: 55 additions & 1 deletion src/Content/LabPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,49 @@ public function content(string|null $languageCode = null): Content
return $this->version()->content($languageCode ?? 'current');
}

/**
* @deprecated since 5.0.0 Use `::version()->read()` instead
*/
public function readContent(string|null $languageCode = null): array
{
return $this->version()->read($languageCode ?? 'current');
}

/**
* @deprecated since 5.0.0 Use `::version()->save()` instead
*/
public function save(
array|null $data = null,
string|null $languageCode = null,
bool $overwrite = false
): static {
$this->version()->save($data, $languageCode ?? 'current', $overwrite);
return $this;
}

/**
* @deprecated since 5.0.0 Use `::version()->save()` instead
*/
protected function saveContent(
array|null $data = null,
bool $overwrite = false
): static {
$this->version()->save($data, 'current', $overwrite);
return $this;
}

/**
* @deprecated since 5.0.0 Use `::version()->save()` instead
*/
protected function saveTranslation(
array|null $data = null,
string|null $languageCode = null,
bool $overwrite = false
): static {
$this->version()->save($data, $languageCode ?? 'current', $overwrite);
return $this;
}

/**
* Sets the content when initializing a model manually.
* This will switch to the in memory storage to keep the
Expand All @@ -43,13 +86,15 @@ protected function setContent(array|null $content = null): static
model: $this,
version: $this->version(),
language: Language::ensure('default'),
fields: $content,
fields: $content
);

return $this;
}

/**
* Stores in-memory translations for the model if they
* are passed to the constructor with the translations prop.
*/
protected function setTranslations(array|null $translations = null): static
{
Expand Down Expand Up @@ -92,4 +137,13 @@ public function translations(): Translations
version: $this->version()
);
}

/**
* @deprecated since 5.0.0 Use `::version()->save()` instead
*/
public function writeContent(array $data, string|null $languageCode = null): bool
{
$this->version()->save($data, $languageCode ?? 'default', true);
return true;
}
}
2 changes: 1 addition & 1 deletion src/Content/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function create(
$fields['slug'] = $slug;
}

$version->create($fields, $language->code());
$version->save($fields, $language->code(), true);

return new static(
model: $model,
Expand Down
58 changes: 44 additions & 14 deletions src/Content/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\Content;

use Kirby\Cms\Language;
use Kirby\Cms\Languages;
use Kirby\Cms\ModelWithContent;
use Throwable;

Expand Down Expand Up @@ -32,15 +33,9 @@ public function __construct(
*/
public function content(string $language = 'default'): Content
{
try {
$data = $this->model->storage()->read($this->id, $this->language($language));
} catch (Throwable) {
$data = [];
}

return new Content(
parent: $this->model,
data: $data,
data: $this->read($language),
);
}

Expand Down Expand Up @@ -71,13 +66,8 @@ public function create(array $fields, string $language = 'default'): void
*/
public function delete(): void
{
// delete the default language in single-language mode
if ($this->model->kirby()->multilang() === false) {
$this->model->storage()->delete($this->id, $this->language('default'));
}

// delete all languages
foreach ($this->model->kirby()->languages() as $language) {
foreach (Languages::ensure() as $language) {
$this->model->storage()->delete($this->id, $language);
}
}
Expand Down Expand Up @@ -162,9 +152,43 @@ public function move(string $fromLanguage, VersionId $toVersionId, string $toLan
* @return array<string, string>
*/
public function read(string $language = 'default'): array
{
try {
return $this->model->storage()->read($this->id, $this->language($language));
} catch (Throwable) {
return [];
}
}

/**
* Replaces the content of the current version with the given fields
*
* @param array<string, string> $fields Content fields
*
* @throws \Kirby\Exception\NotFoundException If the version does not exist
*/
public function replace(array $fields, string $language = 'default'): void
{
$this->ensure($language);
return $this->model->storage()->read($this->id, $this->language($language));
$this->model->storage()->update($this->id, $this->language($language), $fields);
}

/**
* Save will either try to create, update or replace the current version
*/
public function save(
array $fields,
string $language = 'default',
bool $overwrite = false
): void {
match (true) {
$this->exists($language) === false
=> $this->create($fields, $language),
$overwrite
=> $this->replace($fields, $language),
default
=> $this->update($fields, $language)
};
}

/**
Expand Down Expand Up @@ -215,7 +239,13 @@ public function touchLanguage(string $language = 'default'): void
*/
public function update(array $fields, string $language = 'default'): void
{
// make sure the version exists before it can be updated
$this->ensure($language);

// merge the previous state with the new state to always
// update to a complete version
$fields = [...$this->read($language), ...$fields];

$this->model->storage()->update($this->id, $this->language($language), $fields);
}
}

0 comments on commit 54a615f

Please sign in to comment.