From 7c6d8cfa9314a8d3344794d6a6ba0ae8e8f4973f Mon Sep 17 00:00:00 2001 From: Awilum Date: Wed, 22 Jul 2020 09:31:01 +0300 Subject: [PATCH] feat(support): Simplify parsers and serializers #438 --- src/flextype/Foundation/Parsers/Markdown.php | 37 ---- src/flextype/Foundation/Parsers/Parser.php | 111 ------------ .../shortcodes/EntriesShortcodeExtension.php | 18 -- .../Foundation/Serializers/Frontmatter.php | 65 ------- src/flextype/Foundation/Serializers/Json.php | 114 ------------- .../Foundation/Serializers/Serializer.php | 160 ------------------ 6 files changed, 505 deletions(-) delete mode 100644 src/flextype/Foundation/Parsers/Markdown.php delete mode 100644 src/flextype/Foundation/Parsers/Parser.php delete mode 100644 src/flextype/Foundation/Parsers/shortcodes/EntriesShortcodeExtension.php delete mode 100644 src/flextype/Foundation/Serializers/Frontmatter.php delete mode 100644 src/flextype/Foundation/Serializers/Json.php delete mode 100644 src/flextype/Foundation/Serializers/Serializer.php diff --git a/src/flextype/Foundation/Parsers/Markdown.php b/src/flextype/Foundation/Parsers/Markdown.php deleted file mode 100644 index a597fd1c..00000000 --- a/src/flextype/Foundation/Parsers/Markdown.php +++ /dev/null @@ -1,37 +0,0 @@ -text($input); - } -} diff --git a/src/flextype/Foundation/Parsers/Parser.php b/src/flextype/Foundation/Parsers/Parser.php deleted file mode 100644 index b3f6ea9d..00000000 --- a/src/flextype/Foundation/Parsers/Parser.php +++ /dev/null @@ -1,111 +0,0 @@ - [ - 'name' => 'markdown', - 'ext' => 'md', - ], - 'shortcodes' => [ - 'name' => 'shortcodes', - 'ext' => 'php', - ], - ]; - - /** - * Constructor - * - * @access public - */ - public function __construct($flextype) - { - $this->flextype = $flextype; - } - - /** - * Get Parser Information - * - * @param string $input Content to parse - * @param string $parser Parser type [frontmatter, json, yaml] - * - * @return array - */ - public function getParserInfo(string $parser) : array - { - return $this->parsers[$parser]; - } - - /** - * Parse INPUT content. - * - * @param string $input Content to parse - * @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 - */ - public function parse(string $input, string $parser, bool $cache = true) - { - switch ($parser) { - case 'markdown': - if ($cache === true && $this->flextype['registry']->get('flextype.settings.cache.enabled') === true) { - $key = md5($input); - - if ($data_from_cache = $this->flextype['cache']->fetch($key)) { - return $data_from_cache; - } - - $data = Markdown::decode($input); - $this->flextype['cache']->save($key, $data); - - return $data; - } else { - return Markdown::decode($input); - } - - break; - case 'shortcodes': - if ($cache === true && $this->flextype['registry']->get('flextype.settings.cache.enabled') === true) { - $key = md5($input); - - if ($data_from_cache = $this->flextype['cache']->fetch($key)) { - return $data_from_cache; - } - - $data = $this->flextype['shortcodes']->process($input); - $this->flextype['cache']->save($key, $data); - - return $data; - } else { - return $this->flextype['shortcodes']->process($input); - } - - break; - default: - // code... - break; - } - } -} diff --git a/src/flextype/Foundation/Parsers/shortcodes/EntriesShortcodeExtension.php b/src/flextype/Foundation/Parsers/shortcodes/EntriesShortcodeExtension.php deleted file mode 100644 index 4c75ac47..00000000 --- a/src/flextype/Foundation/Parsers/shortcodes/EntriesShortcodeExtension.php +++ /dev/null @@ -1,18 +0,0 @@ -addHandler('entries_fetch', static function (ShortcodeInterface $s) use ($flextype) { - return Arr::get($flextype['entries']->fetch($s->getParameter('id')), $s->getParameter('field'), $s->getParameter('default')); -}); diff --git a/src/flextype/Foundation/Serializers/Frontmatter.php b/src/flextype/Foundation/Serializers/Frontmatter.php deleted file mode 100644 index c8d44c94..00000000 --- a/src/flextype/Foundation/Serializers/Frontmatter.php +++ /dev/null @@ -1,65 +0,0 @@ - trim($input)]; - } - - return Yaml::decode(trim($parts[1])) + ['content' => trim(implode(PHP_EOL . '---' . PHP_EOL, array_slice($parts, 2)))]; - } -} diff --git a/src/flextype/Foundation/Serializers/Json.php b/src/flextype/Foundation/Serializers/Json.php deleted file mode 100644 index 04f826f5..00000000 --- a/src/flextype/Foundation/Serializers/Json.php +++ /dev/null @@ -1,114 +0,0 @@ - [ - 'name' => 'frontmatter', - 'ext' => 'md', - ], - 'json' => [ - 'name' => 'json', - 'ext' => 'json', - ], - 'yaml' => [ - 'name' => 'yaml', - 'ext' => 'yaml', - ], - ]; - - /** - * Constructor - * - * @access public - */ - public function __construct($flextype) - { - $this->flextype = $flextype; - } - - /** - * Get Parser Information - * - * @param string $input Content to parse - * @param string $serializer Parser type [frontmatter, json, yaml] - * - * @return array - */ - public function getParserInfo(string $serializer) : array - { - return $this->serializer[$serializer]; - } - - /** - * Dumps a PHP value to a string CONTENT. - * - * @param mixed $input Content to parse - * @param string $serializer Parser type [frontmatter, json, yaml] - * - * @return mixed PHP value converted to a string CONTENT. - */ - public function encode($input, string $serializer) : string - { - switch ($serializer) { - case 'frontmatter': - return Frontmatter::encode($input); - - break; - case 'json': - return Json::encode($input); - - break; - case 'yaml': - return Yaml::encode($input); - - break; - default: - break; - } - } - - /** - * Parse INPUT content into a PHP value. - * - * @param string $input Content to parse - * @param string $serializer Serializer type [frontmatter, json, yaml] - * @param bool $cache Cache result data or no. Default is true - * - * @return mixed The Content converted to a PHP value - */ - public function decode(string $input, string $serializer, bool $cache = true) - { - switch ($serializer) { - case 'frontmatter': - if ($cache === true && $this->flextype['registry']->get('flextype.settings.cache.enabled') === true) { - $key = md5($input); - - if ($data_from_cache = $this->flextype['cache']->fetch($key)) { - return $data_from_cache; - } - - $data = Frontmatter::decode($input); - $this->flextype['cache']->save($key, $data); - - return $data; - } else { - return Frontmatter::decode($input); - } - - break; - case 'json': - if ($cache === true && $this->flextype['registry']->get('flextype.settings.cache.enabled') === true) { - $key = md5($input); - - if ($data_from_cache = $this->flextype['cache']->fetch($key)) { - return $data_from_cache; - } - - $data = Json::decode($input); - $this->flextype['cache']->save($key, $data); - - return $data; - } else { - return Json::decode($input); - } - - break; - case 'yaml': - if ($cache === true && $this->flextype['registry']->get('flextype.settings.cache.enabled') === true) { - $key = md5($input); - - if ($data_from_cache = $this->flextype['cache']->fetch($key)) { - return $data_from_cache; - } - - $data = Yaml::decode($input); - $this->flextype['cache']->save($key, $data); - - return $data; - } else { - return Yaml::decode($input); - } - - break; - default: - // code... - break; - } - } -}