From ad8ccc049f8d8c750fb7ebee23375f8bf69c5dde Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 15 Sep 2015 21:43:23 -0700 Subject: [PATCH] CRM-14707 - CRM_Utils_Hook::runHooks - Cache function names --- CRM/Utils/Hook.php | 108 +++++++++++++++++++++++++++------------------ 1 file changed, 65 insertions(+), 43 deletions(-) diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index cf6c688d89..2b38e59cba 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -67,6 +67,11 @@ abstract class CRM_Utils_Hook { */ private $commonCiviModules = array(); + /** + * @var CRM_Utils_Cache_Interface + */ + protected $cache; + /** * Constructor and getter for the singleton instance. * @@ -84,6 +89,14 @@ abstract class CRM_Utils_Hook { return self::$_singleton; } + public function __construct() { + $this->cache = CRM_Utils_Cache::create(array( + 'name' => 'hooks', + 'type' => array('ArrayCache'), + 'prefetch' => 1, + )); + } + /** * Invoke hooks. * @@ -186,51 +199,60 @@ abstract class CRM_Utils_Hook { // to reproduce the issue are pretty intricate. $result = array(); - if ($civiModules !== NULL) { - foreach ($civiModules as $module) { - $fnName = "{$module}_{$fnSuffix}"; - if (function_exists($fnName)) { - $fResult = array(); - switch ($numParams) { - case 0: - $fResult = $fnName(); - break; - - case 1: - $fResult = $fnName($arg1); - break; - - case 2: - $fResult = $fnName($arg1, $arg2); - break; - - case 3: - $fResult = $fnName($arg1, $arg2, $arg3); - break; - - case 4: - $fResult = $fnName($arg1, $arg2, $arg3, $arg4); - break; - - case 5: - $fResult = $fnName($arg1, $arg2, $arg3, $arg4, $arg5); - break; - - case 6: - $fResult = $fnName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6); - break; - - default: - CRM_Core_Error::fatal(ts('Invalid hook invocation')); - break; - } - - if (!empty($fResult) && - is_array($fResult) - ) { - $result = array_merge($result, $fResult); + $fnNames = $this->cache->get($fnSuffix); + if (!is_array($fnNames)) { + $fnNames = array(); + if ($civiModules !== NULL) { + foreach ($civiModules as $module) { + $fnName = "{$module}_{$fnSuffix}"; + if (function_exists($fnName)) { + $fnNames[] = $fnName; } } + $this->cache->set($fnSuffix, $fnNames); + } + } + + foreach ($fnNames as $fnName) { + $fResult = array(); + switch ($numParams) { + case 0: + $fResult = $fnName(); + break; + + case 1: + $fResult = $fnName($arg1); + break; + + case 2: + $fResult = $fnName($arg1, $arg2); + break; + + case 3: + $fResult = $fnName($arg1, $arg2, $arg3); + break; + + case 4: + $fResult = $fnName($arg1, $arg2, $arg3, $arg4); + break; + + case 5: + $fResult = $fnName($arg1, $arg2, $arg3, $arg4, $arg5); + break; + + case 6: + $fResult = $fnName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6); + break; + + default: + CRM_Core_Error::fatal(ts('Invalid hook invocation')); + break; + } + + if (!empty($fResult) && + is_array($fResult) + ) { + $result = array_merge($result, $fResult); } } -- 2.25.1