Skip to content

Commit

Permalink
#10514 Included callbacks implemented in classes which are derived fr…
Browse files Browse the repository at this point in the history
…om the Plugin class and added comments
  • Loading branch information
jonasraoni committed Jan 16, 2025
1 parent ed1711c commit c2521bb
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions classes/plugins/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,29 @@ public static function run(string $hookName, array $args = []): bool
return static::ABORT;
}
} catch (Throwable $e) {
/**
* This is an attempt to improve the application's resilience against errors inside plugins
* If an exception happens at a hook handler which was implemented inside a plugin (callbacks implemented inside a plugin-derived class or located inside the folders "lib/pkp/plugins" or "plugins"), the exception will be logged and the hook flow will continue
* @see https://github.com/pkp/pkp-lib/discussions/9083
*/
$pluginDirectories = [realpath(BASE_SYS_DIR . '/' . PKP_LIB_PATH . '/plugins'), realpath(BASE_SYS_DIR . '/plugins')];
foreach ($e->getTrace() as $stackFrame) {
$filename = (($class = $stackFrame['class'] ?? null) ? (new ReflectionClass($class))->getFileName() : null)
// If the code was implemented inside a plugin class, let the hook flow continue
if (is_subclass_of($class = $stackFrame['class'] ?? null, Plugin::class)) {
error_log("Plugin {$class} failed to handle the hook {$hookName}\n{$e}");
continue 2;
}

// Attempt to recover the file where the callback was implemented
$filename = ($class ? (new ReflectionClass($class))->getFileName() : null)
?? (($function = $stackFrame['function'] ?? null) ? (new ReflectionFunction($function))->getFileName() : null)
?? $stackFrame['file']
?? null;

if (!$filename) {
continue;
}

// If the code was implemented inside a plugin folder, let the hook flow continue
$filename = realpath($filename);
foreach ($pluginDirectories as $pluginDirectory) {
if (strpos($filename, $pluginDirectory) === 0) {
Expand All @@ -185,6 +197,7 @@ public static function run(string $hookName, array $args = []): bool
}
}
}

throw $e;
}
}
Expand Down

0 comments on commit c2521bb

Please sign in to comment.