From ce4f167fab6e5f70592740207a89a25d5e7eac50 Mon Sep 17 00:00:00 2001 From: Awilum Date: Mon, 11 Nov 2019 17:42:58 +0300 Subject: [PATCH] refactor(core): Parser API's refactoring #288 --- flextype/core/Parser.php | 4 ++-- flextype/parsers/Frontmatter.php | 10 +++++----- flextype/parsers/Markdown.php | 8 ++++++-- flextype/twig/JsonTwigExtension.php | 19 ++++++++++++++++--- flextype/twig/MarkdownTwigExtension.php | 4 ++-- flextype/twig/YamlTwigExtension.php | 20 +++++++++++++++++--- 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/flextype/core/Parser.php b/flextype/core/Parser.php index d2a80951..53a8d794 100644 --- a/flextype/core/Parser.php +++ b/flextype/core/Parser.php @@ -162,12 +162,12 @@ class Parser return $data_from_cache; } - $data = Markdown::parse($input); + $data = Markdown::decode($input); $this->flextype['cache']->save($key, $data); return $data; } else { - return Markdown::parse($input); + return Markdown::decode($input); } break; diff --git a/flextype/parsers/Frontmatter.php b/flextype/parsers/Frontmatter.php index eaeaa4a7..242f8c22 100644 --- a/flextype/parsers/Frontmatter.php +++ b/flextype/parsers/Frontmatter.php @@ -21,11 +21,11 @@ use function trim; class Frontmatter { /** - * Returns the Frontmatter representation of a value + * Returns the FRONTMATTER representation of a value * * @param mixed $input The PHP value * - * @return string A Frontmatter string representing the original PHP value + * @return string A FRONTMATTER string representing the original PHP value */ public static function encode($input) : string { @@ -47,11 +47,11 @@ class Frontmatter } /** - * Takes a Frontmatter encoded string and converts it into a PHP variable. + * Takes a FRONTMATTER encoded string and converts it into a PHP variable. * - * @param string $input A string containing Frontmatter + * @param string $input A string containing FRONTMATTER * - * @return mixed The Frontmatter converted to a PHP value + * @return mixed The FRONTMATTER converted to a PHP value */ public static function decode(string $input) { diff --git a/flextype/parsers/Markdown.php b/flextype/parsers/Markdown.php index e44009cf..3f98188c 100644 --- a/flextype/parsers/Markdown.php +++ b/flextype/parsers/Markdown.php @@ -25,9 +25,13 @@ class Markdown private static $markdown = null; /** - * parse + * Takes a MARKDOWN encoded string and converts it into a PHP variable. + * + * @param string $input A string containing MARKDOWN + * + * @return mixed The MARKDOWN converted to a PHP value */ - public static function parse($input) : string + public static function decode($input) : string { !isset(self::$markdown) and self::$markdown = new ParsedownExtra(); diff --git a/flextype/twig/JsonTwigExtension.php b/flextype/twig/JsonTwigExtension.php index 83a4ad64..0eee281f 100644 --- a/flextype/twig/JsonTwigExtension.php +++ b/flextype/twig/JsonTwigExtension.php @@ -14,6 +14,19 @@ use Twig_SimpleFunction; class JsonTwigExtension extends Twig_Extension { + /** + * Flextype Dependency Container + */ + private $flextype; + + /** + * Constructor + */ + public function __construct($flextype) + { + $this->flextype = $flextype; + } + /** * Returns a list of functions to add to the existing list. * @@ -32,14 +45,14 @@ class JsonTwigExtension extends Twig_Extension */ public function encode($input) : string { - return Json::encode($input); + return $this->flextype['parser']->encode($input, 'json'); } /** * Decode JSON */ - public function decode(string $input) + public function decode(string $input, bool $cache = true) { - return Json::decode($input); + return $this->flextype['parser']->decode($input, 'json', $cache); } } diff --git a/flextype/twig/MarkdownTwigExtension.php b/flextype/twig/MarkdownTwigExtension.php index 181752fe..ee627b5a 100644 --- a/flextype/twig/MarkdownTwigExtension.php +++ b/flextype/twig/MarkdownTwigExtension.php @@ -42,8 +42,8 @@ class MarkdownTwigExtension extends Twig_Extension /** * Markdown process */ - public function markdown($value) : string + public function markdown($input, bool $cache = true) : string { - return Markdown::parse($value); + return $this->flextype['parser']->decode($input, 'markdown', $cache); } } diff --git a/flextype/twig/YamlTwigExtension.php b/flextype/twig/YamlTwigExtension.php index 375a69b5..0b7de6c7 100644 --- a/flextype/twig/YamlTwigExtension.php +++ b/flextype/twig/YamlTwigExtension.php @@ -14,6 +14,20 @@ use Twig_SimpleFunction; class YamlTwigExtension extends Twig_Extension { + + /** + * Flextype Dependency Container + */ + private $flextype; + + /** + * Constructor + */ + public function __construct($flextype) + { + $this->flextype = $flextype; + } + /** * Returns a list of functions to add to the existing list. * @@ -32,14 +46,14 @@ class YamlTwigExtension extends Twig_Extension */ public function encode($input) : string { - return Yaml::encode($input); + return $this->flextype['parser']->encode($input, 'yaml'); } /** * Decode YAML */ - public function decode(string $input) + public function decode(string $input, bool $cache = true) { - return Yaml::decode($input); + return $this->flextype['parser']->decode($input, 'yaml', $cache); } }