From e9bbd1e0b0328ad8e3f27c61109a90656aa4f95e Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 10 May 2019 17:17:06 -0600 Subject: [PATCH] Optimizations for Plugin/Theme loading --- system/src/Grav/Common/Plugins.php | 31 ++++++++++++++++++------------ system/src/Grav/Common/Themes.php | 3 +-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/system/src/Grav/Common/Plugins.php b/system/src/Grav/Common/Plugins.php index 856f0dcdf..87908517a 100644 --- a/system/src/Grav/Common/Plugins.php +++ b/system/src/Grav/Common/Plugins.php @@ -185,24 +185,31 @@ class Plugins extends Iterator $grav = Grav::instance(); $locator = $grav['locator']; - $filePath = $locator->findResource('plugins://' . $name . DS . $name . PLUGIN_EXT); - if (!is_file($filePath)) { + $file = $locator->findResource('plugins://' . $name . DS . $name . PLUGIN_EXT); + + if (is_file($file)) { + // Local variables available in the file: $grav, $config, $name, $file + $class = include_once $file; + + $pluginClassFormat = [ + 'Grav\\Plugin\\' . ucfirst($name). 'Plugin', + 'Grav\\Plugin\\' . Inflector::camelize($name) . 'Plugin' + ]; + + foreach ($pluginClassFormat as $pluginClass) { + if (class_exists($pluginClass)) { + $class = new $pluginClass($name, $grav); + break; + } + } + } else { $grav['log']->addWarning( sprintf("Plugin '%s' enabled but not found! Try clearing cache with `bin/grav clear-cache`", $name) ); return null; } - require_once $filePath; - - $pluginClassName = 'Grav\\Plugin\\' . ucfirst($name) . 'Plugin'; - if (!class_exists($pluginClassName)) { - $pluginClassName = 'Grav\\Plugin\\' . Inflector::camelize($name) . 'Plugin'; - if (!class_exists($pluginClassName)) { - throw new \RuntimeException(sprintf("Plugin '%s' class not found! Try reinstalling this plugin.", $name)); - } - } - return new $pluginClassName($name, $grav); + return $class; } } diff --git a/system/src/Grav/Common/Themes.php b/system/src/Grav/Common/Themes.php index 8c0e08b6e..2d12b3399 100644 --- a/system/src/Grav/Common/Themes.php +++ b/system/src/Grav/Common/Themes.php @@ -196,8 +196,7 @@ class Themes extends Iterator foreach ($themeClassFormat as $themeClass) { if (class_exists($themeClass)) { - $themeClassName = $themeClass; - $class = new $themeClassName($grav, $config, $name); + $class = new $themeClass($grav, $config, $name); break; } }