From acfc1ee6ff8e9c472057e2c21a5189aee3d1f0a5 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 12 Oct 2019 19:31:37 +0300 Subject: [PATCH] fix(core): fix Plugins API - createPluginsDictionary method and increase app perfomance #259 --- flextype/core/Plugins.php | 86 +++++++++++++++++++---------- flextype/twig/I18nTwigExtension.php | 4 +- 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/flextype/core/Plugins.php b/flextype/core/Plugins.php index c3b7d523..257aed51 100755 --- a/flextype/core/Plugins.php +++ b/flextype/core/Plugins.php @@ -66,28 +66,33 @@ class Plugins // Set empty plugins item $this->flextype['registry']->set('plugins', []); - // Get Plugins List - $plugins_list = []; + // Set locale + $locale = $this->flextype['registry']->get('settings.locale'); - foreach (Filesystem::listContents(PATH['plugins']) as $plugin) { - if ($plugin['type'] !== 'dir') { - continue; - } + // Get plugins list + $plugins_list = $this->getPluginsList(); - $plugins_list[] = $plugin; - } - - // Get plugins cache ID + // Get plugins Cache ID $plugins_cache_id = $this->getPluginsCacheID($plugins_list); - // If Plugins List isnt empty then create plugin cache ID + // If Plugins List isnt empty then continue if (! is_array($plugins_list) || count($plugins_list) <= 0) { return; } - // Get plugins list from cache or scan plugins folder and create new plugins cache item + // Get plugins from cache or scan plugins folder and create new plugins cache item if ($this->flextype['cache']->contains($plugins_cache_id)) { + $this->flextype['registry']->set('plugins', $this->flextype['cache']->fetch($plugins_cache_id)); + + if ($this->flextype['cache']->contains($locale)) { + I18n::add($this->flextype['cache']->fetch($locale), $locale); + } else { + // Save plugins dictionary + $dictionary = $this->getPluginsDictionary($plugins_list, $locale); + $this->flextype['cache']->save($locale, $dictionary); + } + } else { // If Plugins List isnt empty if (is_array($plugins_list) && count($plugins_list) > 0) { @@ -147,13 +152,16 @@ class Plugins // Sort plugins list by priority. $plugins = Arr::sort($plugins, 'priority', 'DESC'); + // Save plugins list $this->flextype['registry']->set('plugins', $plugins); $this->flextype['cache']->save($plugins_cache_id, $plugins); + + // Save plugins dictionary + $dictionary = $this->getPluginsDictionary($plugins_list, $locale); + $this->flextype['cache']->save($locale, $dictionary); } } - $this->createPluginsDictionary($plugins_list); - $this->includeEnabledPlugins($flextype, $app); $this->flextype['emitter']->emit('onPluginsInitialized'); @@ -166,26 +174,23 @@ class Plugins * * @access protected */ - private function createPluginsDictionary(array $plugins_list) : void + private function getPluginsDictionary(array $plugins_list, string $locale) : array { - if (! is_array($plugins_list) || count($plugins_list) <= 0) { - return; - } + foreach ($plugins_list as $plugin) { + $language_file = PATH['plugins'] . '/' . $plugin['dirname'] . '/lang/' . $locale . '.yaml'; - foreach ($this->locales as $locale => $locale_title) { - foreach ($plugins_list as $plugin) { - $language_file = PATH['plugins'] . '/' . $plugin['dirname'] . '/lang/' . $locale . '.yaml'; - if (! Filesystem::has($language_file)) { - continue; - } - - if (($content = Filesystem::read($language_file)) === false) { - throw new RuntimeException('Load file: ' . $language_file . ' - failed!'); - } - - I18n::add(Parser::decode($content, 'yaml'), $locale); + if (! Filesystem::has($language_file)) { + continue; } + + if (($content = Filesystem::read($language_file)) === false) { + throw new RuntimeException('Load file: ' . $language_file . ' - failed!'); + } + + I18n::add(Parser::decode($content, 'yaml'), $locale); } + + return I18n::$dictionary; } /** @@ -225,6 +230,27 @@ class Plugins return $plugins_cache_id; } + /** + * Get plugins list + * + * @access public + */ + public function getPluginsList() : array + { + // Get Plugins List + $plugins_list = []; + + foreach (Filesystem::listContents(PATH['plugins']) as $plugin) { + if ($plugin['type'] !== 'dir') { + continue; + } + + $plugins_list[] = $plugin; + } + + return $plugins_list; + } + /** * Include enabled plugins * diff --git a/flextype/twig/I18nTwigExtension.php b/flextype/twig/I18nTwigExtension.php index 7a5008a4..28883085 100644 --- a/flextype/twig/I18nTwigExtension.php +++ b/flextype/twig/I18nTwigExtension.php @@ -43,8 +43,8 @@ class I18nTwigExtension extends Twig_Extension /** * Translate string */ - public function tr(string $translate, string $locale = null, array $values = []) : string + public function tr(string $translate, array $values = [], string $locale = null) : string { - return I18n::find($translate, $locale, $values); + return I18n::find($translate, $values, $locale); } }