Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature stable 3 3 0 10514 plugin resilience #10520

Open
wants to merge 2 commits into
base: stable-3_3_0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion classes/plugins/HookRegistry.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,18 @@ static function call($hookName, $args = null) {
ksort($hooks[$hookName], SORT_NUMERIC);
foreach ($hooks[$hookName] as $priority => $hookList) {
foreach ($hookList as $hook) {
if ($result = call_user_func($hook, $hookName, $args)) return true;
try {
if ($result = call_user_func($hook, $hookName, $args)) return true;
} catch (Throwable $e) {
$trace = $e->getTrace();
foreach ($trace as $frame) {
if (is_subclass_of($frame['class'] ?? null, 'Plugin')) {
error_log('The plugin hook failed: ' . $e);
return false;
}
}
throw $e;
}
}
}
}
Expand Down
48 changes: 40 additions & 8 deletions classes/plugins/PluginRegistry.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,21 @@ static function getAllPlugins() {
* @return boolean True IFF the plugin was registered successfully
*/
static function register($category, $plugin, $path, $mainContextId = null) {
$pluginName = $plugin->getName();
try {
$pluginName = $plugin->getName();
} catch (Throwable $e) {
error_log('The plugin getName() failed: ' . $e);
return false;
}
$plugins =& self::getPlugins();

// Allow the plugin to register.
if (!$plugin->register($category, $path, $mainContextId)) return false;
try {
if (!$plugin->register($category, $path, $mainContextId)) return false;
} catch (Throwable $e) {
error_log('The plugin failed to be registered: ' . $e);
return false;
}

// If the plugin was already loaded, do not load it again.
if (isset($plugins[$category][$pluginName])) return false;
Expand Down Expand Up @@ -113,7 +123,11 @@ static function loadCategory ($category, $enabledOnly = false, $mainContextId =
$file = $product->getProduct();
$plugin = self::_instantiatePlugin($category, $categoryDir, $file, $product->getProductClassname());
if ($plugin instanceof Plugin) {
$plugins[$plugin->getSeq()]["$categoryDir/$file"] = $plugin;
try {
$plugins[$plugin->getSeq()]["$categoryDir/$file"] = $plugin;
} catch (Throwable $e) {
error_log('The plugin getSeq() failed: ' . $e);
}
}
}
} else {
Expand All @@ -125,7 +139,11 @@ static function loadCategory ($category, $enabledOnly = false, $mainContextId =
if ($file == '.' || $file == '..') continue;
$plugin = self::_instantiatePlugin($category, $categoryDir, $file);
if ($plugin && is_object($plugin)) {
$plugins[$plugin->getSeq()]["$categoryDir/$file"] = $plugin;
try {
$plugins[$plugin->getSeq()]["$categoryDir/$file"] = $plugin;
} catch (Throwable $e) {
error_log('The plugin getSeq() failed: ' . $e);
}
}
}
closedir($handle);
Expand Down Expand Up @@ -176,7 +194,12 @@ static function loadPlugin($category, $pathName, $mainContextId = null) {
$pluginPath = PLUGINS_PREFIX . $category . '/' . $pathName;
if (!is_dir($pluginPath) || !file_exists($pluginPath . '/index.php')) return null;

$plugin = @include("$pluginPath/index.php");
try {
$plugin = @include("$pluginPath/index.php");
} catch (Throwable $e) {
error_log('The plugin failed to be loaded: ' . $e);
return null;
}
if (!is_object($plugin)) return null;

self::register($category, $plugin, $pluginPath, $mainContextId);
Expand Down Expand Up @@ -238,7 +261,12 @@ static function _instantiatePlugin($category, $categoryDir, $file, $classToCheck
// compatibility.
$pluginWrapper = "$pluginPath/index.php";
if (file_exists($pluginWrapper)) {
$plugin = include($pluginWrapper);
try {
$plugin = include($pluginWrapper);
} catch (Throwable $e) {
error_log('The plugin failed to be loaded: ' . $e);
return null;
}
assert(is_a($plugin, $classToCheck ?: 'Plugin'));
return $plugin;
} else {
Expand All @@ -248,12 +276,16 @@ static function _instantiatePlugin($category, $categoryDir, $file, $classToCheck
if (file_exists("$pluginPath/$pluginClassFile")) {
// Try to instantiate the plug-in class.
$pluginPackage = 'plugins.'.$category.'.'.$file;
$plugin = instantiate($pluginPackage.'.'.$pluginClassName, $pluginClassName, $pluginPackage, 'register');
try {
$plugin = instantiate($pluginPackage.'.'.$pluginClassName, $pluginClassName, $pluginPackage, 'register');
} catch (Throwable $e) {
error_log('The plugin failed to be loaded: ' . $e);
return null;
}
assert(is_a($plugin, $classToCheck ?: 'Plugin'));
return $plugin;
}
}
return null;
}
}