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 8: New ModelWithContent::version method #6455

Merged
merged 17 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion src/Cms/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public function modified(
*/
protected function modifiedContent(string|null $languageCode = null): int
{
return $this->storage()->modified(VersionId::published(), $languageCode) ?? 0;
return $this->version(VersionId::published())->modified($languageCode ?? 'current') ?? 0;
}

/**
Expand Down
33 changes: 21 additions & 12 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\Version;
use Kirby\Content\VersionId;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
Expand Down Expand Up @@ -201,10 +202,7 @@ protected function convertTo(string $blueprint): static
$content = $this->content($code)->convertTo($blueprint);

// delete the old text file
$this->storage()->delete(
$identifier,
$code
);
$this->version($identifier)->delete($code);

// save to re-create the translation content file
// with the converted/updated content
Expand All @@ -228,7 +226,7 @@ protected function convertTo(string $blueprint): static
$content = $this->content()->convertTo($blueprint);

// delete the old text file
$this->storage()->delete($identifier, 'default');
$this->version($identifier)->delete('default');

return $new->save($content);
}
Expand Down Expand Up @@ -418,10 +416,7 @@ public function query(
public function readContent(string|null $languageCode = null): array
{
try {
return $this->storage()->read(
VersionId::default($this),
$languageCode
);
return $this->version()->read($languageCode ?? 'default');
} catch (NotFoundException) {
// only if the content file really does not exist, it's ok
// to return empty content. Otherwise this could lead to
Expand Down Expand Up @@ -730,6 +725,18 @@ public function uuid(): Uuid|null
return Uuid::for($this);
}

/**
* Returns a content version instance
* @since 5.0.0
*/
public function version(VersionId|string|null $versionId = null): Version
{
return new Version(
model: $this,
id: VersionId::from($versionId ?? VersionId::default($this))
);
}

/**
* Low level data writer method
* to store the given data on disk or anywhere else
Expand All @@ -738,14 +745,16 @@ public function uuid(): Uuid|null
public function writeContent(array $data, string|null $languageCode = null): bool
{
$data = $this->contentFileData($data, $languageCode);
$id = VersionId::default($this);

// update the default language, unless a specific language is passed
$languageCode ??= 'default';

try {
// we can only update if the version already exists
$this->storage()->update($id, $languageCode, $data);
$this->version()->update($data, $languageCode);
} catch (NotFoundException) {
// otherwise create a new version
$this->storage()->create($id, $languageCode, $data);
$this->version()->create($data, $languageCode);
}

return true;
Expand Down
6 changes: 2 additions & 4 deletions src/Cms/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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 @@ -830,9 +829,8 @@ public function modified(
string|null $handler = null,
string|null $languageCode = null
): int|string|false|null {
$modified = $this->storage()->modified(
VersionId::default($this),
$languageCode
$modified = $this->version()->modified(
$languageCode ?? 'current'
bastianallgeier marked this conversation as resolved.
Show resolved Hide resolved
);

if ($modified === null) {
Expand Down
5 changes: 1 addition & 4 deletions src/Cms/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ public function exposedFileUrl(string $folder): string|null

switch ($folder) {
case 'content':
return $url . '/' . basename($this->app->site()->storage()->contentFile(
VersionId::published(),
'default'
));
return $url . '/' . basename($this->app->site()->version(VersionId::published())->contentFile());
case 'git':
return $url . '/config';
case 'kirby':
Expand Down
7 changes: 2 additions & 5 deletions src/Cms/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,7 @@ public function email(): string|null
*/
public function exists(): bool
{
return $this->storage()->exists(
VersionId::published(),
'default'
);
return $this->version(VersionId::published())->exists('default');
}

/**
Expand Down Expand Up @@ -473,7 +470,7 @@ public function modified(
string|null $handler = null,
string|null $languageCode = null
): int|string|false {
$modifiedContent = $this->storage()->modified(VersionId::published(), $languageCode);
$modifiedContent = $this->version(VersionId::published())->modified($languageCode ?? 'current');
bastianallgeier marked this conversation as resolved.
Show resolved Hide resolved
$modifiedIndex = F::modified($this->root() . '/index.php');
$modifiedTotal = max([$modifiedContent, $modifiedIndex]);

Expand Down
5 changes: 1 addition & 4 deletions src/Content/ContentTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ public function content(): array
*/
public function contentFile(): string
{
return $this->contentFile = $this->parent->storage()->contentFile(
VersionId::default($this->parent),
$this->code
);
return $this->contentFile = $this->parent->version()->contentFile($this->code);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Content/VersionId.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ public static function default(ModelWithContent $model): static
/**
* Creates a VersionId instance from a simple string value
*/
public static function from(string $value): static
public static function from(VersionId|string $value): static
{
if ($value instanceof VersionId) {
return $value;
}

return new static($value);
}

Expand Down
36 changes: 18 additions & 18 deletions tests/Cms/Files/FileActionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,19 @@ public function testChangeName(File $file)
// create an empty dummy file
F::write($file->root(), '');
// ...and an empty content file for it
F::write($file->storage()->contentFile(VersionId::published(), 'default'), '');
F::write($file->version(VersionId::published())->contentFile('default'), '');

$this->assertFileExists($file->root());
$this->assertFileExists($file->storage()->contentFile(VersionId::published(), 'default'));
$this->assertFileExists($file->version(VersionId::published())->contentFile('default'));

$result = $file->changeName('test');

$this->assertNotSame($file->root(), $result->root());
$this->assertSame('test.csv', $result->filename());
$this->assertFileExists($result->root());
$this->assertFileExists($result->storage()->contentFile(VersionId::published(), 'default'));
$this->assertFileExists($result->version(VersionId::published())->contentFile('default'));
$this->assertFileDoesNotExist($file->root());
$this->assertFileDoesNotExist($file->storage()->contentFile(VersionId::published(), 'default'));
$this->assertFileDoesNotExist($file->version(VersionId::published())->contentFile('default'));
}

public static function fileProviderMultiLang(): array
Expand All @@ -140,20 +140,20 @@ public function testChangeNameMultiLang(File $file)
// create an empty dummy file
F::write($file->root(), '');
// ...and empty content files for it
F::write($file->storage()->contentFile(VersionId::published(), 'en'), '');
F::write($file->storage()->contentFile(VersionId::published(), 'de'), '');
F::write($file->version(VersionId::published())->contentFile('en'), '');
F::write($file->version(VersionId::published())->contentFile('de'), '');

$this->assertFileExists($file->root());
$this->assertFileExists($file->storage()->contentFile(VersionId::published(), 'en'));
$this->assertFileExists($file->storage()->contentFile(VersionId::published(), 'de'));
$this->assertFileExists($file->version(VersionId::published())->contentFile('en'));
$this->assertFileExists($file->version(VersionId::published())->contentFile('de'));

$result = $file->changeName('test');

$this->assertNotEquals($file->root(), $result->root());
$this->assertSame('test.csv', $result->filename());
$this->assertFileExists($result->root());
$this->assertFileExists($result->storage()->contentFile(VersionId::published(), 'en'));
$this->assertFileExists($result->storage()->contentFile(VersionId::published(), 'de'));
$this->assertFileExists($result->version(VersionId::published())->contentFile('en'));
$this->assertFileExists($result->version(VersionId::published())->contentFile('de'));
}

