1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-26 06:24:32 +02:00

refactor(core): Parser API's refactoring #288

This commit is contained in:
Awilum
2019-11-11 16:47:11 +03:00
parent 23e5d81f35
commit 4b275f7c19
7 changed files with 84 additions and 67 deletions

View File

@@ -60,13 +60,13 @@ if (! Filesystem::has($default_settings_file_path) || ! Filesystem::has($site_se
if (($content = Filesystem::read($default_settings_file_path)) === false) {
throw new RuntimeException('Load file: ' . $default_settings_file_path . ' - failed!');
} else {
$default_settings = YamlParser::decode($content);
$default_settings = Yaml::decode($content);
}
if (($content = Filesystem::read($site_settings_file_path)) === false) {
throw new RuntimeException('Load file: ' . $site_settings_file_path . ' - failed!');
} else {
$site_settings = YamlParser::decode($content);
$site_settings = Yaml::decode($content);
}
// Merge settings

View File

@@ -36,7 +36,7 @@ class FrontmatterParser
return ['content' => trim($content)];
}
return YamlParser::decode(trim($parts[1])) + ['content' => trim(implode(PHP_EOL . '---' . PHP_EOL, array_slice($parts, 2)))];
return Yaml::decode(trim($parts[1])) + ['content' => trim(implode(PHP_EOL . '---' . PHP_EOL, array_slice($parts, 2)))];
}
public static function encode($input) : string
@@ -44,10 +44,10 @@ class FrontmatterParser
if (isset($input['content'])) {
$content = $input['content'];
Arr::delete($input, 'content');
$matter = YamlParser::encode($input);
$matter = Yaml::encode($input);
} else {
$content = '';
$matter = YamlParser::encode($input);
$matter = Yaml::encode($input);
}
$encoded = '---' . "\n" .

View File

@@ -16,7 +16,7 @@ use const JSON_UNESCAPED_UNICODE;
use function json_decode;
use function json_encode;
class JsonParser
class Json
{
/**
* Encode options
@@ -31,7 +31,7 @@ class JsonParser
/**
* Encode Depth
*
* Set the maximum depth.
* Set the maximum depth. Must be greater than zero.
*
* @var int
*/
@@ -40,7 +40,7 @@ class JsonParser
/**
* Decode assoc
*
* Set the maximum depth.
* When TRUE, returned objects will be converted into associative arrays.
*
* @var int
*/
@@ -49,7 +49,7 @@ class JsonParser
/**
* Decode Depth
*
* Set the maximum depth.
* User specified recursion depth.
*
* @var int
*/
@@ -68,20 +68,16 @@ class JsonParser
/**
* Returns the JSON representation of a value
*
* $result = JsonParser::encode($json_content);
*
* @param mixed $input A string containing JSON
* @param int $encode_depth User specified recursion depth.
* @param int $encode_options Bitmask consisting of encode options.
* @param mixed $input A string containing JSON
*
* @return mixed The JSON converted to a PHP value
*/
public static function encode($input, int $encode_options = 0, int $encode_depth = 512) : string
public static function encode($input) : string
{
$encoded = @json_encode(
$input,
$encode_options ? $encode_options : self::$encode_options,
$encode_depth ? $encode_depth : self::$encode_depth
self::$encode_options,
self::$encode_depth
);
if ($encoded === false) {
@@ -94,24 +90,19 @@ class JsonParser
/**
* Takes a JSON encoded string and converts it into a PHP variable.
*
* $array = JsonParser::decode($json_file_content);
*
* @param string $input A string containing JSON
* @param bool $decode_assoc When TRUE, returned objects will be converted into associative arrays.
* @param int $decode_depth User specified recursion depth.
* @param int $decode_options Bitmask consisting of decode options.
* @param string $input A string containing JSON
*
* @return mixed The JSON converted to a PHP value
*
* @throws ParseException If the JSON is not valid
*/
public static function decode(string $input, bool $decode_assoc = true, int $decode_depth = 512, int $decode_options = 0)
public static function decode(string $input)
{
$decoded = @json_decode(
$input,
$decode_assoc ? $decode_assoc : self::$decode_assoc,
$decode_depth ? $decode_depth : self::$decode_depth,
$decode_options ? $decode_options : self::$decode_options
self::$decode_assoc,
self::$decode_depth,
self::$decode_options
);
if ($decoded === false) {

View File

@@ -33,6 +33,9 @@ class Parser
], 'yaml' => [
'name' => 'yaml',
'ext' => 'yaml',
], 'markdown' => [
'name' => 'markdown',
'ext' => 'md',
],
];
@@ -75,11 +78,11 @@ class Parser
break;
case 'json':
return JsonParser::encode($input);
return Json::encode($input);
break;
case 'yaml':
return YamlParser::encode($input);
return Yaml::encode($input);
break;
default:
@@ -92,7 +95,7 @@ class Parser
* Parse INPUT content into a PHP value.
*
* @param string $input Content to parse
* @param string $parser Parser type [frontmatter, json, yaml]
* @param string $parser Parser type [frontmatter, json, yaml, markdown]
* @param bool $cache Cache result data or no. Default is true
*
* @return mixed The Content converted to a PHP value
@@ -125,12 +128,12 @@ class Parser
return $data_from_cache;
}
$data = JsonParser::decode($input);
$data = Json::decode($input);
$this->flextype['cache']->save($key, $data);
return $data;
} else {
return JsonParser::decode($input);
return Json::decode($input);
}
break;
@@ -142,12 +145,29 @@ class Parser
return $data_from_cache;
}
$data = YamlParser::decode($input);
$data = Yaml::decode($input);
$this->flextype['cache']->save($key, $data);
return $data;
} else {
return YamlParser::decode($input);
return Yaml::decode($input);
}
break;
case 'markdown':
if ($cache) {
$key = md5($input);
if ($data_from_cache = $this->flextype['cache']->fetch($key)) {
return $data_from_cache;
}
$data = MarkdownParser::parse($input);
$this->flextype['cache']->save($key, $data);
return $data;
} else {
return MarkdownParser::parse($input);
}
break;

