update kirby to v5 and add refresh cache panel view button
This commit is contained in:
commit
9a86d41254
466 changed files with 19960 additions and 10497 deletions
|
|
@ -4,6 +4,7 @@ namespace Kirby\Data;
|
|||
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Filesystem\F;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* The `Data` class provides readers and
|
||||
|
|
@ -61,13 +62,17 @@ class Data
|
|||
}
|
||||
|
||||
if ($handler === null || class_exists($handler) === false) {
|
||||
throw new Exception('Missing handler for type: "' . $type . '"');
|
||||
throw new Exception(
|
||||
message: 'Missing handler for type: "' . $type . '"'
|
||||
);
|
||||
}
|
||||
|
||||
$handler = new $handler();
|
||||
|
||||
if ($handler instanceof Handler === false) {
|
||||
throw new Exception('Handler for type: "' . $type . '" needs to extend Kirby\\Data\\Handler');
|
||||
throw new Exception(
|
||||
message: 'Handler for type: "' . $type . '" needs to extend ' . Handler::class
|
||||
);
|
||||
}
|
||||
|
||||
return $handler;
|
||||
|
|
@ -76,9 +81,20 @@ class Data
|
|||
/**
|
||||
* Decodes data with the specified handler
|
||||
*/
|
||||
public static function decode($string, string $type): array
|
||||
{
|
||||
return static::handler($type)->decode($string);
|
||||
public static function decode(
|
||||
$string,
|
||||
string $type,
|
||||
bool $fail = true
|
||||
): array {
|
||||
try {
|
||||
return static::handler($type)->decode($string);
|
||||
} catch (Throwable $e) {
|
||||
if ($fail === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -94,11 +110,22 @@ class Data
|
|||
* the data handler is automatically chosen by
|
||||
* the extension if not specified
|
||||
*/
|
||||
public static function read(string $file, string|null $type = null): array
|
||||
{
|
||||
$type ??= F::extension($file);
|
||||
$handler = static::handler($type);
|
||||
return $handler->read($file);
|
||||
public static function read(
|
||||
string $file,
|
||||
string|null $type = null,
|
||||
bool $fail = true
|
||||
): array {
|
||||
try {
|
||||
$type ??= F::extension($file);
|
||||
$handler = static::handler($type);
|
||||
return $handler->read($file);
|
||||
} catch (Throwable $e) {
|
||||
if ($fail === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -38,7 +38,9 @@ abstract class Handler
|
|||
$contents = F::read($file);
|
||||
|
||||
if ($contents === false) {
|
||||
throw new Exception('The file "' . $file . '" does not exist or cannot be read');
|
||||
throw new Exception(
|
||||
message: 'The file "' . $file . '" does not exist or cannot be read'
|
||||
);
|
||||
}
|
||||
|
||||
return static::decode($contents);
|
||||
|
|
|
|||
|
|
@ -18,12 +18,15 @@ class Json extends Handler
|
|||
/**
|
||||
* Converts an array to an encoded JSON string
|
||||
*/
|
||||
public static function encode($data): string
|
||||
public static function encode($data, bool $pretty = false): string
|
||||
{
|
||||
return json_encode(
|
||||
$data,
|
||||
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
|
||||
);
|
||||
$constants = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
|
||||
|
||||
if ($pretty === true) {
|
||||
$constants |= JSON_PRETTY_PRINT;
|
||||
}
|
||||
|
||||
return json_encode($data, $constants);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -40,7 +43,9 @@ class Json extends Handler
|
|||
}
|
||||
|
||||
if (is_string($string) === false) {
|
||||
throw new InvalidArgumentException('Invalid JSON data; please pass a string');
|
||||
throw new InvalidArgumentException(
|
||||
message: 'Invalid JSON data; please pass a string'
|
||||
);
|
||||
}
|
||||
|
||||
$result = json_decode($string, true);
|
||||
|
|
@ -49,6 +54,8 @@ class Json extends Handler
|
|||
return $result;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('JSON string is invalid');
|
||||
throw new InvalidArgumentException(
|
||||
message: 'JSON string is invalid'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,30 +18,42 @@ use Kirby\Filesystem\F;
|
|||
class PHP extends Handler
|
||||
{
|
||||
/**
|
||||
* Converts an array to PHP file content
|
||||
* Converts data to PHP file content
|
||||
*
|
||||
* @param string $indent For internal use only
|
||||
*/
|
||||
public static function encode($data, string $indent = ''): string
|
||||
{
|
||||
switch (gettype($data)) {
|
||||
case 'array':
|
||||
$indexed = array_keys($data) === range(0, count($data) - 1);
|
||||
$array = [];
|
||||
return match (gettype($data)) {
|
||||
'array' => static::encodeArray($data, $indent),
|
||||
'boolean' => $data ? 'true' : 'false',
|
||||
'integer',
|
||||
'double' => (string)$data,
|
||||
default => var_export($data, true)
|
||||
};
|
||||
}
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$array[] = "$indent " . ($indexed ? '' : static::encode($key) . ' => ') . static::encode($value, "$indent ");
|
||||
}
|
||||
/**
|
||||
* Converts an array to PHP file content
|
||||
*/
|
||||
protected static function encodeArray(array $data, string $indent): string
|
||||
{
|
||||
$indexed = array_is_list($data);
|
||||
$lines = [];
|
||||
|
||||
return "[\n" . implode(",\n", $array) . "\n" . $indent . ']';
|
||||
case 'boolean':
|
||||
return $data ? 'true' : 'false';
|
||||
case 'integer':
|
||||
case 'double':
|
||||
return (string)$data;
|
||||
default:
|
||||
return var_export($data, true);
|
||||
foreach ($data as $key => $value) {
|
||||
$line = "$indent ";
|
||||
|
||||
if ($indexed === false) {
|
||||
$line .= static::encode($key) . ' => ';
|
||||
}
|
||||
|
||||
$line .= static::encode($value, "$indent ");
|
||||
|
||||
$lines[] = $line;
|
||||
}
|
||||
|
||||
return "[\n" . implode(",\n", $lines) . "\n" . $indent . ']';
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -49,7 +61,9 @@ class PHP extends Handler
|
|||
*/
|
||||
public static function decode($string): array
|
||||
{
|
||||
throw new BadMethodCallException('The PHP::decode() method is not implemented');
|
||||
throw new BadMethodCallException(
|
||||
message: 'The PHP::decode() method is not implemented'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -58,7 +72,9 @@ class PHP extends Handler
|
|||
public static function read(string $file): array
|
||||
{
|
||||
if (is_file($file) !== true) {
|
||||
throw new Exception('The file "' . $file . '" does not exist');
|
||||
throw new Exception(
|
||||
message: 'The file "' . $file . '" does not exist'
|
||||
);
|
||||
}
|
||||
|
||||
return (array)F::load($file, [], allowOutput: false);
|
||||
|
|
|
|||
|
|
@ -42,13 +42,12 @@ class Txt extends Handler
|
|||
*/
|
||||
protected static function encodeValue(array|string|float $value): string
|
||||
{
|
||||
// avoid problems with arrays
|
||||
if (is_array($value) === true) {
|
||||
$value = Data::encode($value, 'yaml');
|
||||
// avoid problems with localized floats
|
||||
} elseif (is_float($value) === true) {
|
||||
$value = Str::float($value);
|
||||
}
|
||||
// avoid problems with certain values
|
||||
$value = match (true) {
|
||||
is_array($value) => Data::encode($value, 'yaml'),
|
||||
is_float($value) => Str::float($value),
|
||||
default => $value
|
||||
};
|
||||
|
||||
// escape accidental dividers within a field
|
||||
$value = preg_replace('!(?<=\n|^)----!', '\\----', $value);
|
||||
|
|
@ -64,9 +63,10 @@ class Txt extends Handler
|
|||
$value = trim($value);
|
||||
$result = $key . ':';
|
||||
|
||||
// multi-line content
|
||||
$result .= match (preg_match('!\R!', $value)) {
|
||||
1 => "\n\n",
|
||||
// multi-line content
|
||||
1 => "\n\n",
|
||||
// single line content, just add space after colon
|
||||
default => ' ',
|
||||
};
|
||||
|
||||
|
|
@ -89,7 +89,9 @@ class Txt extends Handler
|
|||
}
|
||||
|
||||
if (is_string($string) === false) {
|
||||
throw new InvalidArgumentException('Invalid TXT data; please pass a string');
|
||||
throw new InvalidArgumentException(
|
||||
message: 'Invalid TXT data; please pass a string'
|
||||
);
|
||||
}
|
||||
|
||||
// remove Unicode BOM at the beginning of the file
|
||||
|
|
|
|||
|
|
@ -38,7 +38,9 @@ class Xml extends Handler
|
|||
}
|
||||
|
||||
if (is_string($string) === false) {
|
||||
throw new InvalidArgumentException('Invalid XML data; please pass a string');
|
||||
throw new InvalidArgumentException(
|
||||
message: 'Invalid XML data; please pass a string'
|
||||
);
|
||||
}
|
||||
|
||||
$result = XmlConverter::parse($string);
|
||||
|
|
@ -53,6 +55,6 @@ class Xml extends Handler
|
|||
return $result;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('XML string is invalid');
|
||||
throw new InvalidArgumentException(message: 'XML string is invalid');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,9 @@ class Yaml extends Handler
|
|||
}
|
||||
|
||||
if (is_string($string) === false) {
|
||||
throw new InvalidArgumentException('Invalid YAML data; please pass a string');
|
||||
throw new InvalidArgumentException(
|
||||
message: 'Invalid YAML data; please pass a string'
|
||||
);
|
||||
}
|
||||
|
||||
return match (static::handler()) {
|
||||
|
|
@ -53,7 +55,6 @@ class Yaml extends Handler
|
|||
/**
|
||||
* Returns which YAML parser (`spyc` or `symfony`)
|
||||
* is configured to be used
|
||||
* @internal
|
||||
*/
|
||||
public static function handler(): string
|
||||
{
|
||||
|
|
|
|||
|
|
@ -38,6 +38,6 @@ class YamlSpyc
|
|||
|
||||
// apparently Spyc always returns an array, even for invalid YAML syntax
|
||||
// so this Exception should currently never be thrown
|
||||
throw new InvalidArgumentException('The YAML data cannot be parsed'); // @codeCoverageIgnore
|
||||
throw new InvalidArgumentException(message: 'The YAML data cannot be parsed'); // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue