mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 14:16:46 +02:00
feat(serializers): decouple Frontmatter Parser from core #560
This commit is contained in:
@@ -1,106 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Flextype\Support\Serializers;
|
||||
|
||||
use function array_slice;
|
||||
use function arrays;
|
||||
use function count;
|
||||
use function flextype;
|
||||
use function implode;
|
||||
use function ltrim;
|
||||
use function preg_replace;
|
||||
use function preg_split;
|
||||
use function strings;
|
||||
|
||||
use const PHP_EOL;
|
||||
|
||||
class Frontmatter
|
||||
{
|
||||
/**
|
||||
* Returns the FRONTMATTER representation of a value
|
||||
*
|
||||
* @param mixed $input The PHP value
|
||||
*
|
||||
* @return string A FRONTMATTER string representing the original PHP value
|
||||
*/
|
||||
public function encode($input): string
|
||||
{
|
||||
if (isset($input['content'])) {
|
||||
$content = $input['content'];
|
||||
$input = arrays($input)->delete('content')->toArray();
|
||||
$matter = serializers()->yaml()->encode($input);
|
||||
} else {
|
||||
$content = '';
|
||||
$matter = serializers()->yaml()->encode($input);
|
||||
}
|
||||
|
||||
return '---' . "\n" .
|
||||
$matter .
|
||||
'---' . "\n" .
|
||||
$content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$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);
|
||||
|
||||
// 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()];
|
||||
}
|
||||
|
||||
return 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 && registry()->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
if ($dataFromCache = cache()->get($key)) {
|
||||
return $dataFromCache;
|
||||
}
|
||||
|
||||
$data = $decode($input);
|
||||
cache()->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $decode($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
@@ -10,6 +10,8 @@ declare(strict_types=1);
|
||||
namespace Flextype\Support\Serializers;
|
||||
|
||||
use Atomastic\Macroable\Macroable;
|
||||
use Flextype\Support\Serializers\Json;
|
||||
use Flextype\Support\Serializers\Yaml;
|
||||
|
||||
class Serializers
|
||||
{
|
||||
@@ -30,12 +32,4 @@ class Serializers
|
||||
{
|
||||
return new Yaml();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Frontmatter instance.
|
||||
*/
|
||||
public function frontmatter(): Frontmatter
|
||||
{
|
||||
return new Frontmatter();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user