Update to Kirby 4.7.0

This commit is contained in:
Paul Nicoué 2025-04-21 18:57:21 +02:00
parent 02a9ab387c
commit ba25a9a198
509 changed files with 26604 additions and 14872 deletions

View file

@ -0,0 +1,21 @@
<?php
namespace Kirby\Exception;
/**
* AuthException
* Thrown when authentication is required
* but no user is logged in.
*
* @package Kirby Exception
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class AuthException extends Exception
{
protected static string $defaultKey = 'auth';
protected static string $defaultFallback = 'Unauthenticated';
protected static int $defaultHttpCode = 401;
}

View file

@ -14,8 +14,8 @@ namespace Kirby\Exception;
*/
class BadMethodCallException extends Exception
{
protected static $defaultKey = 'invalidMethod';
protected static $defaultFallback = 'The method "{ method }" does not exist';
protected static $defaultHttpCode = 400;
protected static $defaultData = ['method' => null];
protected static string $defaultKey = 'invalidMethod';
protected static string $defaultFallback = 'The method "{ method }" does not exist';
protected static int $defaultHttpCode = 400;
protected static array $defaultData = ['method' => null];
}

View file

@ -15,7 +15,7 @@ namespace Kirby\Exception;
*/
class DuplicateException extends Exception
{
protected static $defaultKey = 'duplicate';
protected static $defaultFallback = 'The entry exists';
protected static $defaultHttpCode = 400;
protected static string $defaultKey = 'duplicate';
protected static string $defaultFallback = 'The entry exists';
protected static int $defaultHttpCode = 400;
}

View file

@ -15,7 +15,7 @@ namespace Kirby\Exception;
*/
class ErrorPageException extends Exception
{
protected static $defaultKey = 'errorPage';
protected static $defaultFallback = 'Triggered error page';
protected static $defaultHttpCode = 404;
protected static string $defaultKey = 'errorPage';
protected static string $defaultFallback = 'Triggered error page';
protected static int $defaultHttpCode = 404;
}

View file

