1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-06-28 20:45:00 +03:00

Поддержка наследований классов движка плагинами через $aInherits

This commit is contained in:
Mzhelskiy Maxim 2010-05-28 15:20:55 +00:00
parent f356566968
commit 0feaef7197
4 changed files with 100 additions and 1 deletions

View file

@ -637,5 +637,36 @@ function __autoload($sClassName) {
require_once($sFileClass);
}
}
/**
* Загрузка цепочки наследуемых классов
*/
if (preg_match("/^Plugin(\w+)\_Inherits\_([\w\_]+)$/i",$sClassName,$aMatch)) {
$sPlugin=$aMatch[1];
$sInheritClass=$aMatch[2];
$sParentClass=Engine::getInstance()->Plugin_GetParentInherit($sInheritClass);
class_alias($sParentClass,$sClassName);
}
/**
* Загрузка класса экшена
*/
if (preg_match("/^Action(\w+)$/i",$sClassName,$aMatch)) {
$sFileClass=Config::get('path.root.server').'/classes/actions/'.$sClassName.'.class.php';
if (file_exists($sFileClass)) {
require_once($sFileClass);
}
}
/**
* Загрузка класса экшена плагина
*/
if (preg_match("/^Plugin(\w+)\_Action(\w+)$/i",$sClassName,$aMatch)) {
$sFileClass=Config::get('path.root.server').'/plugins/'.strtolower($aMatch[1]).'/classes/actions/Action'.$aMatch[2].'.class.php';
if (file_exists($sFileClass)) {
require_once($sFileClass);
}
}
}
?>

View file

@ -38,6 +38,12 @@ abstract class Plugin extends Object {
* @var array
*/
protected $aDelegates=array();
/**
* Массив наследуемых классов плагина
*
* @var array
*/
protected $aInherits=array();
public function __construct() {
@ -96,10 +102,25 @@ abstract class Plugin extends Object {
}
}
}
if(is_array($this->aInherits) and count($this->aInherits)) {
foreach ($this->aInherits as $sObjectName=>$aParams) {
if(is_array($aParams) and count($aParams)) {
foreach ($aParams as $sFrom=>$sTo) {
if (is_int($sFrom)) {
$sFrom=$sTo;
$sTo=null;
}
$this->DelegateWrapper($sObjectName,$sFrom,$sTo);
$this->Plugin_Inherit($sFrom,$sTo,get_class($this));
}
}
}
}
}
public function DelegateWrapper($sObjectName,$sFrom,$sTo=null) {
public function DelegateWrapper($sObjectName,$sFrom,&$sTo=null) {
/**
* Если не указан делегат TO, считаем, что делегатом является
* одноименный объект текущего плагина

View file

@ -406,4 +406,10 @@ if (!function_exists('array_intersect_key')) {
}
}
}
if (!function_exists('class_alias')) {
function class_alias($original, $alias) {
eval('abstract class ' . $alias . ' extends ' . $original . ' {}');
}
}
?>

View file

@ -58,6 +58,13 @@ class ModulePlugin extends Module {
'template' => array()
);
/**
* Стек наследований
*
* @var array
*/
protected $aInherits=array();
/**
* Инициализация модуля
*
@ -319,6 +326,40 @@ class ModulePlugin extends Module {
);
}
/**
* Добавляет в стек наследника класса
*
* @param string $sFrom
* @param string $sTo
* @param string $sSign
*/
public function Inherit($sFrom,$sTo,$sSign=__CLASS__) {
if(!is_string($sSign) or !strlen($sSign)) return null;
if(!$sFrom or !$sTo) return null;
$this->aInherits[trim(strtolower($sFrom))]['items'][]=array(
'inherit'=>trim($sTo),
'sign'=>$sSign
);
$this->aInherits[trim(strtolower($sFrom))]['position']=count($this->aInherits[trim(strtolower($sFrom))]['items'])-1;
}
/**
* Получает следующего родителя у наследника.
* ВНИМАНИЕ! Данный метод нужно вызвать только из __autoload()
*
* @param unknown_type $sFrom
* @return unknown
*/
public function GetParentInherit($sFrom) {
$sKey=strtolower($sFrom);
if (!isset($this->aInherits[$sKey]['items']) or count($this->aInherits[$sKey]['items'])<=1 or $this->aInherits[$sKey]['position']<1) {
return $sFrom;
}
$this->aInherits[$sKey]['position']--;
return $this->aInherits[$sKey]['items'][$this->aInherits[$sKey]['position']]['inherit'];
}
/**
* Возвращает делегат модуля, экшена, сущности.
* Если делегат не определен, отдает переданный в качестве sender`a параметр