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

Changes 1: Refactor content storage to use new VersionId class #6436

Merged
merged 13 commits into from
May 21, 2024
3 changes: 2 additions & 1 deletion src/Cms/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use IntlDateFormatter;
use Kirby\Content\VersionId;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Filesystem\F;
use Kirby\Filesystem\IsFile;
Expand Down Expand Up @@ -425,7 +426,7 @@ public function modified(
*/
protected function modifiedContent(string|null $languageCode = null): int
{
return $this->storage()->modified('published', $languageCode) ?? 0;
return $this->storage()->modified(VersionId::published(), $languageCode) ?? 0;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Cms/ModelWithContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Kirby\Content\ContentStorage;
use Kirby\Content\ContentTranslation;
use Kirby\Content\PlainTextContentStorageHandler;
use Kirby\Content\VersionId;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Form\Form;
Expand Down Expand Up @@ -187,7 +188,7 @@ protected function convertTo(string $blueprint): static
$new = $this->clone(['template' => $blueprint]);

// temporary compatibility change (TODO: also convert changes)
$identifier = $this->storage()->defaultVersion();
$identifier = VersionId::default($this);

// for multilang, we go through all translations and
// covnert the content for each of them, remove the content file
Expand Down Expand Up @@ -418,7 +419,7 @@ public function readContent(string|null $languageCode = null): array
{
try {
return $this->storage()->read(
$this->storage()->defaultVersion(),
VersionId::default($this),
$languageCode
);
} catch (NotFoundException) {
Expand Down Expand Up @@ -737,7 +738,7 @@ public function uuid(): Uuid|null
public function writeContent(array $data, string|null $languageCode = null): bool
{
$data = $this->contentFileData($data, $languageCode);
$id = $this->storage()->defaultVersion();
$id = VersionId::default($this);

try {
// we can only update if the version already exists
Expand Down
5 changes: 2 additions & 3 deletions src/Cms/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Kirby\Content\Field;
use Kirby\Content\VersionId;
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
Expand Down Expand Up @@ -826,10 +827,8 @@ public function modified(
string|null $handler = null,
string|null $languageCode = null
): int|string|false|null {
$identifier = $this->isDraft() === true ? 'changes' : 'published';

$modified = $this->storage()->modified(
$identifier,
VersionId::default($this),
$languageCode
);

Expand Down
5 changes: 3 additions & 2 deletions src/Cms/PageActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\Cms;

use Closure;
use Kirby\Content\VersionId;
use Kirby\Exception\DuplicateException;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\LogicException;
Expand Down Expand Up @@ -471,8 +472,8 @@ public function copy(array $options = []): static
$ignore[] = $file->root();

// append all content files
array_push($ignore, ...$file->storage()->contentFiles('published'));
array_push($ignore, ...$file->storage()->contentFiles('changes'));
array_push($ignore, ...$file->storage()->contentFiles(VersionId::published()));
array_push($ignore, ...$file->storage()->contentFiles(VersionId::changes()));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Cms/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\Cms;

use Kirby\Cms\System\UpdateStatus;
use Kirby\Content\VersionId;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\PermissionException;
use Kirby\Filesystem\Dir;
Expand Down Expand Up @@ -76,7 +77,7 @@ public function exposedFileUrl(string $folder): string|null
switch ($folder) {
case 'content':
return $url . '/' . basename($this->app->site()->storage()->contentFile(
'published',
VersionId::published(),
'default'
));
case 'git':
Expand Down
5 changes: 3 additions & 2 deletions src/Cms/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Exception;
use Kirby\Content\Field;
use Kirby\Content\VersionId;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Exception\PermissionException;
Expand Down Expand Up @@ -204,7 +205,7 @@ public function email(): string|null
public function exists(): bool
{
return $this->storage()->exists(
'published',
VersionId::published(),
'default'
);
}
Expand Down Expand Up @@ -468,7 +469,7 @@ public function modified(
string|null $handler = null,
string|null $languageCode = null
): int|string|false {
$modifiedContent = $this->storage()->modified('published', $languageCode);
$modifiedContent = $this->storage()->modified(VersionId::published(), $languageCode);
$modifiedIndex = F::modified($this->root() . '/index.php');
$modifiedTotal = max([$modifiedContent, $modifiedIndex]);

Expand Down
74 changes: 29 additions & 45 deletions src/Content/ContentStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function __call(string $name, array $args): mixed
public function all(): Generator
{
foreach ($this->model->kirby()->languages()->codes() as $lang) {
foreach ($this->dynamicVersions() as $version) {
if ($this->exists($version, $lang) === true) {
yield $version => $lang;
foreach ($this->dynamicVersions() as $versionId) {
if ($this->exists($versionId, $lang) === true) {
yield $versionId => $lang;
}
}
}
Expand All @@ -71,12 +71,12 @@ public function all(): Generator
* @throws \Kirby\Exception\LogicException If the model type doesn't have a known content filename
*/
public function contentFile(
string $version,
VersionId $versionId,
string $lang,
bool $force = false
): string {
$lang = $this->language($lang, $force);
return $this->handler->contentFile($version, $lang);
return $this->handler->contentFile($versionId, $lang);
}

/**
Expand All @@ -88,8 +88,8 @@ public function convertLanguage(string $from, string $to): void
$from = $this->language($from, true);
$to = $this->language($to, true);

foreach ($this->dynamicVersions() as $version) {
$this->handler->move($version, $from, $version, $to);
foreach ($this->dynamicVersions() as $versionId) {
$this->handler->move($versionId, $from, $versionId, $to);
}
}

Expand All @@ -100,28 +100,12 @@ public function convertLanguage(string $from, string $to): void
* @param array<string, string> $fields Content fields
*/
public function create(
string $versionType,
VersionId $versionId,
string|null $lang,
array $fields
): void {
$lang = $this->language($lang);
$this->handler->create($versionType, $lang, $fields);
}

/**
* Returns the default version identifier for the model
* @internal
*/
public function defaultVersion(): string
{
if (
$this->model instanceof Page === true &&
$this->model->isDraft() === true
) {
return 'changes';
}

return 'published';
$this->handler->create($versionId, $lang, $fields);
}

/**
Expand All @@ -130,12 +114,12 @@ public function defaultVersion(): string
* @param string $lang Code `'default'` in a single-lang installation
*/
public function delete(
string $version,
VersionId $versionId,
string|null $lang = null,
bool $force = false
): void {
$lang = $this->language($lang, $force);
$this->handler->delete($version, $lang);
$this->handler->delete($versionId, $lang);
}

/**
Expand All @@ -157,13 +141,13 @@ public function deleteLanguage(string|null $lang): void
*/
public function dynamicVersions(): array
{
$versions = ['changes'];
$versions = [VersionId::changes()];

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

return $versions;
Expand All @@ -176,14 +160,14 @@ public function dynamicVersions(): array
* checks for "any language" if not provided
*/
public function exists(
string $version,
VersionId $versionId,
string|null $lang
): bool {
if ($lang !== null) {
$lang = $this->language($lang);
}

return $this->handler->exists($version, $lang);
return $this->handler->exists($versionId, $lang);
}

/**
Expand All @@ -193,11 +177,11 @@ public function exists(
* @param string $lang Code `'default'` in a single-lang installation
*/
public function modified(
string $version,
VersionId $versionId,
string|null $lang = null
): int|null {
$lang = $this->language($lang);
return $this->handler->modified($version, $lang);
return $this->handler->modified($versionId, $lang);
}

/**
Expand All @@ -209,12 +193,12 @@ public function modified(
* @throws \Kirby\Exception\NotFoundException If the version does not exist
*/
public function read(
string $version,
VersionId $versionId,
string|null $lang = null
): array {
$lang = $this->language($lang);
$this->ensureExistingVersion($version, $lang);
return $this->handler->read($version, $lang);
$this->ensureExistingVersion($versionId, $lang);
return $this->handler->read($versionId, $lang);
}

/**
Expand All @@ -225,12 +209,12 @@ public function read(
* @throws \Kirby\Exception\NotFoundException If the version does not exist
*/
public function touch(
string $version,
VersionId $versionId,
string|null $lang = null
): void {
$lang = $this->language($lang);
$this->ensureExistingVersion($version, $lang);
$this->handler->touch($version, $lang);
$this->ensureExistingVersion($versionId, $lang);
$this->handler->touch($versionId, $lang);
}

/**
Expand All @@ -257,24 +241,24 @@ public function touchLanguage(string|null $lang): void
* @throws \Kirby\Exception\NotFoundException If the version does not exist
*/
public function update(
string $version,
VersionId $versionId,
string|null $lang = null,
array $fields = []
): void {
$lang = $this->language($lang);
$this->ensureExistingVersion($version, $lang);
$this->handler->update($version, $lang, $fields);
$this->ensureExistingVersion($versionId, $lang);
$this->handler->update($versionId, $lang, $fields);
}

/**
* @throws \Kirby\Exception\NotFoundException If the version does not exist
*/
protected function ensureExistingVersion(
string $version,
VersionId $versionId,
string $lang
): void {
if ($this->exists($version, $lang) !== true) {
throw new NotFoundException('Version "' . $version . ' (' . $lang . ')" does not already exist');
if ($this->exists($versionId, $lang) !== true) {
throw new NotFoundException('Version "' . $versionId . ' (' . $lang . ')" does not already exist');
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/Content/ContentStorageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@ public function __construct(ModelWithContent $model);
* @param string $lang Code `'default'` in a single-lang installation
* @param array<string, string> $fields Content fields
*/
public function create(string $versionType, string $lang, array $fields): void;
public function create(VersionId $versionId, string $lang, array $fields): void;

/**
* Deletes an existing version in an idempotent way if it was already deleted
*
* @param string $lang Code `'default'` in a single-lang installation
*/
public function delete(string $version, string $lang): void;
public function delete(VersionId $versionId, string $lang): void;

/**
* Checks if a version exists
*
* @param string|null $lang Code `'default'` in a single-lang installation;
* checks for "any language" if not provided
*/
public function exists(string $version, string|null $lang): bool;
public function exists(VersionId $versionId, string|null $lang): bool;

/**
* Returns the modification timestamp of a version if it exists
*
* @param string $lang Code `'default'` in a single-lang installation
*/
public function modified(string $version, string $lang): int|null;
public function modified(VersionId $versionId, string $lang): int|null;

/**
* Moves content from one version-language combination to another
Expand All @@ -59,9 +59,9 @@ public function modified(string $version, string $lang): int|null;
* @param string $toLang Code `'default'` in a single-lang installation
*/
public function move(
string $fromVersion,
VersionId $fromVersionId,
string $fromLang,
string $toVersion,
VersionId $toVersionId,
string $toLang
): void;

Expand All @@ -73,7 +73,7 @@ public function move(
*
* @throws \Kirby\Exception\NotFoundException If the version does not exist
*/
public function read(string $version, string $lang): array;
public function read(VersionId $versionId, string $lang): array;

/**
* Updates the modification timestamp of an existing version
Expand All @@ -82,7 +82,7 @@ public function read(string $version, string $lang): array;
*
* @throws \Kirby\Exception\NotFoundException If the version does not exist
*/
public function touch(string $version, string $lang): void;
public function touch(VersionId $versionId, string $lang): void;

/**
* Updates the content fields of an existing version
Expand All @@ -92,5 +92,5 @@ public function touch(string $version, string $lang): void;
*
* @throws \Kirby\Exception\NotFoundException If the version does not exist
*/
public function update(string $version, string $lang, array $fields): void;
public function update(VersionId $versionId, string $lang, array $fields): void;
}
Loading
Loading