From 243c835c24fa59e24365ec6db7a3f317ceedca2e Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 31 Mar 2020 00:36:33 -0600 Subject: [PATCH] Minor performance improvement for PluginManager. Suggested by @tobias-kuendig in #4337, implemented because normalizeIdentifier() would be called more frequently by merging #4838. --- modules/system/classes/PluginManager.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/modules/system/classes/PluginManager.php b/modules/system/classes/PluginManager.php index 0985f2c32..4d8894c92 100644 --- a/modules/system/classes/PluginManager.php +++ b/modules/system/classes/PluginManager.php @@ -38,6 +38,11 @@ class PluginManager */ protected $pathMap = []; + /** + * @var array A map of normalized plugin identifiers [lowercase.identifier => Normalized.Identifier] + */ + protected $normalizedMap = []; + /** * @var bool Check if all plugins have had the register() method called. */ @@ -157,6 +162,7 @@ class PluginManager $this->plugins[$classId] = $classObj; $this->pathMap[$classId] = $path; + $this->normalizedMap[strtolower($classId)] = $classId; return $classObj; } @@ -441,15 +447,16 @@ class PluginManager /** * Takes a human plugin code (acme.blog) and makes it authentic (Acme.Blog) - * @param string $id + * Returns the provided identifier if a match isn't found + * + * @param string $identifier * @return string */ public function normalizeIdentifier($identifier) { - foreach ($this->plugins as $id => $object) { - if (strtolower($id) == strtolower($identifier)) { - return $id; - } + $id = strtolower($identifier); + if (isset($this->normalizedMap[$id])) { + return $this->normalizedMap[$id]; } return $identifier;