From 20297deaec5edc393688be36787ad188a6a1fc99 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Thu, 12 Oct 2017 12:11:55 +0200 Subject: [PATCH] Pico::loadLocalPlugins(): Don't load plugins case-insensitive The performance vs. error-proneness trade-off doesn't justify this additional complexity. This is Pico 2.0, we always try to minimize BC-breaking changes, but we're breaking BC anyway by loading plugins from plugins//.php only... --- lib/Pico.php | 83 ++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 52 deletions(-) diff --git a/lib/Pico.php b/lib/Pico.php index 829d749..6ffd810 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -540,56 +540,6 @@ class Pico */ protected function loadLocalPlugins() { - $pluginsLowered = array_change_key_case($this->plugins, CASE_LOWER); - - $pluginFiles = array(); - $files = scandir($this->getPluginsDir()) ?: array(); - foreach ($files as $file) { - if ($file[0] === '.') { - continue; - } - - if (is_dir($this->getPluginsDir() . $file)) { - $className = preg_replace('/^[0-9]+-/', '', $file); - $classNameLowered = strtolower($className); - - if (isset($pluginsLowered[$classNameLowered])) { - continue; - } - - if (file_exists($this->getPluginsDir() . $file . '/' . $className . '.php')) { - $pluginFiles[$className] = $file . '/' . $className . '.php'; - } else { - $subdirFiles = $this->getFilesGlob($this->getPluginsDir() . $file . '/?*.php', self::SORT_NONE); - foreach ($subdirFiles as $subdirFile) { - $subdirFile = basename($subdirFile, '.php'); - if ($classNameLowered === strtolower($subdirFile)) { - $pluginFiles[$className] = $file . '/' . $subdirFile . '.php'; - break; - } - } - } - - if (!isset($pluginFiles[$className])) { - throw new RuntimeException( - "Unable to load plugin '" . $className . "' from " - . "'" . $file . "/" . $className . ".php': File not found" - ); - } - } elseif (substr($file, -4) === '.php') { - $className = preg_replace('/^[0-9]+-/', '', substr($file, 0, -4)); - $classNameLowered = strtolower($className); - - if (isset($pluginsLowered[$classNameLowered])) { - continue; - } - - $pluginFiles[$className] = $file; - } else { - throw new RuntimeException("Unable to load plugin from '" . $file . "': Not a valid plugin file"); - } - } - // scope isolated require() $includeClosure = function ($pluginFile) { require($pluginFile); @@ -598,7 +548,34 @@ class Pico $includeClosure = $includeClosure->bindTo(null); } - foreach ($pluginFiles as $className => $pluginFile) { + $pluginFiles = array(); + $files = scandir($this->getPluginsDir()) ?: array(); + foreach ($files as $file) { + if ($file[0] === '.') { + continue; + } + + $className = $pluginFile = null; + if (is_dir($this->getPluginsDir() . $file)) { + $className = preg_replace('/^[0-9]+-/', '', $file); + $pluginFile = $file . '/' . $className . '.php'; + + if (!file_exists($this->getPluginsDir() . $pluginFile)) { + throw new RuntimeException( + "Unable to load plugin '" . $className . "' from '" . $pluginFile . "': File not found" + ); + } + } elseif (substr($file, -4) === '.php') { + $className = preg_replace('/^[0-9]+-/', '', substr($file, 0, -4)); + $pluginFile = $file; + } else { + throw new RuntimeException("Unable to load plugin from '" . $file . "': Not a valid plugin file"); + } + + if (isset($this->plugins[$className])) { + continue; + } + $includeClosure($this->getPluginsDir() . $pluginFile); if (class_exists($className, false)) { @@ -614,7 +591,9 @@ class Pico } } } else { - throw new RuntimeException("Unable to load plugin '" . $className . "' from '" . $pluginFile . "'"); + throw new RuntimeException( + "Unable to load plugin '" . $className . "' from '" . $pluginFile . "': Plugin class not found" + ); } } }