Skip to content
Extraits de code Groupes Projets
Valider 76c1a324 rédigé par Thomas Steur's avatar Thomas Steur
Parcourir les fichiers

this should cache all "find plugin components" calls when trying to find...

this should cache all "find plugin components" calls when trying to find components in the plugin directory
parent 98101dfa
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -105,6 +105,8 @@ class Plugin ...@@ -105,6 +105,8 @@ class Plugin
*/ */
private $pluginInformation; private $pluginInformation;
private static $cachedPluginComponents = array();
/** /**
* Constructor. * Constructor.
* *
...@@ -293,22 +295,29 @@ class Plugin ...@@ -293,22 +295,29 @@ class Plugin
*/ */
public function findComponent($componentName, $expectedSubclass) 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)) { if (null === $klassName) {
return; $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)) { $klassName = sprintf('Piwik\\Plugins\\%s\\%s', $this->pluginName, $componentName);
return;
} if (!class_exists($klassName)) {
return;
}
if (!empty($expectedSubclass) && !is_subclass_of($klassName, $expectedSubclass)) { if (!empty($expectedSubclass) && !is_subclass_of($klassName, $expectedSubclass)) {
Log::warning(sprintf('Cannot use component %s for plugin %s, class %s does not extend %s', Log::warning(sprintf('Cannot use component %s for plugin %s, class %s does not extend %s',
$componentName, $this->pluginName, $klassName, $expectedSubclass)); $componentName, $this->pluginName, $klassName, $expectedSubclass));
return; return;
}
$this->cacheComponent($cacheKey, $klassName);
} }
return new $klassName; return new $klassName;
...@@ -316,6 +325,13 @@ class Plugin ...@@ -316,6 +325,13 @@ class Plugin
public function findMultipleComponents($directoryWithinPlugin, $expectedSubclass) public function findMultipleComponents($directoryWithinPlugin, $expectedSubclass)
{ {
$cacheKey = $directoryWithinPlugin . $expectedSubclass;
$cached = $this->getCachedComponent($cacheKey);
if (null !== $cached) {
return $cached;
}
$components = array(); $components = array();
$files = Filesystem::globr(PIWIK_INCLUDE_PATH . '/plugins/' . $this->pluginName .'/' . $directoryWithinPlugin, '*.php'); $files = Filesystem::globr(PIWIK_INCLUDE_PATH . '/plugins/' . $this->pluginName .'/' . $directoryWithinPlugin, '*.php');
...@@ -340,9 +356,32 @@ class Plugin ...@@ -340,9 +356,32 @@ class Plugin
$components[] = $klassName; $components[] = $klassName;
} }
$this->cacheComponent($cacheKey, $components);
return $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. * Detect whether there are any missing dependencies.
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter