diff --git a/flextype/bootstrap.php b/flextype/bootstrap.php index 051ab220..12994a95 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 = Parser::decode($content, 'yaml'); + $default_settings = YamlParser::decode($content); } if (($content = Filesystem::read($site_settings_file_path)) === false) { throw new RuntimeException('Load file: ' . $site_settings_file_path . ' - failed!'); } else { - $site_settings = Parser::decode($content, 'yaml'); + $site_settings = YamlParser::decode($content); } // Merge settings diff --git a/flextype/core/Entries.php b/flextype/core/Entries.php index 12b2a8ce..ce9c1546 100755 --- a/flextype/core/Entries.php +++ b/flextype/core/Entries.php @@ -142,7 +142,7 @@ class Entries // else Try to get requested entry from the filesystem } - $entry_decoded = Parser::decode(Filesystem::read($entry_file), 'frontmatter'); + $entry_decoded = $this->flextype['parser']->decode(Filesystem::read($entry_file), 'frontmatter'); // Add predefined entry items // Entry Date @@ -414,8 +414,8 @@ class Entries if (Filesystem::has($entry_file)) { $body = Filesystem::read($entry_file); - $entry = Parser::decode($body, 'frontmatter'); - return Filesystem::write($entry_file, Parser::encode(array_replace_recursive($entry, $data), 'frontmatter')); + $entry = $this->flextype['parser']->decode($body, 'frontmatter'); + return Filesystem::write($entry_file, $this->flextype['parser']->encode(array_replace_recursive($entry, $data), 'frontmatter')); } return false; @@ -448,7 +448,7 @@ class Entries $data['published_by'] = (Session::exists('uuid') ? Session::get('uuid') : ''); $data['created_by'] = (Session::exists('uuid') ? Session::get('uuid') : ''); - return Filesystem::write($entry_file, Parser::encode($data, 'frontmatter')); + return Filesystem::write($entry_file, $this->flextype['parser']->encode($data, 'frontmatter')); } return false; diff --git a/flextype/core/Fieldsets.php b/flextype/core/Fieldsets.php index 9705db4f..de9de118 100644 --- a/flextype/core/Fieldsets.php +++ b/flextype/core/Fieldsets.php @@ -45,7 +45,7 @@ class Fieldsets if (Filesystem::has($fieldset_file)) { if ($fieldset_body = Filesystem::read($fieldset_file)) { - if ($fieldset_decoded = Parser::decode($fieldset_body, 'yaml')) { + if ($fieldset_decoded = $this->flextype['parser']->decode($fieldset_body, 'yaml')) { return $fieldset_decoded; } @@ -80,7 +80,7 @@ class Fieldsets continue; } - $fieldset_content = Parser::decode(Filesystem::read($fieldset['path']), 'yaml'); + $fieldset_content = $this->flextype['parser']->decode(Filesystem::read($fieldset['path']), 'yaml'); $fieldsets[$fieldset['basename']] = $fieldset_content['title']; } } @@ -119,7 +119,7 @@ class Fieldsets $fieldset_file = $this->getFileLocation($id); if (Filesystem::has($fieldset_file)) { - return Filesystem::write($fieldset_file, Parser::encode($data, 'yaml')); + return Filesystem::write($fieldset_file, $this->flextype['parser']->encode($data, 'yaml')); } return false; @@ -140,7 +140,7 @@ class Fieldsets $fieldset_file = $this->getFileLocation($id); if (! Filesystem::has($fieldset_file)) { - return Filesystem::write($fieldset_file, Parser::encode($data, 'yaml')); + return Filesystem::write($fieldset_file, $this->flextype['parser']->encode($data, 'yaml')); } return false; diff --git a/flextype/core/Plugins.php b/flextype/core/Plugins.php index 7d664cdd..661d8ed7 100755 --- a/flextype/core/Plugins.php +++ b/flextype/core/Plugins.php @@ -41,7 +41,7 @@ class Plugins public function __construct($flextype, $app) { $this->flextype = $flextype; - $this->locales = Parser::decode(Filesystem::read(ROOT_DIR . '/flextype/config/locales.yaml'), 'yaml'); + $this->locales = $this->flextype['parser']->decode(Filesystem::read(ROOT_DIR . '/flextype/config/locales.yaml'), 'yaml'); } /** @@ -117,11 +117,11 @@ class Plugins if (Filesystem::has($default_plugin_settings_file)) { $default_plugin_settings_file_content = Filesystem::read($default_plugin_settings_file); - $default_plugin_settings = Parser::decode($default_plugin_settings_file_content, 'yaml'); + $default_plugin_settings = $this->flextype['parser']->decode($default_plugin_settings_file_content, 'yaml'); if (Filesystem::has($site_plugin_settings_file)) { $site_plugin_settings_file_content = Filesystem::read($site_plugin_settings_file); - $site_plugin_settings = Parser::decode($site_plugin_settings_file_content, 'yaml'); + $site_plugin_settings = $this->flextype['parser']->decode($site_plugin_settings_file_content, 'yaml'); } } else { throw new RuntimeException('Load ' . $plugin['dirname'] . ' plugin settings - failed!'); @@ -129,11 +129,11 @@ class Plugins if (Filesystem::has($default_plugin_manifest_file)) { $default_plugin_manifest_file_content = Filesystem::read($default_plugin_manifest_file); - $default_plugin_manifest = Parser::decode($default_plugin_manifest_file_content, 'yaml'); + $default_plugin_manifest = $this->flextype['parser']->decode($default_plugin_manifest_file_content, 'yaml'); if (Filesystem::has($site_plugin_manifest_file)) { $site_plugin_manifest_file_content = Filesystem::read($site_plugin_manifest_file); - $site_plugin_manifest = Parser::decode($site_plugin_manifest_file_content, 'yaml'); + $site_plugin_manifest = $this->flextype['parser']->decode($site_plugin_manifest_file_content, 'yaml'); } } else { throw new RuntimeException('Load ' . $plugin['dirname'] . ' plugin manifest - failed!'); @@ -188,7 +188,7 @@ class Plugins throw new RuntimeException('Load file: ' . $language_file . ' - failed!'); } - I18n::add(Parser::decode($content, 'yaml'), $locale); + I18n::add($this->flextype['parser']->decode($content, 'yaml'), $locale); } return I18n::$dictionary; diff --git a/flextype/core/Themes.php b/flextype/core/Themes.php index 46a3102d..fbc210da 100644 --- a/flextype/core/Themes.php +++ b/flextype/core/Themes.php @@ -62,7 +62,7 @@ class Themes throw new RuntimeException('Load file: ' . $theme_settings_file . ' - failed!'); } - $theme_settings = Parser::decode($content, 'yaml'); + $theme_settings = $this->flextype['parser']->decode($content, 'yaml'); } // Get theme manifest @@ -71,7 +71,7 @@ class Themes throw new RuntimeException('Load file: ' . $theme_manifest_file . ' - failed!'); } - $theme_manifest = Parser::decode($content, 'yaml'); + $theme_manifest = $this->flextype['parser']->decode($content, 'yaml'); } $themes[$theme['dirname']] = array_merge($theme_settings, $theme_manifest); diff --git a/flextype/dependencies.php b/flextype/dependencies.php index 4c3e5f1d..a617ce36 100644 --- a/flextype/dependencies.php +++ b/flextype/dependencies.php @@ -112,6 +112,13 @@ $flextype['cache'] = static function ($container) use ($flextype) { return new Cache($flextype); }; +/** + * Add cache service to Flextype container + */ +$flextype['parser'] = static function ($container) use ($flextype) { + return new Parser($flextype); +}; + /** * Add images service to Flextype container */ @@ -248,7 +255,7 @@ $flextype['view'] = static function ($container) { $view->addExtension(new YamlTwigExtension()); // Add Parser Twig Extension - $view->addExtension(new ParserTwigExtension()); + $view->addExtension(new ParserTwigExtension($container)); // Add Markdown Twig Extension $view->addExtension(new MarkdownTwigExtension($container)); diff --git a/flextype/parsers/Parser.php b/flextype/parsers/Parser.php index fec18d71..20e36606 100644 --- a/flextype/parsers/Parser.php +++ b/flextype/parsers/Parser.php @@ -12,23 +12,20 @@ namespace Flextype; class Parser { /** - * Default parser - * - * @var array + * Flextype Dependency Container */ - public static $default_parser = 'frontmatter'; + private $flextype; /** * Parsers * * @var array */ - public static $parsers = [ + private $parsers = [ 'frontmatter' => [ 'name' => 'frontmatter', 'ext' => 'md', - ], - 'json' => [ + ], 'json' => [ 'name' => 'json', 'ext' => 'json', ], 'yaml' => [ @@ -37,7 +34,38 @@ class Parser ], ]; - public static function encode($input, string $parser) : string + /** + * 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]; + } + + /** + * Dumps a PHP value to a string CONTENT. + * + * @param string $input Content to parse + * @param string $parser Parser type [frontmatter, json, yaml] + * + * @return mixed PHP value converted to a string CONTENT. + */ + public function encode($input, string $parser) : string { switch ($parser) { case 'frontmatter': @@ -53,24 +81,72 @@ class Parser break; default: - // code... + return FrontmatterParser::encode($input); + break; } } - public static function decode(string $input, string $parser) + /** + * Parse INPUT content into a PHP value. + * + * @param string $input Content to parse + * @param string $parser Parser 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 $parser, bool $cache = true) { switch ($parser) { case 'frontmatter': - return FrontmatterParser::decode($input); + if ($cache) { + $key = md5($input); + + if ($this->flextype['cache']->contains($key)) { + return $this->flextype['cache']->fetch($key); + } else { + $data = FrontmatterParser::decode($input); + $this->flextype['cache']->save($key, $data); + return $data; + } + } else { + return FrontmatterParser::decode($input); + } break; case 'json': - return JsonParser::decode($input); + + if ($cache) { + $key = md5($input); + + if ($this->flextype['cache']->contains($key)) { + return $this->flextype['cache']->fetch($key); + } else { + $data = JsonParser::decode($input); + $this->flextype['cache']->save($key, $data); + return $data; + } + } else { + return JsonParser::decode($input); + } break; case 'yaml': - return YamlParser::decode($input); + + if ($cache) { + $key = md5($input); + + if ($this->flextype['cache']->contains($key)) { + return $this->flextype['cache']->fetch($key); + } else { + $data = YamlParser::decode($input); + $this->flextype['cache']->save($key, $data); + return $data; + } + } else { + return YamlParser::decode($input); + } break; default: