yl197837 发表于 2017-12-29 14:34:27

php钩子程序设计

<?php  
/**
  
* @name Service_Page_Test
  
* @desc page层对接第三方抽象类
  
* @author
  
*/

  
abstract>  
{
  
public $hookGroupPrev = null; // 前钩子组
  
public $hookGroupAfter = null; // 后钩子组
  
public $hookReturn = array(); //钩子返回值
  
public $reqData = null; // page模块分析的数据
  

  
/**
  
* 获取需要验证的参数配置
  
* @return array
  
*/
  
public function _getCheckParams()
  
{
  
return array();
  
}
  

  

  

  
/**
  
* 入口方法
  
* @param array $arrInput
  
* @return array
  
*/
  
public function execute($arrInput)
  
{
  
$res = array(
  
'errno' => Test_Errno::ERRNO_SUCCESS,
  
'errmsg' => Test_Errno::$ERRMSG,
  
);
  
try {
  
$this->_init($arrInput);
  
$this->_beforeExecute();
  
$res = $this->doExecute($arrInput);
  
$this->_afterExecute();
  
} catch (Test_Exception $e) {
  
$res = array(
  
'errno' => $e->getCode(),
  
'errmsg' => $e->getMessage(),
  
);
  
} catch (Exception $e) {
  
$res = array(
  
'errno' => $e->getCode(),
  
'errmsg' => $e->getMessage(),
  
);
  

  
}
  
return $res;
  
}
  

  

  

  
/**
  
* auto exec
  
* @param array $arrInput
  
* @throws Exception
  
* @return array
  
*/
  
protected function doExecute($arrInput){
  
}
  

  

  
/**
  
* 获取权限信息
  
* @param array $arrInput
  
* @return array
  
*/
  
public function _init($arrInput)
  
{
  
$pageModulesConf = Conf::getConf('page/' . get_class($this));
  
$this->reqData = $arrInput;
  
$this->hookGroupPrev[] = $pageModulesConf['hook_group']['prev'];
  
$this->hookGroupAfter[] = $pageModulesConf['hook_group']['after'];
  
}
  

  

  
/**
  
* 执行filter
  
* @param string
  
*/
  
public function _beforeExecute()
  
{
  
if (!empty($this->hookGroupPrev) && is_array($this->hookGroupPrev)) {
  
foreach ($this->hookGroupPrev as $hookGroups) {
  
foreach ($hookGroups as $hookGroup) {
  
$this->_executeHook($hookGroup, $this->reqData);
  
}
  
}
  
}
  
}
  

  

  
/**
  
* @param array $arrInput
  
* @return array
  
*/
  
public function _afterExecute()
  
{
  
if (!empty($this->hookGroupAfter) && is_array($this->hookGroupAfter)) {
  
foreach ($this->hookGroupAfter as $hookGroups) {
  
foreach ($hookGroups as $hookGroup) {
  
$this->_executeHook($hookGroup, $this->reqData);
  
}
  
}
  
}
  
}
  

  

  
/**
  
* 执行filter
  
* @param string
  
*/
  
public function _executeHook($hookGroup, $reqData)
  
{
  

  
$hookGroupConf = Conf::getConf('hook/group/' . $hookGroup);
  
if(!empty($hookGroupConf)){
  
foreach($hookGroupConf as $hook){
  
$hookConf = Conf::getConf('hook/hook/' . $hook);
  
$class = $hookConf['class'];
  
$method = $hookConf['method'];
  
$inputParams = isset($hookConf['getInputParams']) ? $this->{$hookConf['getInputParams']}() : null;
  
if (class_exists($class)) {
  
$obj = new $class();
  
if (method_exists($obj, $method)) {
  
$this->hookReturn[$hook][] =$obj->$method($inputParams, $reqData);
  
}
  
}
  
}
  
}
  

  
}
  

  
}
  
页: [1]
查看完整版本: php钩子程序设计