public function testChangeTemplate()
Expand Down Expand Up @@ -375,9 +375,9 @@ public function testChangeTemplateMultilang()
$this->assertNull($modified->caption()->value());
$this->assertSame('Das ist der Text', $modified->text()->value());

$this->assertFileExists($modified->storage()->contentFile(VersionId::published(), 'en'));
$this->assertFileExists($modified->storage()->contentFile(VersionId::published(), 'de'));
$this->assertFileDoesNotExist($modified->storage()->contentFile(VersionId::published(), 'fr'));
$this->assertFileExists($modified->version(VersionId::published())->contentFile('en'));
$this->assertFileExists($modified->version(VersionId::published())->contentFile('de'));
$this->assertFileDoesNotExist($modified->version(VersionId::published())->contentFile('fr'));
}

public function testChangeTemplateDefault()
Expand Down Expand Up @@ -753,17 +753,17 @@ public function testDelete(File $file)
// create an empty dummy file
F::write($file->root(), '');
// ...and an empty content file for it
F::write($file->storage()->contentFile(VersionId::published(), 'default'), '');
F::write($file->version(VersionId::published())->contentFile('default'), '');

$this->assertFileExists($file->root());
$this->assertFileExists($file->storage()->contentFile(VersionId::published(), 'default'));
$this->assertFileExists($file->version(VersionId::published())->contentFile('default'));

$result = $file->delete();

$this->assertTrue($result);

$this->assertFileDoesNotExist($file->root());
$this->assertFileDoesNotExist($file->storage()->contentFile(VersionId::published(), 'default'));
$this->assertFileDoesNotExist($file->version(VersionId::published())->contentFile('default'));
}

