From 6da11ddb372d67d31aa8eea8014cc600c5cd4c89 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 10 Aug 2019 18:34:47 +0300 Subject: [PATCH] Flextype Core: Add ability to work with different types of content #212 #186 --- composer.json | 3 +- flextype/core/Entries.php | 35 ++++++---- flextype/parsers/JsonParser.php | 2 - flextype/parsers/Parser.php | 47 +++++++++++++ flextype/parsers/YamlParser.php | 120 ++++++++++++++++++++++++++++++++ 5 files changed, 191 insertions(+), 16 deletions(-) create mode 100644 flextype/parsers/Parser.php create mode 100644 flextype/parsers/YamlParser.php diff --git a/composer.json b/composer.json index e9d2f28c..72704d2a 100755 --- a/composer.json +++ b/composer.json @@ -51,7 +51,8 @@ "cocur/slugify": "^3.2", "oscarotero/psr7-middlewares": "^3.21", "thunderer/shortcode": "^0.7.2", - "ramsey/uuid": "^3.8" + "ramsey/uuid": "^3.8", + "symfony/yaml": "^4.3.3" }, "suggest": { "ext-zend-opcache": "Recommended for better performance", diff --git a/flextype/core/Entries.php b/flextype/core/Entries.php index 56af3408..e19c2be4 100755 --- a/flextype/core/Entries.php +++ b/flextype/core/Entries.php @@ -5,8 +5,6 @@ declare(strict_types=1); /** * Flextype (http://flextype.org) * Founded by Sergey Romanenko and maintained Flextype Community. - * - * @license https://github.com/flextype/flextype/blob/master/LICENSE.txt (MIT License) */ namespace Flextype; @@ -72,13 +70,13 @@ class Entries $entry_file = $this->_file_location($id); // If requested entry founded then process it - if (Filesystem::has($entry_file)) { + if ($entry_file) { // Create unique entry cache_id // Entry Cache ID = entry + entry file + entry file time stamp - if ($timestamp = Filesystem::getTimestamp($entry_file)) { - $entry_cache_id = md5('entry' . $entry_file . $timestamp); + if ($timestamp = Filesystem::getTimestamp($entry_file['file'])) { + $entry_cache_id = md5('entry' . $entry_file['file'] . $timestamp); } else { - $entry_cache_id = md5('entry' . $entry_file); + $entry_cache_id = md5('entry' . $entry_file['file']); } // Try to get the requested entry from cache @@ -98,16 +96,16 @@ class Entries } // Try to get requested entry body content - if ($entry_body = Filesystem::read($entry_file)) { + if ($entry_body = Filesystem::read($entry_file['file'])) { // Try to decode requested entry body content - if ($entry_decoded = JsonParser::decode($entry_body)) { + if ($entry_decoded = Parser::decode($entry_body, $entry_file['driver'])) { // Add predefined entry items // Entry Date - $entry_decoded['published_at'] = $entry_decoded['published_at'] ? $entry_decoded['published_at'] : Filesystem::getTimestamp($entry_file); - $entry_decoded['created_at'] = $entry_decoded['created_at'] ? $entry_decoded['created_at'] : Filesystem::getTimestamp($entry_file); + $entry_decoded['published_at'] = $entry_decoded['published_at'] ? $entry_decoded['published_at'] : Filesystem::getTimestamp($entry_file['file']); + $entry_decoded['created_at'] = $entry_decoded['created_at'] ? $entry_decoded['created_at'] : Filesystem::getTimestamp($entry_file['file']); // Entry Timestamp - $entry_decoded['modified_at'] = Filesystem::getTimestamp($entry_file); + $entry_decoded['modified_at'] = Filesystem::getTimestamp($entry_file['file']); // Entry Slug $entry_decoded['slug'] = $entry_decoded['slug'] ?? ltrim(rtrim($id, '/'), '/'); @@ -466,9 +464,20 @@ class Entries * * @access private */ - private function _file_location(string $id) : string + private function _file_location(string $id) { - return PATH['entries'] . '/' . $id . '/entry.json'; + $json_file = PATH['entries'] . '/' . $id . '/entry.json'; + $yaml_file = PATH['entries'] . '/' . $id . '/entry.yaml'; + + if (Filesystem::has($json_file)) { + return ['file' => $json_file, 'driver' => 'json']; + } + + if (Filesystem::has($yaml_file)) { + return ['file' => $yaml_file, 'driver' => 'yaml']; + } + + return false; } /** diff --git a/flextype/parsers/JsonParser.php b/flextype/parsers/JsonParser.php index c1a7cbe6..9c5e40eb 100644 --- a/flextype/parsers/JsonParser.php +++ b/flextype/parsers/JsonParser.php @@ -5,8 +5,6 @@ declare(strict_types=1); /** * Flextype (http://flextype.org) * Founded by Sergey Romanenko and maintained Flextype Community. - * - * @license https://github.com/flextype/flextype/blob/master/LICENSE.txt (MIT License) */ namespace Flextype; diff --git a/flextype/parsers/Parser.php b/flextype/parsers/Parser.php new file mode 100644 index 00000000..09a4ffcb --- /dev/null +++ b/flextype/parsers/Parser.php @@ -0,0 +1,47 @@ +getMessage(), 0, $e); + } + } + + /** + * 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 + * + * @throws ParseException If the YAML is not valid + */ + public static function decode(string $input, int $flags = 0) + { + // Try native PECL YAML PHP extension first if available. + if (self::$native && function_exists('yaml_parse')) { + // Safely decode YAML. + $saved = @ini_get('yaml.decode_php'); + @ini_set('yaml.decode_php', 0); + $decoded = @yaml_parse($input); + @ini_set('yaml.decode_php', $saved); + + if ($decoded !== false) { + return (array) $decoded; + } + } + + try { + return (array) Yaml::parse($input); + } catch (ParseException $e) { + throw new RuntimeException('Decoding YAML failed: ' . $e->getMessage(), 0, $e); + } + } +}