From 76c1a324aeaf5ab6b0230a18d355ee04bbc575f0 Mon Sep 17 00:00:00 2001 From: Thomas Steur <thomas.steur@googlemail.com> Date: Fri, 20 Jun 2014 04:33:59 +0200 Subject: [PATCH] this should cache all "find plugin components" calls when trying to find components in the plugin directory --- core/Plugin.php | 63 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/core/Plugin.php b/core/Plugin.php index 967d23b0ec..e938a9a1fe 100644 --- a/core/Plugin.php +++ b/core/Plugin.php @@ -105,6 +105,8 @@ class Plugin */ private $pluginInformation; + private static $cachedPluginComponents = array(); + /** * Constructor. * @@ -293,22 +295,29 @@ class Plugin */ public function findComponent($componentName, $expectedSubclass) { - $componentFile = sprintf('%s/plugins/%s/%s.php', PIWIK_INCLUDE_PATH, $this->pluginName, $componentName); + $cacheKey = $componentName . $expectedSubclass; + $klassName = $this->getCachedComponent($cacheKey); - if (!file_exists($componentFile)) { - return; - } + if (null === $klassName) { + $componentFile = sprintf('%s/plugins/%s/%s.php', PIWIK_INCLUDE_PATH, $this->pluginName, $componentName); - $klassName = sprintf('Piwik\\Plugins\\%s\\%s', $this->pluginName, $componentName); + if (!file_exists($componentFile)) { + return; + } - if (!class_exists($klassName)) { - return; - } + $klassName = sprintf('Piwik\\Plugins\\%s\\%s', $this->pluginName, $componentName); + + if (!class_exists($klassName)) { + return; + } - if (!empty($expectedSubclass) && !is_subclass_of($klassName, $expectedSubclass)) { - Log::warning(sprintf('Cannot use component %s for plugin %s, class %s does not extend %s', - $componentName, $this->pluginName, $klassName, $expectedSubclass)); - return; + if (!empty($expectedSubclass) && !is_subclass_of($klassName, $expectedSubclass)) { + Log::warning(sprintf('Cannot use component %s for plugin %s, class %s does not extend %s', + $componentName, $this->pluginName, $klassName, $expectedSubclass)); + return; + } + + $this->cacheComponent($cacheKey, $klassName); } return new $klassName; @@ -316,6 +325,13 @@ class Plugin public function findMultipleComponents($directoryWithinPlugin, $expectedSubclass) { + $cacheKey = $directoryWithinPlugin . $expectedSubclass; + $cached = $this->getCachedComponent($cacheKey); + + if (null !== $cached) { + return $cached; + } + $components = array(); $files = Filesystem::globr(PIWIK_INCLUDE_PATH . '/plugins/' . $this->pluginName .'/' . $directoryWithinPlugin, '*.php'); @@ -340,9 +356,32 @@ class Plugin $components[] = $klassName; } + $this->cacheComponent($cacheKey, $components); + return $components; } + private function getCachedComponent($cacheKey) + { + if (empty(self::$cachedPluginComponents[$this->pluginName])) { + return null; + } + + if (!array_key_exists($cacheKey, self::$cachedPluginComponents[$this->pluginName])) { + return null; + } + + return self::$cachedPluginComponents[$this->pluginName][$cacheKey]; + } + + private function cacheComponent($cacheKey, $foundComponents) + { + if (!array_key_exists($this->pluginName, self::$cachedPluginComponents)) { + self::$cachedPluginComponents[$this->pluginName] = array(); + } + + self::$cachedPluginComponents[$this->pluginName][$cacheKey] = $foundComponents; + } /** * Detect whether there are any missing dependencies. -- GitLab