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

App::models() #5337

Merged
merged 1 commit into from
Jul 2, 2023
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
25 changes: 25 additions & 0 deletions src/Cms/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\Cms;

use Closure;
use Generator;
use Kirby\Data\Data;
use Kirby\Email\Email as BaseEmail;
use Kirby\Exception\ErrorPageException;
Expand Down Expand Up @@ -959,6 +960,30 @@ public function markdown(string $text = null, array $options = null): string
return ($this->component('markdown'))($this, $text, $options);
}

/**
* Yields all models (site, pages, files and users) of this site
* @since 4.0.0
*
* @return \Generator|\Kirby\Cms\ModelWithContent[]
*/
public function models(): Generator
{
$site = $this->site();

yield from $site->files();
yield $site;

foreach ($site->index(true) as $page) {
yield from $page->files();
yield $page;
}

foreach ($this->users() as $user) {
yield from $user->files();
yield $user;
}
}

/**
* Check for a multilang setup
*/
Expand Down
83 changes: 7 additions & 76 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,55 +142,14 @@ public function code(): string
* Internal converter to create or remove
* translation files.
*/
protected static function converter(string $from, string $to): bool
protected static function converter(string $from, string $to): void
{
$kirby = App::instance();
$site = $kirby->site();

// convert site
foreach ($site->files() as $file) {
F::move(
$file->contentFile($from, true),
$file->contentFile($to, true)
);
}

F::move(
$site->contentFile($from, true),
$site->contentFile($to, true)
);

// convert all pages
foreach ($kirby->site()->index(true) as $page) {
foreach ($page->files() as $file) {
F::move(
$file->contentFile($from, true),
$file->contentFile($to, true)
);
}

foreach (App::instance()->models() as $model) {
F::move(
$page->contentFile($from, true),
$page->contentFile($to, true)
$model->contentFile($from, true),
$model->contentFile($to, true)
);
}

// convert all users
foreach ($kirby->users() as $user) {
foreach ($user->files() as $file) {
F::move(
$file->contentFile($from, true),
$file->contentFile($to, true)
);
}

F::move(
$user->contentFile($from, true),
$user->contentFile($to, true)
);
}

return true;
}

/**
Expand Down Expand Up @@ -272,7 +231,9 @@ public function delete(): bool
if ($this->isLast() === true) {
$this->converter($code, '');
} else {
$this->deleteContentFiles($code);
foreach ($kirby->models() as $model) {
F::remove($model->contentFile($code, true));
}
}

// get the original language collection and remove the current language
Expand All @@ -286,36 +247,6 @@ public function delete(): bool
return true;
}

/**
* When the language is deleted, all content files with
* the language code must be removed as well.
*/
protected function deleteContentFiles(mixed $code): bool
{
$kirby = App::instance();
$site = $kirby->site();

F::remove($site->contentFile($code, true));

foreach ($kirby->site()->index(true) as $page) {
foreach ($page->files() as $file) {
F::remove($file->contentFile($code, true));
}

F::remove($page->contentFile($code, true));
}

foreach ($kirby->users() as $user) {
foreach ($user->files() as $file) {
F::remove($file->contentFile($code, true));
}

F::remove($user->contentFile($code, true));
}

return true;
}

/**
* Reading direction of this language
*/
Expand Down
47 changes: 47 additions & 0 deletions tests/Cms/App/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,53 @@ public function testImage()
$this->assertNull($image);
}

/**
* @covers ::models
*/
public function testModels()
{
$app = new App([
'roots' => [
'index' => '/dev/null'
],
'site' => [
'files' => [
['filename' => 'sitefile.jpg']
],
'children' => [
[
'slug' => 'test',
'files' => [
['filename' => 'pagefile.jpg']
]
]
]
],
'users' => [
[
'email' => '[email protected]',
'files' => [
['filename' => 'userfile.jpg'],
]
]
]
]);

$models = $app->models();

$this->assertSame('sitefile.jpg', $models->current()->filename());
$models->next();
$this->assertInstanceOf(Site::class, $models->current());
$models->next();
$this->assertSame('pagefile.jpg', $models->current()->filename());
$models->next();
$this->assertSame('test', $models->current()->slug());
$models->next();
$this->assertSame('userfile.jpg', $models->current()->filename());
$models->next();
$this->assertSame('[email protected]', $models->current()->email());
}

/**
* @covers ::nonce
*/
Expand Down