/**
Expand Down Expand Up @@ -873,11 +873,11 @@ public function testSave($file)
F::write($file->root(), '');

$this->assertFileExists($file->root());
$this->assertFileDoesNotExist($file->storage()->contentFile(VersionId::published(), 'default'));
$this->assertFileDoesNotExist($file->version(VersionId::published())->contentFile('default'));

$file = $file->clone(['content' => ['caption' => 'save']])->save();

$this->assertFileExists($file->storage()->contentFile(VersionId::published(), 'default'));
$this->assertFileExists($file->version(VersionId::published())->contentFile('default'));
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/Cms/Models/ModelWithContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Kirby\Cms;

use Closure;
use Kirby\Content\Version;
use Kirby\Content\VersionId;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Panel\Page as PanelPage;
use Kirby\Uuid\PageUuid;
Expand Down Expand Up @@ -443,4 +445,28 @@ public function testUuid()
$model = new Page(['slug' => 'foo']);
$this->assertInstanceOf(PageUuid::class, $model->uuid());
}

public function testVersion()
{
$model = new Site();
$this->assertInstanceOf(Version::class, $model->version('published'));
$this->assertSame('published', $model->version('published')->id()->value());
$this->assertSame('published', $model->version(VersionId::published())->id()->value());

$model = new Page(['slug' => 'foo']);
$this->assertInstanceOf(Version::class, $model->version('published'));
$this->assertSame('published', $model->version('published')->id()->value());
$this->assertSame('published', $model->version(VersionId::published())->id()->value());
}

public function testVersionFallback()
{
$model = new Page(['slug' => 'foo']);
$this->assertInstanceOf(Version::class, $model->version());
$this->assertSame('published', $model->version()->id()->value());

$model = new Page(['slug' => 'foo', 'isDraft' => true]);
$this->assertInstanceOf(Version::class, $model->version());
$this->assertSame('changes', $model->version()->id()->value());
}
}
20 changes: 10 additions & 10 deletions tests/Cms/Pages/PageActionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ public function testChangeTemplateMultilang()
$this->assertSame('article', $modified->intendedTemplate()->name());
$this->assertSame(2, $calls);

$this->assertFileExists($modified->storage()->contentFile(VersionId::published(), 'en'));
$this->assertFileExists($modified->storage()->contentFile(VersionId::published(), 'de'));
$this->assertFileDoesNotExist($modified->storage()->contentFile(VersionId::published(), 'fr'));
$this->assertFileExists($modified->version(VersionId::published())->contentFile('en'));
$this->assertFileExists($modified->version(VersionId::published())->contentFile('de'));
$this->assertFileDoesNotExist($modified->version(VersionId::published())->contentFile('fr'));
$this->assertNull($modified->caption()->value());
$this->assertSame('Text', $modified->text()->value());
$this->assertNull($modified->content('de')->get('caption')->value());
Expand Down Expand Up @@ -889,15 +889,15 @@ public function testDuplicateMultiLang()
'parent' => $page,
'code' => 'en',
]);
$this->assertFileExists($page->storage()->contentFile(VersionId::changes(), 'en'));
$this->assertFileExists($page->version(VersionId::changes())->contentFile('en'));

$drafts = $app->site()->drafts();
$childrenAndDrafts = $app->site()->childrenAndDrafts();

$copy = $page->duplicate('test-copy');

$this->assertFileExists($copy->storage()->contentFile(VersionId::changes(), 'en'));
$this->assertFileDoesNotExist($copy->storage()->contentFile(VersionId::changes(), 'de'));
$this->assertFileExists($copy->version(VersionId::changes())->contentFile('en'));
$this->assertFileDoesNotExist($copy->version(VersionId::changes())->contentFile('de'));

$this->assertIsPage($page, $drafts->find('test'));
$this->assertIsPage($page, $childrenAndDrafts->find('test'));
Expand Down Expand Up @@ -929,8 +929,8 @@ public function testDuplicateMultiLangSlug()
'slug' => 'test-de'
], 'de');

$this->assertFileExists($page->storage()->contentFile(VersionId::changes(), 'en'));
$this->assertFileExists($page->storage()->contentFile(VersionId::changes(), 'de'));
$this->assertFileExists($page->version(VersionId::changes())->contentFile('en'));
$this->assertFileExists($page->version(VersionId::changes())->contentFile('de'));
$this->assertSame('test', $page->slug());
$this->assertSame('test-de', $page->slug('de'));

Expand Down Expand Up @@ -1044,8 +1044,8 @@ public function testDuplicateChildrenMultiLang()

$copy = $page->duplicate('test-copy', ['children' => true]);

$this->assertFileExists($copy->storage()->contentFile(VersionId::changes(), 'en'));
$this->assertFileDoesNotExist($copy->storage()->contentFile(VersionId::changes(), 'de'));
$this->assertFileExists($copy->version(VersionId::changes())->contentFile('en'));
$this->assertFileDoesNotExist($copy->version(VersionId::changes())->contentFile('de'));


$this->assertNotSame($page->uuid()->id(), $copy->uuid()->id());
Expand Down
11 changes: 10 additions & 1 deletion tests/Content/VersionIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,19 @@ public function testDefault()
* @covers ::from
* @covers ::value
*/
public function testFrom()
public function testFromString()
{
$version = VersionId::from('published');
$this->assertSame('published', $version->value());
}

/**
* @covers ::from
* @covers ::value
*/
public function testFromInstance()
{
$version = VersionId::from(VersionId::published());
$this->assertSame('published', $version->value());
}

Expand Down
Loading