From 4b275f7c19deaefe24b437f613dc6d181932367b Mon Sep 17 00:00:00 2001 From: Awilum Date: Mon, 11 Nov 2019 16:47:11 +0300 Subject: [PATCH] refactor(core): Parser API's refactoring #288 --- flextype/bootstrap.php | 4 +- flextype/parsers/FrontmatterParser.php | 6 +- flextype/parsers/{JsonParser.php => Json.php} | 35 +++++------- flextype/parsers/Parser.php | 34 ++++++++--- flextype/parsers/{YamlParser.php => Yaml.php} | 56 ++++++++++--------- flextype/twig/JsonTwigExtension.php | 8 +-- flextype/twig/YamlTwigExtension.php | 8 +-- 7 files changed, 84 insertions(+), 67 deletions(-) rename flextype/parsers/{JsonParser.php => Json.php} (59%) rename flextype/parsers/{YamlParser.php => Yaml.php} (61%) diff --git a/flextype/bootstrap.php b/flextype/bootstrap.php index 12994a95..dc6b4771 100755 --- a/flextype/bootstrap.php +++ b/flextype/bootstrap.php @@ -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 diff --git a/flextype/parsers/FrontmatterParser.php b/flextype/parsers/FrontmatterParser.php index 53160eb9..78878b3a 100644 --- a/flextype/parsers/FrontmatterParser.php +++ b/flextype/parsers/FrontmatterParser.php @@ -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" . diff --git a/flextype/parsers/JsonParser.php b/flextype/parsers/Json.php similarity index 59% rename from flextype/parsers/JsonParser.php rename to flextype/parsers/Json.php index 4cf7c210..baac8f53 100644 --- a/flextype/parsers/JsonParser.php +++ b/flextype/parsers/Json.php @@ -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) { diff --git a/flextype/parsers/Parser.php b/flextype/parsers/Parser.php index 12035b2f..7bd7dbdb 100644 --- a/flextype/parsers/Parser.php +++ b/flextype/parsers/Parser.php @@ -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; diff --git a/flextype/parsers/YamlParser.php b/flextype/parsers/Yaml.php similarity index 61% rename from flextype/parsers/YamlParser.php rename to flextype/parsers/Yaml.php index 082803a5..93d4c443 100644 --- a/flextype/parsers/YamlParser.php +++ b/flextype/parsers/Yaml.php @@ -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); } } diff --git a/flextype/twig/JsonTwigExtension.php b/flextype/twig/JsonTwigExtension.php index 177ef870..83a4ad64 100644 --- a/flextype/twig/JsonTwigExtension.php +++ b/flextype/twig/JsonTwigExtension.php @@ -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); } } diff --git a/flextype/twig/YamlTwigExtension.php b/flextype/twig/YamlTwigExtension.php index da8c608c..375a69b5 100644 --- a/flextype/twig/YamlTwigExtension.php +++ b/flextype/twig/YamlTwigExtension.php @@ -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); } }