php鉤子程式設計

來源:互聯網
上載者:User

標籤:.com   custom   功能   cep   lin   des   after   exists   row   

 序    作為程式員,設計出優雅而完美的系統,永遠是讓我們非常興奮的事情。高手不在於你會多少語言,而在於你有多高的思想。   在設計中,怎麼體現自身價值,那就是要比別人多想幾步。   講鉤子程式,起源是對使用者提交的參數校正(永遠不要相信使用者),一開始為了趕工期,按照比較傳統的方式,每個介面裡重複性的對參數進行過濾。後面隨著業務的發展(功能迭代),系統的維護成本越來越高,遂想一個更進階的方式進行處理。故想到使用鉤子方式。 脈路    

 

   概念    把一段程式塊(執行體)通過某種方式掛入系統中,從而獲得對系統的控制權。  應用情境    小的方面: 進行基礎的入參校正或訊息過濾。  大的方面:組件化,可在系統中進行插拔管理。    優點:    1、降低系統的耦合度;    2、降低開發、測試人力成本,用少量的代碼實現高可用功能;    3、提高模組間的可用性;    4、通過配置(設定檔or資料庫)的方式升級介面。  缺點:    學習成本過高;    系統複雜度提升; 
 具體實現     設定檔的方式進行鉤子定義、鉤子鏈管理(使用“組”的概念)、掛鈎。   

 

    鉤子:程式執行體;    鉤子組: 鉤子鏈的分類定義;             掛鈎: 入口(MVC中action或者controller)與鉤子組進行綁定。           掛鈎代碼類(繼承類):
<?php/** * @name Service_Page_Test * @desc page層對接第三方抽象類別 * @author   */abstract class Service_Page_Test {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[Test_Errno::ERRNO_SUCCESS],        );        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->hookGroupPrev[] = $pageModulesConf[‘hook_group‘][‘prev‘];$this->hookGroupAfter[] = $pageModulesConf[‘hook_group‘][‘after‘];}    /**     * 執行filter     * @param string $filterName     */    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 $filterName     */    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);}}}}    }}

  

 

  hook.conf

[group][.check_req_customer]0 : checkReqCustomerBaseInfo[.after_demo]0 : afterDemo[hook][.checkReqCustomerBaseInfo]class:  Service_Page_Hook_Customermethod: checkBaseInfogetInputParams: _getCheckParams[.afterDemo]class:  Service_Page_Hook_Customermethod: afterDemogetInputParams: _getCheckParams

 

  page.conf
[Service_Page_Input][.hook_group][..prev]0 : check_req_customer[..after]0 : after_demo

  

   推薦     

php鉤子程式設計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.