NEW Optimize load of hooks classes (save 1-5Kb of memory)

This commit is contained in:
Laurent Destailleur 2018-04-21 11:24:45 +02:00
parent cc5d54faa5
commit cca4b1cbf5
3 changed files with 23 additions and 17 deletions

View File

@ -4609,7 +4609,7 @@ abstract class CommonObject
$sql.= " FROM ".MAIN_DB_PREFIX.$table_element."_extrafields"; $sql.= " FROM ".MAIN_DB_PREFIX.$table_element."_extrafields";
$sql.= " WHERE fk_object = ".$rowid; $sql.= " WHERE fk_object = ".$rowid;
dol_syslog(get_class($this)."::fetch_optionals get extrafields data for ".$this->table_element, LOG_DEBUG); //dol_syslog(get_class($this)."::fetch_optionals get extrafields data for ".$this->table_element, LOG_DEBUG); // Too verbose
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {

View File

@ -80,22 +80,25 @@ class HookManager
$this->contextarray=array_unique(array_merge($arraycontext,$this->contextarray)); // All contexts are concatenated $this->contextarray=array_unique(array_merge($arraycontext,$this->contextarray)); // All contexts are concatenated
foreach($conf->modules_parts['hooks'] as $module => $hooks) foreach($conf->modules_parts['hooks'] as $module => $hooks) // Loop on each module that brings hooks
{
if ($conf->$module->enabled)
{ {
if (empty($conf->$module->enabled)) continue;
//dol_syslog(get_class($this).'::initHooks module='.$module.' arraycontext='.join(',',$arraycontext));
foreach($arraycontext as $context) foreach($arraycontext as $context)
{ {
if (is_array($hooks)) $arrayhooks=$hooks; // New system if (is_array($hooks)) $arrayhooks=$hooks; // New system
else $arrayhooks=explode(':',$hooks); // Old system (for backward compatibility) else $arrayhooks=explode(':',$hooks); // Old system (for backward compatibility)
if (in_array($context,$arrayhooks) || in_array('all',$arrayhooks)) // We instantiate action class only if hook is required
if (in_array($context, $arrayhooks) || in_array('all', $arrayhooks)) // We instantiate action class only if initialized hook is handled by module
{
// Include actions class overwriting hooks
if (! is_object($this->hooks[$context][$module])) // If set, class was already loaded
{ {
$path = '/'.$module.'/class/'; $path = '/'.$module.'/class/';
$actionfile = 'actions_'.$module.'.class.php'; $actionfile = 'actions_'.$module.'.class.php';
$pathroot = '';
// Include actions class overwriting hooks dol_syslog(get_class($this).'::initHooks Loading hook class for context '.$context.": ".$actionfile, LOG_INFO);
dol_syslog('Loading hook:' . $actionfile, LOG_INFO);
$resaction=dol_include_once($path.$actionfile); $resaction=dol_include_once($path.$actionfile);
if ($resaction) if ($resaction)
{ {
@ -213,20 +216,19 @@ class HookManager
$this->resNbOfHooks++; $this->resNbOfHooks++;
dol_syslog(get_class($this).'::executeHooks a qualified hook was found for method='.$method.' module='.$module." action=".$action." context=".$context);
$modulealreadyexecuted[$module]=$module; // Use the $currentcontext in method to avoid running twice $modulealreadyexecuted[$module]=$module; // Use the $currentcontext in method to avoid running twice
// Clean class (an error may have been set from a previous call of another method for same module/hook) // Clean class (an error may have been set from a previous call of another method for same module/hook)
$actionclassinstance->error=0; $actionclassinstance->error=0;
$actionclassinstance->errors=array(); $actionclassinstance->errors=array();
dol_syslog(get_class($this)."::executeHooks Qualified hook found (hooktype=".$hooktype."). We call method ".$method." of class ".get_class($actionclassinstance).", module=".$module.", action=".$action." context=".$context, LOG_DEBUG);
// Add current context to avoid method execution in bad context, you can add this test in your method : eg if($currentcontext != 'formfile') return; // Add current context to avoid method execution in bad context, you can add this test in your method : eg if($currentcontext != 'formfile') return;
$parameters['currentcontext'] = $context; $parameters['currentcontext'] = $context;
// Hooks that must return int (hooks with type 'addreplace') // Hooks that must return int (hooks with type 'addreplace')
if ($hooktype == 'addreplace') if ($hooktype == 'addreplace')
{ {
dol_syslog("Call method ".$method." of class ".get_class($actionclassinstance).", module=".$module.", hooktype=".$hooktype, LOG_DEBUG);
$resaction += $actionclassinstance->$method($parameters, $object, $action, $this); // $object and $action can be changed by method ($object->id during creation for example or $action to go back to other action for example) $resaction += $actionclassinstance->$method($parameters, $object, $action, $this); // $object and $action can be changed by method ($object->id during creation for example or $action to go back to other action for example)
if ($resaction < 0 || ! empty($actionclassinstance->error) || (! empty($actionclassinstance->errors) && count($actionclassinstance->errors) > 0)) if ($resaction < 0 || ! empty($actionclassinstance->error) || (! empty($actionclassinstance->errors) && count($actionclassinstance->errors) > 0))
{ {

View File

@ -1038,7 +1038,11 @@ class FormFile
} }
else else
{ {
if (! is_object($form)) $form=new Form($this->db); if (! is_object($form))
{
include_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; // The compoent may be included into ajax page that does not include the Form class
$form=new Form($this->db);
}
if (! preg_match('/&id=/', $param) && isset($object->id)) $param.='&id='.$object->id; if (! preg_match('/&id=/', $param) && isset($object->id)) $param.='&id='.$object->id;
$relativepathwihtoutslashend=preg_replace('/\/$/', '', $relativepath); $relativepathwihtoutslashend=preg_replace('/\/$/', '', $relativepath);