View File

@@ -12,15 +12,27 @@ declare(strict_types=1);
namespace Flextype;
use RuntimeException;
use Symfony\Component\Yaml\Exception\DumpException;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\DumpException as SymfonyYamlDumpException;
use Symfony\Component\Yaml\Exception\ParseException as SymfonyYamlParseException;
use Symfony\Component\Yaml\Yaml as SymfonyYaml;
use function function_exists;
use function ini_get;
use function ini_set;
class YamlParser
class Yaml
{
const DUMP_OBJECT = 1;
const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
const PARSE_OBJECT = 4;
const PARSE_OBJECT_FOR_MAP = 8;
const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
const PARSE_DATETIME = 32;
const DUMP_OBJECT_AS_MAP = 64;
const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
const PARSE_CONSTANT = 256;
const PARSE_CUSTOM_TAGS = 512;
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
/**
* Inline
*
@@ -42,20 +54,20 @@ class YamlParser
/**
* Native
*
* Use native or symfony parser
* Use native parser or symfony parser
*
* @var bool
*/
public static $native = true;
/**
* Flag
* Flags
*
* A bit field of PARSE_* constants to customize the YAML parser behavior
*
* @var int
*/
public static $flag = 16;
public static $flags = 16;
/**
* Dumps a PHP value to a YAML string.
@@ -64,22 +76,19 @@ class YamlParser
* to convert the array into friendly YAML.
*
* @param mixed $input The PHP value
* @param int $inline The level where you switch to inline YAML
* @param int $indent The amount of spaces to use for indentation of nested nodes
* @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
*
* @return string A YAML string representing the original PHP value
*/
public static function encode($input, int $inline = 5, int $indent = 2, int $flags = 16) : string
public static function encode($input) : string
{
try {
return Yaml::dump(
return SymfonyYaml::dump(
$input,
$inline ? $inline : self::$inline,
$indent ? $indent : self::$indent,
$flags ? $flags : self::$flag
self::$inline,
self::$indent,
self::$flags
);
} catch (DumpException $e) {
} catch (SymfonyYamlDumpException $e) {
throw new RuntimeException('Encoding YAML failed: ' . $e->getMessage(), 0, $e);
}
}
@@ -87,33 +96,30 @@ class YamlParser
/**
* Parses YAML into a PHP value.
*
* $array = YamlParser::decode($yaml_file_content);
*
* @param string $input A string containing YAML
* @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
*
* @return mixed The YAML converted to a PHP value
* @return array The YAML converted to a PHP value
*
* @throws ParseException If the YAML is not valid
*/
public static function decode(string $input, int $flags = 0)
public static function decode(string $input) : array
{
// Try native PECL YAML PHP extension first if available.
if (\function_exists('yaml_parse') && self::$native) {
// Safely decode YAML.
$saved = @ini_get('yaml.decode_php');
@ini_set('yaml.decode_php', 0);
@ini_set('yaml.decode_php', '0');
$decoded = @yaml_parse($input);
@ini_set('yaml.decode_php', $saved);
if ($decoded !== false) {
return (array) $decoded;
return $decoded;
}
}
try {
return (array) Yaml::parse($input, $flags);
} catch (ParseException $e) {
return SymfonyYaml::parse($input, self::$flags);
} catch (SymfonyYamlParseException $e) {
throw new RuntimeException('Decoding YAML failed: ' . $e->getMessage(), 0, $e);
}
}

View File

@@ -30,16 +30,16 @@ class JsonTwigExtension extends Twig_Extension
/**
* Encode JSON
*/
public function encode($input, int $encode_options = 0, int $encode_depth = 512) : string
public function encode($input) : string
{
return JsonParser::encode($input, $encode_options, $encode_depth);
return Json::encode($input);
}
/**
* Decode JSON
*/
public function decode(string $input, bool $decode_assoc = true, int $decode_depth = 512, int $decode_options = 0)
public function decode(string $input)
{
return JsonParser::decode($input, $decode_assoc, $decode_depth, $decode_options);
return Json::decode($input);
}
}

View File

@@ -30,16 +30,16 @@ class YamlTwigExtension extends Twig_Extension
/**
* Encode YAML
*/
public function encode($input, int $inline = 5, int $indent = 2, int $flags = 16) : string
public function encode($input) : string
{
return YamlParser::encode($input, $inline, $indent, $flags);
return Yaml::encode($input);
}
/**
* Decode YAML
*/
public function decode(string $input, int $flags = 0)
public function decode(string $input)
{
return YamlParser::decode($input, $flags);
return Yaml::decode($input);
}
}