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

поддержка хуков

This commit is contained in:
Mzhelskiy Maxim 2009-05-09 17:04:04 +00:00
parent 2c6d39277a
commit de16c8ad79
5 changed files with 158 additions and 0 deletions

View file

@ -139,6 +139,26 @@ class Engine extends Object {
closedir($hDirConfig);
}
}
/**
* Регистрирует хуки из /classes/hooks/
*
*/
public function InitHooks() {
$sDirHooks=DIR_SERVER_ROOT.'/classes/hooks/';
if ($hDir = opendir($sDirHooks)) {
while (false !== ($sFile = readdir($hDir))) {
if ($sFile !='.' and $sFile !='..' and is_file($sDirHooks.$sFile)) {
if (preg_match("/^Hook([\w]+)\.class\.php$/i",$sFile,$aMatch)) {
require_once($sDirHooks.$sFile);
$sClassName='Hook'.$aMatch[1];
$oHook=new $sClassName;
$oHook->RegisterHook();
}
}
}
closedir($hDir);
}
}
/**
* Вызывает метод нужного модуля
*

View file

@ -0,0 +1,38 @@
<?
/*-------------------------------------------------------
*
* LiveStreet Engine Social Networking
* Copyright © 2008 Mzhelskiy Maxim
*
*--------------------------------------------------------
*
* Official site: www.livestreet.ru
* Contact e-mail: rus.engine@gmail.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/
/**
* Абстракция модуля, от которой наследуются все модули
*
*/
abstract class Hook extends Object {
public function __construct() {
}
protected function AddHook($sName,$sCallBack,$sClassNameHook,$iPriority=1) {
$this->Hook_AddExecHook($sName,$sCallBack,$iPriority,array('sClassName'=>$sClassNameHook));
}
abstract public function RegisterHook();
public function __call($sName,$aArgs) {
return Engine::getInstance()->_CallModule($sName,$aArgs);
}
}
?>

View file

@ -24,6 +24,7 @@ set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__));
require_once("Object.class.php");
require_once("Action.class.php");
require_once("Block.class.php");
require_once("Hook.class.php");
require_once("Module.class.php");
require_once("Engine.class.php");
@ -96,6 +97,7 @@ class Router extends Object {
public function Exec() {
$this->oEngine=Engine::getInstance();
$this->oEngine->InitModules();
$this->oEngine->InitHooks();
$this->ExecAction();
$this->AssignVars();
$this->Viewer_VarAssign();

2
classes/hooks/.htaccess Normal file
View file

@ -0,0 +1,2 @@
Order Deny,Allow
Deny from all

View file

@ -0,0 +1,96 @@
<?
/*-------------------------------------------------------
*
* LiveStreet Engine Social Networking
* Copyright © 2008 Mzhelskiy Maxim
*
*--------------------------------------------------------
*
* Official site: www.livestreet.ru
* Contact e-mail: rus.engine@gmail.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/
/**
* Модуль поддержки хуков(hooks)
*
*/
class LsHook extends Module {
/**
* Содержит список хуков
*
* @var array( 'name' => array(
* array(
* 'type' => 'module' | 'hook' | 'function',
* 'callback' => 'callback_name',
* 'priority' => 1,
* 'params' => array()
* ),
* ),
* )
*/
protected $aHooks=array();
/**
* Инициализация модуля
*
*/
public function Init() {
}
public function Add($sName,$sType,$sCallBack,$iPriority=1,$aParams=array()) {
$sType=strtolower($sType);
if (!in_array($sType,array('module','hook','function'))) {
return false;
}
$this->aHooks[$sName][]=array('type'=>$sType,'callback'=>$sCallBack,'params'=>$aParams,'priority'=>(int)$iPriority);
}
public function AddExecModule($sName,$sCallBack,$iPriority=1) {
return $this->Add($sName,'module',$sCallBack,$iPriority);
}
public function AddExecFunction($sName,$sCallBack,$iPriority=1) {
return $this->Add($sName,'function',$sCallBack,$iPriority);
}
public function AddExecHook($sName,$sCallBack,$iPriority=1,$aParams=array()) {
return $this->Add($sName,'hook',$sCallBack,$iPriority,$aParams);
}
public function Run($sName,$aVars) {
if (isset($this->aHooks[$sName])) {
$aHookNum=array();
for ($i=0;$i<count($this->aHooks[$sName]);$i++) {
$aHookNum[$i]=$this->aHooks[$sName][$i]['priority'];
}
arsort($aHookNum,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;
}
}
}
}
}
?>