mirror of
https://github.com/flextype/flextype.git
synced 2025-08-12 16:14:16 +02:00
refactor(serializers): simplify serializers codebase
This commit is contained in:
@@ -31,40 +31,6 @@ class Frontmatter
|
||||
* @return string A FRONTMATTER string representing the original PHP value
|
||||
*/
|
||||
public function encode($input): string
|
||||
{
|
||||
return $this->_encode($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a FRONTMATTER encoded string and converts it into a PHP variable.
|
||||
*
|
||||
* @param string $input A string containing FRONTMATTER
|
||||
* @param bool $cache Cache result data or no. Default is true
|
||||
*
|
||||
* @return mixed The FRONTMATTER converted to a PHP value
|
||||
*/
|
||||
public function decode(string $input, bool $cache = true)
|
||||
{
|
||||
if ($cache === true && flextype('registry')->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
if ($dataFromCache = flextype('cache')->get($key)) {
|
||||
return $dataFromCache;
|
||||
}
|
||||
|
||||
$data = $this->_decode($input);
|
||||
flextype('cache')->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $this->_decode($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see encode()
|
||||
*/
|
||||
protected function _encode($input): string
|
||||
{
|
||||
if (isset($input['content'])) {
|
||||
$content = $input['content'];
|
||||
@@ -82,27 +48,58 @@ class Frontmatter
|
||||
}
|
||||
|
||||
/**
|
||||
* @see decode()
|
||||
* Takes a FRONTMATTER encoded string and converts it into a PHP variable.
|
||||
*
|
||||
* @param string $input A string containing FRONTMATTER
|
||||
* @param bool $cache Cache result data or no. Default is true
|
||||
*
|
||||
* @return mixed The FRONTMATTER converted to a PHP value
|
||||
*/
|
||||
protected function _decode(string $input)
|
||||
public function decode(string $input, bool $cache = true)
|
||||
{
|
||||
// Remove UTF-8 BOM if it exists.
|
||||
$input = ltrim($input, "\xef\xbb\xbf");
|
||||
$decode = function (string $input) {
|
||||
// Remove UTF-8 BOM if it exists.
|
||||
$input = ltrim($input, "\xef\xbb\xbf");
|
||||
|
||||
// Normalize line endings to Unix style.
|
||||
$input = (string) preg_replace("/(\r\n|\r)/", "\n", $input);
|
||||
// Normalize line endings to Unix style.
|
||||
$input = (string) preg_replace("/(\r\n|\r)/", "\n", $input);
|
||||
|
||||
// Parse Frontmatter and Body
|
||||
$parts = preg_split('/^[\s\r\n]?---[\s\r\n]?$/sm', PHP_EOL . strings($input)->trimLeft()->toString());
|
||||
// Parse Frontmatter and Body
|
||||
$parts = preg_split('/^[\s\r\n]?---[\s\r\n]?$/sm', PHP_EOL . strings($input)->trimLeft()->toString());
|
||||
|
||||
if (count($parts) < 3) {
|
||||
return ['content' => strings($input)->trim()->toString()];
|
||||
if (count($parts) < 3) {
|
||||
return ['content' => strings($input)->trim()->toString()];
|
||||
}
|
||||
|
||||
return flextype('serializers')->yaml()->decode(strings($parts[1])->trim()->toString(), false) + ['content' => strings(implode(PHP_EOL . '---' . PHP_EOL, array_slice($parts, 2)))->trim()->toString()];
|
||||
};
|
||||
|
||||
if ($cache === true && flextype('registry')->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
if ($dataFromCache = flextype('cache')->get($key)) {
|
||||
return $dataFromCache;
|
||||
}
|
||||
|
||||
$data = $decode($input);
|
||||
flextype('cache')->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
return flextype('serializers')->yaml()->decode(strings($parts[1])->trim()->toString(), false) + ['content' => strings(implode(PHP_EOL . '---' . PHP_EOL, array_slice($parts, 2)))->trim()->toString()];
|
||||
return $decode($input);
|
||||
}
|
||||
|
||||
public function getCacheID($input): string
|
||||
/**
|
||||
* Get Cache ID for frontmatter.
|
||||
*
|
||||
* @param string $input Input.
|
||||
*
|
||||
* @return string Cache ID.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function getCacheID(string $input): string
|
||||
{
|
||||
return strings('frontmatter' . $input)->hash()->toString();
|
||||
}
|
||||
|
@@ -41,7 +41,18 @@ class Json
|
||||
*/
|
||||
public function encode($input, int $options = 0, int $depth = 512): string
|
||||
{
|
||||
return $this->_encode($input, $options, $depth);
|
||||
$options = ($options & self::ESCAPE_UNICODE ? 0 : JSON_UNESCAPED_UNICODE)
|
||||
| JSON_UNESCAPED_SLASHES
|
||||
| ($options & self::PRETTY ? JSON_PRETTY_PRINT : 0)
|
||||
| (defined('JSON_PRESERVE_ZERO_FRACTION') ? JSON_PRESERVE_ZERO_FRACTION : 0);
|
||||
|
||||
$json = json_encode($input, $options, $depth);
|
||||
|
||||
if ($error = json_last_error()) {
|
||||
throw new RuntimeException(json_last_error_msg(), $error);
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,6 +70,16 @@ class Json
|
||||
*/
|
||||
public function decode(string $input, bool $cache = true, bool $assoc = true, int $depth = 512, int $flags = 0)
|
||||
{
|
||||
$decode = function (string $input, bool $assoc = true, int $depth = 512, int $flags = 0) {
|
||||
$value = json_decode($input, $assoc, $depth, $flags);
|
||||
|
||||
if ($error = json_last_error()) {
|
||||
throw new RuntimeException(json_last_error_msg(), $error);
|
||||
}
|
||||
|
||||
return $value;
|
||||
};
|
||||
|
||||
if ($cache === true && flextype('registry')->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
@@ -66,49 +87,25 @@ class Json
|
||||
return $dataFromCache;
|
||||
}
|
||||
|
||||
$data = $this->_decode($input, $assoc, $depth, $flags);
|
||||
$data = $decode($input, $assoc, $depth, $flags);
|
||||
flextype('cache')->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $this->_decode($input);
|
||||
return $decode($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Json::encode()
|
||||
* Get Cache ID for JSON.
|
||||
*
|
||||
* @param string $input Input.
|
||||
*
|
||||
* @return string Cache ID.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function _encode($input, $options = 0, int $depth = 512): string
|
||||
{
|
||||
$options = ($options & self::ESCAPE_UNICODE ? 0 : JSON_UNESCAPED_UNICODE)
|
||||
| JSON_UNESCAPED_SLASHES
|
||||
| ($options & self::PRETTY ? JSON_PRETTY_PRINT : 0)
|
||||
| (defined('JSON_PRESERVE_ZERO_FRACTION') ? JSON_PRESERVE_ZERO_FRACTION : 0);
|
||||
|
||||
$json = json_encode($input, $options, $depth);
|
||||
|
||||
if ($error = json_last_error()) {
|
||||
throw new RuntimeException(json_last_error_msg(), $error);
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see decode()
|
||||
*/
|
||||
protected function _decode(string $input, bool $assoc = true, int $depth = 512, int $flags = 0)
|
||||
{
|
||||
$value = json_decode($input, $assoc, $depth, $flags);
|
||||
|
||||
if ($error = json_last_error()) {
|
||||
throw new RuntimeException(json_last_error_msg(), $error);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function getCacheID($input): string
|
||||
public function getCacheID(string $input): string
|
||||
{
|
||||
return strings('json' . $input)->hash()->toString();
|
||||
}
|
||||
|
@@ -59,7 +59,16 @@ class Yaml
|
||||
*/
|
||||
public function encode($input, int $inline = 5, int $indent = 2, int $flags = 0): string
|
||||
{
|
||||
return $this->_encode($input, $inline, $indent, $flags);
|
||||
try {
|
||||
return SymfonyYaml::dump(
|
||||
$input,
|
||||
$inline,
|
||||
$indent,
|
||||
$flags
|
||||
);
|
||||
} catch (SymfonyYamlDumpException $e) {
|
||||
throw new RuntimeException('Encoding YAML failed: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,6 +84,35 @@ class Yaml
|
||||
*/
|
||||
public function decode(string $input, bool $cache = true, int $flags = 0): array
|
||||
{
|
||||
$decode = function (string $input, int $flags = 0) {
|
||||
// Try native PECL YAML PHP extension first if available.
|
||||
if (function_exists('yaml_parse') && $this->native) {
|
||||
// Safely decode YAML.
|
||||
|
||||
// Save and Mute error_reporting
|
||||
$errorReporting = error_reporting();
|
||||
error_reporting(0);
|
||||
|
||||
$saved = ini_get('yaml.decode_php');
|
||||
ini_set('yaml.decode_php', '0');
|
||||
$decoded = yaml_parse($input);
|
||||
ini_set('yaml.decode_php', $saved);
|
||||
|
||||
// Restore error_reporting
|
||||
error_reporting($errorReporting);
|
||||
|
||||
if ($decoded !== false) {
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return SymfonyYaml::parse($input, $flags);
|
||||
} catch (SymfonyYamlParseException $e) {
|
||||
throw new RuntimeException('Decoding YAML failed: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
};
|
||||
|
||||
if ($cache === true && flextype('registry')->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
@@ -82,66 +120,25 @@ class Yaml
|
||||
return $dataFromCache;
|
||||
}
|
||||
|
||||
$data = $this->_decode($input, $flags);
|
||||
$data = $decode($input, $flags);
|
||||
flextype('cache')->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $this->_decode($input, $flags);
|
||||
return $decode($input, $flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see encode()
|
||||
* Get Cache ID for YAML.
|
||||
*
|
||||
* @param string $input Input.
|
||||
*
|
||||
* @return string Cache ID.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
protected function _encode($input, int $inline = 5, int $indent = 2, int $flags = 0): string
|
||||
{
|
||||
try {
|
||||
return SymfonyYaml::dump(
|
||||
$input,
|
||||
$inline,
|
||||
$indent,
|
||||
$flags
|
||||
);
|
||||
} catch (SymfonyYamlDumpException $e) {
|
||||
throw new RuntimeException('Encoding YAML failed: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see decode()
|
||||
*/
|
||||
protected function _decode(string $input, int $flags = 0): array
|
||||
{
|
||||
// Try native PECL YAML PHP extension first if available.
|
||||
if (function_exists('yaml_parse') && $this->native) {
|
||||
// Safely decode YAML.
|
||||
|
||||
// Save and Mute error_reporting
|
||||
$errorReporting = error_reporting();
|
||||
error_reporting(0);
|
||||
|
||||
$saved = ini_get('yaml.decode_php');
|
||||
ini_set('yaml.decode_php', '0');
|
||||
$decoded = yaml_parse($input);
|
||||
ini_set('yaml.decode_php', $saved);
|
||||
|
||||
// Restore error_reporting
|
||||
error_reporting($errorReporting);
|
||||
|
||||
if ($decoded !== false) {
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return SymfonyYaml::parse($input, $flags);
|
||||
} catch (SymfonyYamlParseException $e) {
|
||||
throw new RuntimeException('Decoding YAML failed: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function getCacheID($input): string
|
||||
public function getCacheID(string $input): string
|
||||
{
|
||||
return strings('yaml' . $input)->hash()->toString();
|
||||
}
|
||||
|
Reference in New Issue
Block a user