Upgrade to 4.0.0

This commit is contained in:
Bastian Allgeier
2023-11-28 09:33:56 +01:00
parent f96b96af76
commit 3b0b6546ca
480 changed files with 21371 additions and 13327 deletions

View File

@@ -4,7 +4,8 @@ namespace Kirby\Cms;
use Kirby\Data\Data;
use Kirby\Exception\Exception;
use Kirby\Exception\PermissionException;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\LogicException;
use Kirby\Filesystem\F;
use Kirby\Toolkit\Locale;
use Kirby\Toolkit\Str;
@@ -26,80 +27,54 @@ use Throwable;
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
class Language extends Model
class Language
{
/**
* @var string
*/
protected $code;
use HasSiblings;
/**
* @var bool
* The parent Kirby instance
*/
protected $default;
public static App|null $kirby;
/**
* @var string
*/
protected $direction;
/**
* @var array
*/
protected $locale;
/**
* @var string
*/
protected $name;
/**
* @var array|null
*/
protected $slugs;
/**
* @var array|null
*/
protected $smartypants;
/**
* @var array|null
*/
protected $translations;
/**
* @var string
*/
protected $url;
protected string $code;
protected bool $default;
protected string $direction;
protected array $locale;
protected string $name;
protected array $slugs;
protected array $smartypants;
protected array $translations;
protected string|null $url;
/**
* Creates a new language object
*
* @param array $props
*/
public function __construct(array $props)
{
$this->setRequiredProperties($props, [
'code'
]);
if (isset($props['code']) === false) {
throw new InvalidArgumentException('The property "code" is required');
}
$this->setOptionalProperties($props, [
'default',
'direction',
'locale',
'name',
'slugs',
'smartypants',
'translations',
'url',
]);
static::$kirby = $props['kirby'] ?? null;
$this->code = trim($props['code']);
$this->default = ($props['default'] ?? false) === true;
$this->direction = ($props['direction'] ?? null) === 'rtl' ? 'rtl' : 'ltr';
$this->name = trim($props['name'] ?? $this->code);
$this->slugs = $props['slugs'] ?? [];
$this->smartypants = $props['smartypants'] ?? [];
$this->translations = $props['translations'] ?? [];
$this->url = $props['url'] ?? null;
if ($locale = $props['locale'] ?? null) {
$this->locale = Locale::normalize($locale);
} else {
$this->locale = [LC_ALL => $this->code];
}
}
/**
* Improved `var_dump` output
*
* @return array
* @codeCoverageIgnore
*/
public function __debugInfo(): array
{
@@ -109,8 +84,6 @@ class Language extends Model
/**
* Returns the language code
* when the language is converted to a string
*
* @return string
*/
public function __toString(): string
{
@@ -120,8 +93,6 @@ class Language extends Model
/**
* Returns the base Url for the language
* without the path or other cruft
*
* @return string
*/
public function baseUrl(): string
{
@@ -139,67 +110,40 @@ class Language extends Model
return Url::base($languageUrl) ?? $kirbyUrl;
}
/**
* Creates an instance with the same
* initial properties.
*/
public function clone(array $props = []): static
{
return new static(array_replace_recursive([
'code' => $this->code,
'default' => $this->default,
'direction' => $this->direction,
'locale' => $this->locale,
'name' => $this->name,
'slugs' => $this->slugs,
'smartypants' => $this->smartypants,
'translations' => $this->translations,
'url' => $this->url,
], $props));
}
/**
* Returns the language code/id.
* The language code is used in
* text file names as appendix.
*
* @return string
*/
public function code(): string
{
return $this->code;
}
/**
* Internal converter to create or remove
* translation files.
*
* @param string $from
* @param string $to
* @return bool
*/
protected static function converter(string $from, string $to): bool
{
$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));
}
F::move($page->contentFile($from, true), $page->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;
}
/**
* Creates a new language object
*
* @internal
* @param array $props
* @return static
*/
public static function create(array $props)
public static function create(array $props): static
{
$props['code'] = Str::slug($props['code'] ?? null);
$kirby = App::instance();
@@ -227,7 +171,12 @@ class Language extends Model
$language->save();
if ($languages->count() === 0) {
static::converter('', $language->code());
foreach ($kirby->models() as $model) {
$model->storage()->convertLanguage(
'default',
$language->code()
);
}
}
// update the main languages collection in the app instance
@@ -248,17 +197,18 @@ class Language extends Model
/**
* Delete the current language and
* all its translation files
*
* @internal
* @return bool
*
* @throws \Kirby\Exception\Exception
*/
public function delete(): bool
{
$kirby = App::instance();
$languages = $kirby->languages();
$code = $this->code();
$isLast = $languages->count() === 1;
$kirby = App::instance();
$code = $this->code();
if ($this->isDeletable() === false) {
throw new Exception('The language cannot be deleted');
}
// trigger before hook
$kirby->trigger('language.delete:before', [
@@ -269,10 +219,12 @@ class Language extends Model
throw new Exception('The language could not be deleted');
}
if ($isLast === true) {
$this->converter($code, '');
} else {
$this->deleteContentFiles($code);
foreach ($kirby->models() as $model) {
if ($this->isLast() === true) {
$model->storage()->convertLanguage($code, 'default');
} else {
$model->storage()->deleteLanguage($code);
}
}
// get the original language collection and remove the current language
@@ -286,43 +238,8 @@ class Language extends Model
return true;
}
/**
* When the language is deleted, all content files with
* the language code must be removed as well.
*
* @param mixed $code
* @return bool
*/
protected function deleteContentFiles($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
*
* @return string
*/
public function direction(): string
{
@@ -331,8 +248,6 @@ class Language extends Model
/**
* Check if the language file exists
*
* @return bool
*/
public function exists(): bool
{
@@ -342,19 +257,36 @@ class Language extends Model
/**
* Checks if this is the default language
* for the site.
*
* @return bool
*/
public function isDefault(): bool
{
return $this->default;
}
/**
* Checks if the language can be deleted
*/
public function isDeletable(): bool
{
// the default language can only be deleted if it's the last
if ($this->isDefault() === true && $this->isLast() === false) {
return false;
}
return true;
}
/**
* Checks if this is the last language
*/
public function isLast(): bool
{
return App::instance()->languages()->count() === 1;
}
/**
* The id is required for collections
* to work properly. The code is used as id
*
* @return string
*/
public function id(): string
{
@@ -362,11 +294,17 @@ class Language extends Model
}
/**
* Loads the language rules for provided locale code
*
* @param string $code
* Returns the parent Kirby instance
*/
public static function loadRules(string $code)
public function kirby(): App
{
return static::$kirby ??= App::instance();
}
/**
* Loads the language rules for provided locale code
*/
public static function loadRules(string $code): array
{
$kirby = App::instance();
$code = Str::contains($code, '.') ? Str::before($code, '.') : $code;
@@ -387,9 +325,8 @@ class Language extends Model
* Returns the PHP locale setting array
*
* @param int $category If passed, returns the locale for the specified category (e.g. LC_ALL) as string
* @return array|string
*/
public function locale(int $category = null)
public function locale(int $category = null): array|string|null
{
if ($category !== null) {
return $this->locale[$category] ?? $this->locale[LC_ALL] ?? null;
@@ -401,8 +338,6 @@ class Language extends Model
/**
* Returns the human-readable name
* of the language
*
* @return string
*/
public function name(): string
{
@@ -411,8 +346,6 @@ class Language extends Model
/**
* Returns the URL path for the language
*
* @return string
*/
public function path(): string
{
@@ -425,8 +358,6 @@ class Language extends Model
/**
* Returns the routing pattern for the language
*
* @return string
*/
public function pattern(): string
{
@@ -441,8 +372,6 @@ class Language extends Model
/**
* Returns the absolute path to the language file
*
* @return string
*/
public function root(): string
{
@@ -453,19 +382,15 @@ class Language extends Model
* Returns the LanguageRouter instance
* which is used to handle language specific
* routes.
*
* @return \Kirby\Cms\LanguageRouter
*/
public function router()
public function router(): LanguageRouter
{
return new LanguageRouter($this);
}
/**
* Get slug rules for language
*
* @internal
* @return array
*/
public function rules(): array
{
@@ -476,11 +401,11 @@ class Language extends Model
/**
* Saves the language settings in the languages folder
*
* @internal
*
* @return $this
*/
public function save()
public function save(): static
{
try {
$existingData = Data::read($this->root());
@@ -508,104 +433,15 @@ class Language extends Model
}
/**
* @param string $code
* @return $this
* Private siblings collector
*/
protected function setCode(string $code)
protected function siblingsCollection(): Collection
{
$this->code = trim($code);
return $this;
}
/**
* @param bool $default
* @return $this
*/
protected function setDefault(bool $default = false)
{
$this->default = $default;
return $this;
}
/**
* @param string $direction
* @return $this
*/
protected function setDirection(string $direction = 'ltr')
{
$this->direction = $direction === 'rtl' ? 'rtl' : 'ltr';
return $this;
}
/**
* @param string|array $locale
* @return $this
*/
protected function setLocale($locale = null)
{
if ($locale === null) {
$this->locale = [LC_ALL => $this->code];
} else {
$this->locale = Locale::normalize($locale);
}
return $this;
}
/**
* @param string $name
* @return $this
*/
protected function setName(string $name = null)
{
$this->name = trim($name ?? $this->code);
return $this;
}
/**
* @param array $slugs
* @return $this
*/
protected function setSlugs(array $slugs = null)
{
$this->slugs = $slugs ?? [];
return $this;
}
/**
* @param array $smartypants
* @return $this
*/
protected function setSmartypants(array $smartypants = null)
{
$this->smartypants = $smartypants ?? [];
return $this;
}
/**
* @param array $translations
* @return $this
*/
protected function setTranslations(array $translations = null)
{
$this->translations = $translations ?? [];
return $this;
}
/**
* @param string $url
* @return $this
*/
protected function setUrl(string $url = null)
{
$this->url = $url;
return $this;
return App::instance()->languages();
}
/**
* Returns the custom slug rules for this language
*
* @return array
*/
public function slugs(): array
{
@@ -614,8 +450,6 @@ class Language extends Model
/**
* Returns the custom SmartyPants options for this language
*
* @return array
*/
public function smartypants(): array
{
@@ -625,8 +459,6 @@ class Language extends Model
/**
* Returns the most important
* properties as array
*
* @return array
*/
public function toArray(): array
{
@@ -643,8 +475,6 @@ class Language extends Model
/**
* Returns the translation strings for this language
*
* @return array
*/
public function translations(): array
{
@@ -653,8 +483,6 @@ class Language extends Model
/**
* Returns the absolute Url for the language
*
* @return string
*/
public function url(): string
{
@@ -665,12 +493,9 @@ class Language extends Model
/**
* Update language properties and save them
*
* @internal
* @param array $props
* @return static
*/
public function update(array $props = null)
public function update(array $props = null): static
{
// don't change the language code
unset($props['code']);
@@ -681,6 +506,10 @@ class Language extends Model
$kirby = App::instance();
$updated = $this->clone($props);
if (isset($props['translations']) === true) {
$updated->translations = $props['translations'];
}
// validate the updated language
LanguageRules::update($updated);
@@ -690,32 +519,31 @@ class Language extends Model
'input' => $props
]);
// convert the current default to a non-default language
if ($updated->isDefault() === true) {
$kirby->defaultLanguage()?->clone(['default' => false])->save();
// if language just got promoted to be the new default language
if ($this->isDefault() === false && $updated->isDefault() === true) {
// convert the current default to a non-default language
$previous = $kirby->defaultLanguage()?->clone(['default' => false])->save();
$kirby->languages(false)->set($previous->code(), $previous);
$code = $this->code();
$site = $kirby->site();
touch($site->contentFile($code));
foreach ($kirby->site()->index(true) as $page) {
$files = $page->files();
foreach ($files as $file) {
touch($file->contentFile($code));
}
touch($page->contentFile($code));
foreach ($kirby->models() as $model) {
$model->storage()->touchLanguage($this);
}
} elseif ($this->isDefault() === true) {
throw new PermissionException('Please select another language to be the primary language');
}
// if language was the default language and got demoted…
if (
$this->isDefault() === true &&
$updated->isDefault() === false &&
$kirby->defaultLanguage()->code() === $this->code()
) {
// ensure another language has already been set as default
throw new LogicException('Please select another language to be the primary language');
}
$language = $updated->save();
// make sure the language is also updated in the Kirby language collection
App::instance()->languages(false)->set($language->code(), $language);
// make sure the language is also updated in the languages collection
$kirby->languages(false)->set($language->code(), $language);
// trigger after hook
$kirby->trigger('language.update:after', [
@@ -726,4 +554,19 @@ class Language extends Model
return $language;
}
/**
* Returns a language variable object
* for the key in the translations array
*/
public function variable(string $key, bool $decode = false): LanguageVariable
{
// allows decoding if base64-url encoded url is sent
// for compatibility of different environments
if ($decode === true) {
$key = rawurldecode(base64_decode($key));
}
return new LanguageVariable($this, $key);
}
}