Skip to content

Commit

Permalink
Merge pull request #6454 from getkirby/v5/changes/move-content-storag…
Browse files Browse the repository at this point in the history
…e-methods
  • Loading branch information
lukasbestle authored Jun 16, 2024
2 parents 88ae19d + 2a7d3fb commit f9df95a
Show file tree
Hide file tree
Showing 6 changed files with 575 additions and 122 deletions.
80 changes: 80 additions & 0 deletions src/Content/ContentStorageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Kirby\Content;

use Generator;
use Kirby\Cms\Language;
use Kirby\Cms\ModelWithContent;
use Kirby\Cms\Page;

/**
* Abstract for content storage handlers;
Expand All @@ -25,6 +27,26 @@ public function __construct(protected ModelWithContent $model)
{
}

/**
* Returns generator for all existing version-language combinations
* @todo 5.0.0 Consider more descriptive name and maybe move to a different class
*
* @return Generator<\Kirby\Content\VersionId, \Kirby\Cms\Language>
*/
public function all(): Generator
{
$kirby = $this->model->kirby();
$languages = $kirby->multilang() === false ? [Language::single()] : $kirby->languages();

foreach ($languages as $language) {
foreach ($this->dynamicVersions() as $versionId) {
if ($this->exists($versionId, $language) === true) {
yield $versionId => $language;
}
}
}
}

/**
* Creates a new version
*
Expand All @@ -37,6 +59,38 @@ abstract public function create(VersionId $versionId, Language $language, array
*/
abstract public function delete(VersionId $versionId, Language $language): void;

/**
* Deletes all versions when deleting a language
* @internal
* @todo Move to `Language` class
*/
public function deleteLanguage(Language $language): void
{
foreach ($this->dynamicVersions() as $version) {
$this->delete($version, $language);
}
}

/**
* Returns all versions available for the model that can be updated
* @internal
* @todo We might want to move this directly to the models later or work
* with a `Versions` class
*/
public function dynamicVersions(): array
{
$versions = [VersionId::changes()];

if (
$this->model instanceof Page === false ||
$this->model->isDraft() === false
) {
$versions[] = VersionId::published();
}

return $versions;
}

/**
* Checks if a version exists
*/
Expand All @@ -57,6 +111,18 @@ abstract public function move(
Language $toLanguage
): void;

/**
* Adapts all versions when converting languages
* @internal
* @todo Move to `Language` class
*/
public function moveLanguage(Language $fromLanguage, Language $toLanguage): void
{
foreach ($this->dynamicVersions() as $versionId) {
$this->move($versionId, $fromLanguage, $versionId, $toLanguage);
}
}

/**
* Returns the stored content fields
*
Expand All @@ -73,6 +139,20 @@ abstract public function read(VersionId $versionId, Language $language): array;
*/
abstract public function touch(VersionId $versionId, Language $language): void;

/**
* Touches all versions of a language
* @internal
* @todo Move to `Language` class
*/
public function touchLanguage(Language $language): void
{
foreach ($this->dynamicVersions() as $version) {
if ($this->exists($version, $language) === true) {
$this->touch($version, $language);
}
}
}

/**
* Updates the content fields of an existing version
*
Expand Down
Loading

0 comments on commit f9df95a

Please sign in to comment.