From 9b1e7a4403d27d2e00d61128811b983a469060e0 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Fri, 30 Jun 2023 18:30:05 +0200 Subject: [PATCH] `App::models()` --- src/Cms/App.php | 25 ++++++++++++ src/Cms/Language.php | 83 ++++----------------------------------- tests/Cms/App/AppTest.php | 47 ++++++++++++++++++++++ 3 files changed, 79 insertions(+), 76 deletions(-) diff --git a/src/Cms/App.php b/src/Cms/App.php index a81d480b64..e29e849368 100644 --- a/src/Cms/App.php +++ b/src/Cms/App.php @@ -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; @@ -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 */ diff --git a/src/Cms/Language.php b/src/Cms/Language.php index 3dcf7e9005..b26fd1b3b0 100644 --- a/src/Cms/Language.php +++ b/src/Cms/Language.php @@ -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; } /** @@ -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 @@ -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 */ diff --git a/tests/Cms/App/AppTest.php b/tests/Cms/App/AppTest.php index 895cdef298..f6ed8b4926 100644 --- a/tests/Cms/App/AppTest.php +++ b/tests/Cms/App/AppTest.php @@ -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' => 'test@getkirby.com', + '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('test@getkirby.com', $models->current()->email()); + } + /** * @covers ::nonce */