@ -2,6 +2,7 @@
namespace Kirby\Exception;
use Kirby\Cms\App;
use Kirby\Http\Environment;
use Kirby\Toolkit\I18n;
use Kirby\Toolkit\Str;
@ -21,48 +22,39 @@ class Exception extends \Exception
{
/**
* Data variables that can be used inside the exception message
*
* @var array
*/
protected $data;
protected array $data;
/**
* HTTP code that corresponds with the exception
*
* @var int
*/
protected $httpCode;
protected int $httpCode;
/**
* Additional details that are not included in the exception message
*
* @var array
*/
protected $details;
protected array $details;
/**
* Whether the exception message could be translated into the user's language
*
* @var bool
* Whether the exception message could be translated
* into the user's language
*/
protected $isTranslated = true;
protected bool $isTranslated = true;
/**
* Defaults that can be overridden by specific
* exception classes
*/
protected static $defaultKey = 'general';
protected static $defaultFallback = 'An error occurred';
protected static $defaultData = [];
protected static $defaultHttpCode = 500;
protected static $defaultDetails = [];
protected static string $defaultKey = 'general';
protected static string $defaultFallback = 'An error occurred';
protected static array $defaultData = [];
protected static int $defaultHttpCode = 500;
protected static array $defaultDetails = [];
/**
* Prefix for the exception key (e.g. 'error.general')
*
* @var string
*/
private static $prefix = 'error';
private static string $prefix = 'error';
/**
* Class constructor
@ -71,7 +63,7 @@ class Exception extends \Exception
* 'data', 'httpCode', 'details' and 'previous') or
* just the message string
*/
public function __construct($args = [])
public function __construct(array|string $args = [])
{
// set data and httpCode from provided arguments or defaults
$this->data = $args['data'] ?? static::$defaultData;
@ -79,19 +71,25 @@ class Exception extends \Exception
$this->details = $args['details'] ?? static::$defaultDetails;
// define the Exception key
$key = self::$prefix . '.' . ($args['key'] ?? static::$defaultKey);
$key = $args['key'] ?? static::$defaultKey;
if (Str::startsWith($key, self::$prefix . '.') === false) {
$key = self::$prefix . '.' . $key;
}
if (is_string($args) === true) {
$this->isTranslated = false;
parent::__construct($args);
} else {
// define whether message can/should be translated
$translate = ($args['translate'] ?? true) === true && class_exists('Kirby\Cms\App') === true;
$translate =
($args['translate'] ?? true) === true &&
class_exists(App::class) === true;
// fallback waterfall for message string
$message = null;
if ($translate) {
if ($translate === true) {
// 1. translation for provided key in current language
// 2. translation for provided key in default language
if (isset($args['key']) === true) {
@ -106,7 +104,7 @@ class Exception extends \Exception
$this->isTranslated = false;
}
if ($translate) {
if ($translate === true) {
// 4. translation for default key in current language
// 5. translation for default key in default language
if ($message === null) {
@ -122,11 +120,7 @@ class Exception extends \Exception
}
// format message with passed data
$message = Str::template($message, $this->data, [
'fallback' => '-',
'start' => '{',
'end' => '}'
]);
$message = Str::template($message, $this->data, ['fallback' => '-']);
// handover to Exception parent class constructor
parent::__construct($message, 0, $args['previous'] ?? null);
@ -139,8 +133,6 @@ class Exception extends \Exception
/**
* Returns the file in which the Exception was created
* relative to the document root
*
* @return string
*/
final public function getFileRelative(): string
{
@ -156,8 +148,6 @@ class Exception extends \Exception
/**
* Returns the data variables from the message
*
* @return array
*/
final public function getData(): array
{
@ -167,8 +157,6 @@ class Exception extends \Exception
/**
* Returns the additional details that are
* not included in the message
*
* @return array
*/
final public function getDetails(): array
{
@ -177,8 +165,6 @@ class Exception extends \Exception
/**
* Returns the exception key (error type)
*
* @return string
*/
final public function getKey(): string
{
@ -188,8 +174,6 @@ class Exception extends \Exception
/**
* Returns the HTTP code that corresponds
* with the exception
*
* @return array
*/
final public function getHttpCode(): int
{
@ -199,8 +183,6 @@ class Exception extends \Exception
/**
* Returns whether the exception message could
* be translated into the user's language
*
* @return bool
*/
final public function isTranslated(): bool
{
@ -209,8 +191,6 @@ class Exception extends \Exception
/**
* Converts the object to an array
*
* @return array
*/
public function toArray(): array
{

View file

@ -14,8 +14,8 @@ namespace Kirby\Exception;
*/
class InvalidArgumentException extends Exception
{
protected static $defaultKey = 'invalidArgument';
protected static $defaultFallback = 'Invalid argument "{ argument }" in method "{ method }"';
protected static $defaultHttpCode = 400;
protected static $defaultData = ['argument' => null, 'method' => null];
protected static string $defaultKey = 'invalidArgument';
protected static string $defaultFallback = 'Invalid argument "{ argument }" in method "{ method }"';
protected static int $defaultHttpCode = 400;
protected static array $defaultData = ['argument' => null, 'method' => null];
}

View file

@ -14,7 +14,7 @@ namespace Kirby\Exception;
*/
class LogicException extends Exception
{
protected static $defaultKey = 'logic';
protected static $defaultFallback = 'This task cannot be finished';
protected static $defaultHttpCode = 400;
protected static string $defaultKey = 'logic';
protected static string $defaultFallback = 'This task cannot be finished';
protected static int $defaultHttpCode = 400;
}

View file

@ -14,7 +14,7 @@ namespace Kirby\Exception;
*/
class NotFoundException extends Exception
{
protected static $defaultKey = 'notFound';
protected static $defaultFallback = 'Not found';
protected static $defaultHttpCode = 404;
protected static string $defaultKey = 'notFound';
protected static string $defaultFallback = 'Not found';
protected static int $defaultHttpCode = 404;
}

View file

@ -15,7 +15,7 @@ namespace Kirby\Exception;
*/
class PermissionException extends Exception
{
protected static $defaultKey = 'permission';
protected static $defaultFallback = 'You are not allowed to do this';
protected static $defaultHttpCode = 403;
protected static string $defaultKey = 'permission';
protected static string $defaultFallback = 'You are not allowed to do this';
protected static int $defaultHttpCode = 403;
}