From 48978fb060f0eb1305e079084046a8a49b41e6ba Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Mon, 24 Nov 2014 10:13:51 +0000 Subject: [PATCH] Add a function to check if a plugin exists or it does not. --- src/mibew/libs/classes/Mibew/Plugin/Utils.php | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/mibew/libs/classes/Mibew/Plugin/Utils.php b/src/mibew/libs/classes/Mibew/Plugin/Utils.php index 275c4cff..a1c358c1 100644 --- a/src/mibew/libs/classes/Mibew/Plugin/Utils.php +++ b/src/mibew/libs/classes/Mibew/Plugin/Utils.php @@ -49,22 +49,12 @@ class Utils ); foreach (glob($pattern) as $plugin_file) { - // Build plugin's name and validate it. + // Build plugin's name and make sure the plugin exists. $parts = array_reverse(explode(DIRECTORY_SEPARATOR, $plugin_file)); $plugin_name = $parts[4] . ':' . $parts[1]; - if (!self::isValidPluginName($plugin_name)) { + if (!self::pluginExists($plugin_name)) { continue; } - - // Make sure we found a plugin. - $class_name = self::getPluginClassName($plugin_name); - if (!class_exists($class_name)) { - continue; - } - if (!in_array('Mibew\\Plugin\\PluginInterface', class_implements($class_name))) { - continue; - } - $plugins[] = $plugin_name; } } @@ -101,6 +91,30 @@ class Utils return '\\' . $vendor . '\\Mibew\\Plugin\\' . $short_name . '\\Plugin'; } + /** + * Checks if a plugin with specified name exists. + * + * @param string $plugin_name Name of the plugin to check. + * @return boolean True if the plugin exists and false otherwise. + */ + public static function pluginExists($plugin_name) + { + if (!self::isValidPluginName($plugin_name)) { + return false; + } + + // Make sure plugin class exists and represents a real plugin. + $class_name = self::getPluginClassName($plugin_name); + if (!class_exists($class_name)) { + return false; + } + if (!in_array('Mibew\\Plugin\\PluginInterface', class_implements($class_name))) { + return false; + } + + return true; + } + /** * This class should not be instantiated */