Upgrade to 3.9.4

This commit is contained in:
Bastian Allgeier
2023-04-21 11:26:07 +02:00
parent cd495f054e
commit 38c8ba7e59
57 changed files with 4658 additions and 87 deletions

View File

@@ -2,11 +2,11 @@
namespace Kirby\Data;
use Kirby\Cms\App;
use Kirby\Exception\InvalidArgumentException;
use Spyc;
/**
* Simple Wrapper around the Spyc YAML class
* Simple Wrapper around the Symfony or Spyc YAML class
*
* @package Kirby Data
* @author Bastian Allgeier <bastian@getkirby.com>
@@ -21,8 +21,10 @@ class Yaml extends Handler
*/
public static function encode($data): string
{
// $data, $indent, $wordwrap, $no_opening_dashes
return Spyc::YAMLDump($data, false, false, true);
return match (static::handler()) {
'symfony' => YamlSymfony::encode($data),
default => YamlSpyc::encode($data),
};
}
/**
@@ -42,16 +44,19 @@ class Yaml extends Handler
throw new InvalidArgumentException('Invalid YAML data; please pass a string');
}
// remove BOM
$string = str_replace("\xEF\xBB\xBF", '', $string);
$result = Spyc::YAMLLoadString($string);
return match (static::handler()) {
'symfony' => YamlSymfony::decode($string),
default => YamlSpyc::decode($string)
};
}
if (is_array($result) === true) {
return $result;
}
// 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
/**
* Returns which YAML parser (`spyc` or `symfony`)
* is configured to be used
* @internal
*/
public static function handler(): string
{
return App::instance(null, true)?->option('yaml.handler') ?? 'spyc';
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Kirby\Data;
use Kirby\Exception\InvalidArgumentException;
use Spyc;
/**
* Simple Wrapper around the Spyc YAML class
*
* @package Kirby Data
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class YamlSpyc
{
/**
* Converts an array to an encoded YAML string
*/
public static function encode($data): string
{
// $data, $indent, $wordwrap, $no_opening_dashes
return Spyc::YAMLDump($data, false, false, true);
}
/**
* Parses an encoded YAML string and returns a multi-dimensional array
*/
public static function decode($string): array
{
$result = Spyc::YAMLLoadString($string);
if (is_array($result) === true) {
return $result;
}
// 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
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Kirby\Data;
use Kirby\Cms\App;
use Kirby\Toolkit\A;
use Symfony\Component\Yaml\Yaml as Symfony;
/**
* Simple Wrapper around the Symfony YAML class
*
* @package Kirby Data
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class YamlSymfony
{
/**
* Converts an array to an encoded YAML string
*/
public static function encode($data): string
{
$kirby = App::instance(null, true);
return Symfony::dump(
$data,
$kirby?->option('yaml.params.inline') ?? 9999,
$kirby?->option('yaml.params.indent') ?? 2,
Symfony::DUMP_MULTI_LINE_LITERAL_BLOCK | Symfony::DUMP_EMPTY_ARRAY_AS_SEQUENCE
);
}
/**
* Parses an encoded YAML string and returns a multi-dimensional array
*/
public static function decode($string): array
{
$result = Symfony::parse($string);
$result = A::wrap($result);
return $result;
}
}