1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-05-17 08:18:18 +03:00

переработан механизм регистрации евентов и их запуск, тперь можно регистрировать евент по регулярному выражению, причем как евента, так и всех параметров

This commit is contained in:
Mzhelskiy Maxim 2008-10-05 16:22:25 +00:00
parent 2768b86773
commit 315fcb5f1c
2 changed files with 120 additions and 90 deletions

View file

@ -113,6 +113,21 @@ class ActionBlog extends Action {
$this->AddEvent('new','EventNew');
$this->AddEvent('add','EventAddBlog');
$this->AddEvent('edit','EventEditBlog');
$this->AddEventPreg('/^(\d+)\.html$/i','EventShowTopicPersonal');
$this->AddEventPreg('/^\w+$/i','/^(\d+)\.html$/i','EventShowTopic');
$this->AddEventPreg('/^\w+$/i','/^$/i','EventShowBlogGood');
$this->AddEventPreg('/^\w+$/i','/^page(\d+)$/i','EventShowBlogGood');
$this->AddEventPreg('/^\w+$/i','/^bad$/i','/^$/i','EventShowBlogBad');
$this->AddEventPreg('/^\w+$/i','/^bad$/i','/^page(\d+)$/i','EventShowBlogBad');
$this->AddEventPreg('/^\w+$/i','/^new$/i','/^$/i','EventShowBlogNew');
$this->AddEventPreg('/^\w+$/i','/^new$/i','/^page(\d+)$/i','EventShowBlogNew');
$this->AddEventPreg('/^\w+$/i','/^profile$/i','/^$/i','EventShowBlogProfile');
$this->AddEventPreg('/^\w+$/i','/^profile$/i','/^page(\d+)$/i','EventShowBlogProfile');
}
@ -440,7 +455,8 @@ class ActionBlog extends Action {
* @param unknown_type $iTopicId
* @return unknown
*/
protected function ShowTopicPersonal($iTopicId) {
protected function EventShowTopicPersonal() {
$iTopicId=$this->GetEventMatch(1);
/**
* Меню
*/
@ -512,7 +528,9 @@ class ActionBlog extends Action {
* @param unknown_type $iTopicId
* @return unknown
*/
protected function ShowTopic($sBlogUrl,$iTopicId) {
protected function EventShowTopic() {
$sBlogUrl=$this->sCurrentEvent;
$iTopicId=$this->GetParamEventMatch(0,1);
/**
* Меню
*/
@ -589,7 +607,9 @@ class ActionBlog extends Action {
* @param unknown_type $sPage
* @return unknown
*/
protected function ShowBlogGood($sBlogUrl,$sPage) {
protected function EventShowBlogGood() {
$sBlogUrl=$this->sCurrentEvent;
$sPage=$this->GetParam(0);
/**
* Меню
*/
@ -653,7 +673,9 @@ class ActionBlog extends Action {
* @param unknown_type $sPage
* @return unknown
*/
protected function ShowBlogBad($sBlogUrl,$sPage) {
protected function EventShowBlogBad() {
$sBlogUrl=$this->sCurrentEvent;
$sPage=$this->GetParam(1);
/**
* Меню
*/
@ -717,7 +739,9 @@ class ActionBlog extends Action {
* @param unknown_type $sPage
* @return unknown
*/
protected function ShowBlogNew($sBlogUrl,$sPage) {
protected function EventShowBlogNew() {
$sBlogUrl=$this->sCurrentEvent;
$sPage=$this->GetParam(1);
/**
* Меню
*/
@ -782,7 +806,9 @@ class ActionBlog extends Action {
* @param unknown_type $iPage
* @return unknown
*/
protected function ShowBlogProfile($sBlogUrl,$iPage) {
protected function EventShowBlogProfile() {
$sBlogUrl=$this->sCurrentEvent;
$iPage=$this->GetParam(1);
/**
* Меню
*/
@ -947,71 +973,7 @@ class ActionBlog extends Action {
}
}
}
/**
* Обрабатывает динамический УРЛ, т.е. когда евент не определен заранее
*
* @return unknown
*/
protected function EventNotFound() {
/**
* Для топика из персонального блога
*/
if (preg_match("/^(\d+)\.html$/i",$this->sCurrentEvent,$aMatch)) {
return $this->ShowTopicPersonal($aMatch[1]);
}
/**
* Для топика из коллективного блога
*/
if (preg_match("/^(\d+)\.html$/i",$this->getParam(0),$aMatch)) {
return $this->ShowTopic($this->sCurrentEvent,$aMatch[1]);
}
/**
* Для списка хороших топиков из коллективного блога
* site.ru/blog/blog_name/
* site.ru/blog/blog_name/page1/
*/
if (preg_match("/^\w+$/i",$this->sCurrentEvent)
and (is_null($this->getParam(0)) or preg_match("/^page(\d+)$/i",$this->getParam(0)))
)
{
return $this->ShowBlogGood($this->sCurrentEvent,$this->getParam(0));
}
/**
* Для списка плохих топиков из коллективного блога
* site.ru/blog/blog_name/bad/
* site.ru/blog/blog_name/bad/page1/
*/
if (preg_match("/^\w+$/i",$this->sCurrentEvent)
and $this->getParam(0)=='bad'
and (is_null($this->getParam(1)) or preg_match("/^page(\d+)$/i",$this->getParam(1)))
)
{
return $this->ShowBlogBad($this->sCurrentEvent,$this->getParam(1));
}
/**
* Для списка новых топиков из коллективного блога
* site.ru/blog/blog_name/new/
* site.ru/blog/blog_name/new/page1/
*/
if (preg_match("/^\w+$/i",$this->sCurrentEvent)
and $this->getParam(0)=='new'
and (is_null($this->getParam(1)) or preg_match("/^page(\d+)$/i",$this->getParam(1)))
)
{
return $this->ShowBlogNew($this->sCurrentEvent,$this->getParam(1));
}
/**
* Для профиля блога
*/
if (preg_match("/^\w+$/i",$this->sCurrentEvent) and $this->getParam(0)=='profile' and (is_null($this->getParam(1)) or preg_match("/^page(\d+)$/i",$this->getParam(1)))) {
return $this->ShowBlogProfile($this->sCurrentEvent,$this->getParam(1));
}
/**
* Иначе на страницу ошибки
*/
return parent::EventNotFound();
}
/**
* Выполняется при завершении работы экшена

View file

@ -23,6 +23,7 @@ abstract class Action extends Object {
protected $aRegisterEvent=array();
protected $aParams=array();
protected $aParamsEventMatch=array('event'=>array(),'params'=>array());
protected $oEngine=null;
protected $sActionTemplate=null;
protected $sDefaultEvent=null;
@ -44,19 +45,35 @@ abstract class Action extends Object {
/**
* Добавляет евент в экшен
* По сути является оберткой для AddEventPreg(), оставлен для простоты и совместимости с прошлыми версиями ядра
*
* @param string $sEventName Название евента
* @param string $sEventFunction Какой метод ему соответствует
*/
*/
protected function AddEvent($sEventName,$sEventFunction) {
$sEventName=strtolower($sEventName);
if (!isset($this->aRegisterEvent[$sEventName])) {
if (method_exists($this,$sEventFunction)) {
$this->aRegisterEvent[$sEventName]=$sEventFunction;
} else {
throw new Exception("Добавляемый метод евента не найден: ".$sEventFunction);
}
$this->AddEventPreg("/^{$sEventName}$/i",$sEventFunction);
}
/**
* Добавляет евент в экшен, используя регулярное вырожение для евента и параметров
*
*/
protected function AddEventPreg() {
$iCountArgs=func_num_args();
if ($iCountArgs<2) {
throw new Exception("Некорректное число аргументов при добавлении евента");
}
$aEvent=array();
$aEvent['method']=func_get_arg($iCountArgs-1);
if (!method_exists($this,$aEvent['method'])) {
throw new Exception("Добавляемый метод евента не найден: ".$aEvent['method']);
}
$aEvent['preg']=func_get_arg(0);
$aEvent['params_preg']=array();
for ($i=1;$i<$iCountArgs-1;$i++) {
$aEvent['params_preg'][]=func_get_arg($i);
}
$this->aRegisterEvent[]=$aEvent;
}
/**
@ -66,19 +83,30 @@ abstract class Action extends Object {
* @return unknown
*/
public function ExecEvent() {
dump($this->aRegisterEvent);
$this->sCurrentEvent=Router::GetActionEvent();
if ($this->sCurrentEvent==null) {
$this->sCurrentEvent=$this->GetDefaultEvent();
Router::SetActionEvent($this->sCurrentEvent);
}
foreach ($this->aRegisterEvent as $aEvent) {
if (preg_match($aEvent['preg'],$this->sCurrentEvent,$aMatch)) {
$this->aParamsEventMatch['event']=$aMatch;
$this->aParamsEventMatch['params']=array();
foreach ($aEvent['params_preg'] as $iKey => $sParamPreg) {
if (preg_match($sParamPreg,$this->GetParam($iKey,''),$aMatch)) {
$this->aParamsEventMatch['params'][$iKey]=$aMatch;
} else {
continue 2;
}
}
$sCmd='$result=$this->'.$aEvent['method'].'();';
eval($sCmd);
return $result;
}
}
if (isset($this->aRegisterEvent[$this->sCurrentEvent])) {
$sCmd='$result=$this->'.$this->aRegisterEvent[$this->sCurrentEvent].'();';
eval($sCmd);
return $result;
} else {
return $this->EventNotFound();
}
}
return $this->EventNotFound();
}
/**
* Устанавливает евент по умолчанию
@ -97,16 +125,56 @@ abstract class Action extends Object {
public function GetDefaultEvent() {
return $this->sDefaultEvent;
}
/**
* Возвращает элементы совпадения по регулярному выражению для евента
*
* @param unknown_type $iItem
* @return unknown
*/
protected function GetEventMatch($iItem=null) {
if ($iItem) {
if (isset($this->aParamsEventMatch['event'][$iItem])) {
return $this->aParamsEventMatch['event'][$iItem];
} else {
return null;
}
} else {
return $this->aParamsEventMatch['event'];
}
}
/**
* Возвращает элементы совпадения по регулярному выражению для параметров евента
*
* @param unknown_type $iParamNum
* @param unknown_type $iItem
* @return unknown
*/
protected function GetParamEventMatch($iParamNum,$iItem=null) {
if ($iItem) {
if (isset($this->aParamsEventMatch['params'][$iParamNum][$iItem])) {
return $this->aParamsEventMatch['params'][$iParamNum][$iItem];
} else {
return null;
}
} else {
if (isset($this->aParamsEventMatch['event'][$iParamNum])) {
return $this->aParamsEventMatch['event'][$iParamNum];
} else {
return null;
}
}
}
/**
* Получает параметр из URL по его номеру, если его нет то null
*
* @param unknown_type $iOffset
* @return unknown
*/
public function GetParam($iOffset) {
public function GetParam($iOffset,$default=null) {
$iOffset=(int)$iOffset;
return isset($this->aParams[$iOffset]) ? $this->aParams[$iOffset] : null;
return isset($this->aParams[$iOffset]) ? $this->aParams[$iOffset] : $default;
}
/**