Upgrade to 3.2.0

This commit is contained in:
Bastian Allgeier
2019-06-25 09:56:08 +02:00
parent 9e18cf635d
commit 9c89153d35
296 changed files with 14408 additions and 2504 deletions

View File

@@ -6,7 +6,6 @@ use Kirby\Data\Data;
use Kirby\Exception\DuplicateException;
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\LogicException;
use Kirby\Exception\PermissionException;
use Kirby\Toolkit\F;
use Kirby\Toolkit\Str;
@@ -24,8 +23,9 @@ use Throwable;
*
* @package Kirby Cms
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://getkirby.com/license
*/
class Language extends Model
{
@@ -55,6 +55,11 @@ class Language extends Model
*/
protected $name;
/**
* @var array|null
*/
protected $slugs;
/**
* @var array|null
*/
@@ -81,13 +86,14 @@ class Language extends Model
'direction',
'locale',
'name',
'slugs',
'translations',
'url',
]);
}
/**
* Improved var_dump output
* Improved `var_dump` output
*
* @return array
*/
@@ -167,12 +173,11 @@ class Language extends Model
* @param array $props
* @return self
*/
public static function create(array $props): self
public static function create(array $props)
{
$props['code'] = Str::slug($props['code'] ?? null);
$kirby = App::instance();
$languages = $kirby->languages();
$site = $kirby->site();
// make the first language the default language
if ($languages->count() === 0) {
@@ -181,9 +186,8 @@ class Language extends Model
$language = new static($props);
if ($language->exists() === true) {
throw new DuplicateException('The language already exists');
}
// validate the new language
LanguageRules::create($language);
$language->save();
@@ -209,7 +213,6 @@ class Language extends Model
$kirby = App::instance();
$languages = $kirby->languages();
$site = $kirby->site();
$code = $this->code();
if (F::remove($this->root()) !== true) {
@@ -347,13 +350,50 @@ class Language extends Model
return App::instance()->root('languages') . '/' . $this->code() . '.php';
}
/**
* Returns the LanguageRouter instance
* which is used to handle language specific
* routes.
*
* @return Kirby\Cms\LanguageRouter
*/
public function router()
{
return new LanguageRouter($this);
}
/**
* Get slug rules for language
*
* @internal
* @return array
*/
public function rules(): array
{
$code = $this->locale(LC_CTYPE);
$code = Str::contains($code, '.') ? Str::before($code, '.') : $code;
$file = $this->kirby()->root('i18n:rules') . '/' . $code . '.json';
if (F::exists($file) === false) {
$file = $this->kirby()->root('i18n:rules') . '/' . Str::before($code, '_') . '.json';
}
try {
$data = Data::read($file);
} catch (\Exception $e) {
$data = [];
}
return array_merge($data, $this->slugs());
}
/**
* Saves the language settings in the languages folder
*
* @internal
* @return self
*/
public function save(): self
public function save()
{
try {
$existingData = Data::read($this->root());
@@ -384,9 +424,9 @@ class Language extends Model
* @param string $code
* @return self
*/
protected function setCode(string $code): self
protected function setCode(string $code)
{
$this->code = $code;
$this->code = trim($code);
return $this;
}
@@ -394,7 +434,7 @@ class Language extends Model
* @param boolean $default
* @return self
*/
protected function setDefault(bool $default = false): self
protected function setDefault(bool $default = false)
{
$this->default = $default;
return $this;
@@ -404,7 +444,7 @@ class Language extends Model
* @param string $direction
* @return self
*/
protected function setDirection(string $direction = 'ltr'): self
protected function setDirection(string $direction = 'ltr')
{
$this->direction = $direction === 'rtl' ? 'rtl' : 'ltr';
return $this;
@@ -414,7 +454,7 @@ class Language extends Model
* @param string|array $locale
* @return self
*/
protected function setLocale($locale = null): self
protected function setLocale($locale = null)
{
if (is_array($locale)) {
$this->locale = $locale;
@@ -433,9 +473,19 @@ class Language extends Model
* @param string $name
* @return self
*/
protected function setName(string $name = null): self
protected function setName(string $name = null)
{
$this->name = $name ?? $this->code;
$this->name = trim($name ?? $this->code);
return $this;
}
/**
* @param array $slug
* @return self
*/
protected function setSlugs(array $slugs = null)
{
$this->slugs = $slugs ?? [];
return $this;
}
@@ -443,7 +493,7 @@ class Language extends Model
* @param array $translations
* @return self
*/
protected function setTranslations(array $translations = null): self
protected function setTranslations(array $translations = null)
{
$this->translations = $translations ?? [];
return $this;
@@ -453,12 +503,22 @@ class Language extends Model
* @param string $url
* @return self
*/
protected function setUrl(string $url = null): self
protected function setUrl(string $url = null)
{
$this->url = $url;
return $this;
}
/**
* Returns the custom slug rules for this language
*
* @return array
*/
public function slugs(): array
{
return $this->slugs;
}
/**
* Returns the most important
* properties as array
@@ -473,6 +533,7 @@ class Language extends Model
'direction' => $this->direction(),
'locale' => $this->locale(),
'name' => $this->name(),
'rules' => $this->rules(),
'url' => $this->url()
];
}
@@ -504,7 +565,7 @@ class Language extends Model
* @param array $props
* @return self
*/
public function update(array $props = null): self
public function update(array $props = null)
{
$props['slug'] = Str::slug($props['slug'] ?? null);
$kirby = App::instance();