1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-06-17 07:10:48 +03:00

Возможность через хуки "делегировать" методы модулей

This commit is contained in:
Mzhelskiy Maxim 2010-03-26 14:08:16 +00:00
parent 308539857b
commit d1e1215878
6 changed files with 95 additions and 27 deletions

View file

@ -203,7 +203,7 @@ $config['module']['security']['key'] = "livestreet_security_key"; // ключ
$config['module']['security']['hash'] = "livestreet_security_key"; // "примесь" к строке, хешируемой в качестве security-кода
// Какие модули должны быть загружены на старте
$config['module']['autoLoad'] = array('Cache','Security','Session','Lang','User','Message');
$config['module']['autoLoad'] = array('Hook','Cache','Security','Session','Lang','User','Message');
/**
* Настройка базы данных
*/

View file

@ -342,11 +342,26 @@ class Engine extends Object {
if (!method_exists($oModule,$sMethod)) {
throw new Exception("The module has no required method: ".$sModuleName.'->'.$sMethod.'()');
}
$sCmd='$result=$oModule->'.$sMethod.'('.$sArgs.');';
$oProfiler=ProfilerSimple::getInstance();
$iTimeId=$oProfiler->Start('callModule',$sModuleName.'->'.$sMethod.'()');
eval($sCmd);
$sModuleName=strtolower($sModuleName);
$aResultHook=array();
if (!in_array($sModuleName,array('plugin','hook'))) {
$aResultHook=$this->Hook_Run('module_'.$sModuleName.'_'.strtolower($sMethod).'_before',$aArgs);
}
if (array_key_exists('delegate_result',$aResultHook)) {
$result=$aResultHook['delegate_result'];
} else {
$result=call_user_func_array(array($oModule,$sMethod),$aArgs);
}
if (!in_array($sModuleName,array('plugin','hook'))) {
$this->Hook_Run('module_'.$sModuleName.'_'.strtolower($sMethod).'_after',array('result'=>$result));
}
$oProfiler->Stop($iTimeId);
return $result;
}
@ -370,7 +385,7 @@ class Engine extends Object {
/**
* Подхватыем делегат модуля (в случае наличия такового)
*/
if($sModuleName!='Plugin') $sModuleName=$this->Plugin_GetDelegate('module',$sModuleName);
if(!in_array($sModuleName,array('Plugin','Hook'))) $sModuleName=$this->Plugin_GetDelegate('module',$sModuleName);
if (isset($this->aModules[$sModuleName])) {
$oModule=$this->aModules[$sModuleName];

View file

@ -28,6 +28,10 @@ abstract class Hook extends Object {
protected function AddHook($sName,$sCallBack,$sClassNameHook,$iPriority=1) {
$this->Hook_AddExecHook($sName,$sCallBack,$iPriority,array('sClassName'=>$sClassNameHook));
}
protected function AddDelegateHook($sName,$sCallBack,$sClassNameHook,$iPriority=1) {
$this->Hook_AddDelegateHook($sName,$sCallBack,$iPriority,array('sClassName'=>$sClassNameHook));
}
abstract public function RegisterHook();

View file

@ -44,6 +44,7 @@ class LsHook extends Module {
}
public function Add($sName,$sType,$sCallBack,$iPriority=1,$aParams=array()) {
$sName=strtolower($sName);
$sType=strtolower($sType);
if (!in_array($sType,array('module','hook','function'))) {
return false;
@ -63,34 +64,82 @@ class LsHook extends Module {
return $this->Add($sName,'hook',$sCallBack,$iPriority,$aParams);
}
public function Run($sName,$aVars=array()) {
public function AddDelegateModule($sName,$sCallBack,$iPriority=1) {
return $this->Add($sName,'module',$sCallBack,$iPriority,array('delegate'=>true));
}
public function AddDelegateFunction($sName,$sCallBack,$iPriority=1) {
return $this->Add($sName,'function',$sCallBack,$iPriority,array('delegate'=>true));
}
public function AddDelegateHook($sName,$sCallBack,$iPriority=1,$aParams=array()) {
$aParams['delegate']=true;
return $this->Add($sName,'hook',$sCallBack,$iPriority,$aParams);
}
public function Run($sName,$aVars=array()) {
$result=array();
$sName=strtolower($sName);
if (isset($this->aHooks[$sName])) {
$aHookNum=array();
for ($i=0;$i<count($this->aHooks[$sName]);$i++) {
$aHookNum[$i]=$this->aHooks[$sName][$i]['priority'];
$aHookNumDelegate=array();
/**
* Все хуки делим на обычные(exec) и делигирующие(delegate)
*/
for ($i=0;$i<count($this->aHooks[$sName]);$i++) {
if (isset($this->aHooks[$sName][$i]['params']['delegate']) and $this->aHooks[$sName][$i]['params']['delegate']) {
$aHookNumDelegate[$i]=$this->aHooks[$sName][$i]['priority'];
} else {
$aHookNum[$i]=$this->aHooks[$sName][$i]['priority'];
}
}
arsort($aHookNum,SORT_NUMERIC);
arsort($aHookNumDelegate,SORT_NUMERIC);
/**
* Сначала запускаем на выполнение простые
*/
foreach ($aHookNum as $iKey => $iPr) {
$aHook=$this->aHooks[$sName][$iKey];
switch ($aHook['type']) {
case 'module':
call_user_func_array(array($this,$aHook['callback']),array($aVars));
break;
case 'function':
call_user_func_array($aHook['callback'],array($aVars));
break;
case 'hook':
if (isset($aHook['params']['sClassName']) and class_exists($aHook['params']['sClassName'])) {
$oHook=new $aHook['params']['sClassName'];
call_user_func_array(array($oHook,$aHook['callback']),array($aVars));
}
break;
default:
break;
}
$this->RunType($aHook,$aVars);
}
/**
* Теперь запускаем делигирующие
* Делегирующий хук должен вернуть результат в формате:
*
*/
foreach ($aHookNumDelegate as $iKey => $iPr) {
$aHook=$this->aHooks[$sName][$iKey];
$result=array(
'delegate_result'=>$this->RunType($aHook,$aVars)
);
/**
* На данный момент только один хук может быть делегирующим
*/
break;
}
}
}
return $result;
}
protected function RunType($aHook,$aVars) {
$result=null;
switch ($aHook['type']) {
case 'module':
$result=call_user_func_array(array($this,$aHook['callback']),array($aVars));
break;
case 'function':
$result=call_user_func_array($aHook['callback'],array($aVars));
break;
case 'hook':
if (isset($aHook['params']['sClassName']) and class_exists($aHook['params']['sClassName'])) {
$oHook=new $aHook['params']['sClassName'];
$result=call_user_func_array(array($oHook,$aHook['callback']),array($aVars));
}
break;
default:
break;
}
return $result;
}
}
?>

View file

@ -49,7 +49,7 @@ ul.autocompleter-choices li.autocompleter-selected span.autocompleter-queried
input.autocompleter-loading
{
background-image: url(../img/spinner.gif);
background-image: url(../images/spinner.gif);
background-repeat: no-repeat;
background-position: right 50%;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB