Php hook program design and hook Program Design

Source: Internet
Author: User

Php hook program design and hook Program Design
As a programmer, designing an elegant and perfect system always makes us very excited. A master is not about how many languages you speak, but about how high your thoughts are. In the design, how to reflect its own value is to think a few more steps than others. Speaking about the hook program, the origin is the parameter verification submitted by the user (never trust the user). At the beginning, in order to catch up with the construction period, it is a traditional method, parameters are repeatedly filtered in each interface. With the development of the business (Function Iteration), the maintenance cost of the system is getting higher and higher, so I want to process it in a more advanced way. Use hooks for refactoring based on previous code from colleagues. I have previously written the javascript hook mechanism. The partial and back hooks can be used for reference. The pulse concept mounts a block (execution body) into the system in some way to gain control over the system. Note:

 

Small applications: basic input parameter verification or message filtering. Major aspect: componentization, which can be managed in the system. Advantages: 1. Reduce the Coupling Degree of the system; 2. Reduce development and testing labor costs, use a small amount of code to achieve high availability; 3. Improve availability between modules; 4. Upgrade the interface through configuration (configuration file or database. Disadvantages: high learning cost and improved system complexity;
Implement hook definition, hook Chain Management (using the "Group" concept), and hook in the way of idea configuration files.

 

HOOK: The execution body of the program. Hook group: The classification definition of the hook chain. Hook: the entry (action or controller in MVC) is bound to the hook group.
Implementation Method hook handler (inheritance class ):
<? Php/*** @ name Service_Page_Test * @ desc page connects to a third-party abstract class * @ author */abstract class Service_Page_Test {public $ hookGroupPrev = null; // The Front hook Group public $ hookGroupAfter = null; // The rear hook Group public $ hookReturn = array (); // The Hook return value public $ reqData = null; // data analyzed by the page module/*** obtain the parameter configuration to be verified * @ return array */public function _ getCheckParams () {return array ();} /*** entry method ** @ param array $ arrInput * @ return array */pub Lic 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) {}/*** get permission information * @ param array $ arrInput * @ return array */public function _ init ($ arrInput) {$ pageModulesConf = Conf :: getConf ('page /'. get_class ($ this); $ this-> reqData = $ arrInput; $ this-> hookGroup Prev [] = $ pageModulesConf ['hook _ group'] ['prev']; $ this-> hookGroupAfter [] = $ pageModulesConf ['hook _ group'] ['after'];} /*** execute 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-> messages) {foreach ($ this-> hookGroupAfter as $ hookGroups) {foreach ($ hookGroups as $ hookGroup) {$ this-> _ executeHook ($ hookGroup, $ this-> reqData );}}}} /*** run 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 );}}}}}}

  

 

Hook. conf

# Hook Group
[Group] [. check_req_customer] 0: checkReqCustomerBaseInfo [. after_demo] 0: afterDemo # hook [hook] [. checkReqCustomerBaseInfo] class: Service_Page_Hook_Customermethod: checkBaseInfogetInputParams: _ getCheckParams [. afterDemo] class: Service_Page_Hook_Customermethod: afterDemogetInputParams: _ getCheckParams

 

Page. conf
[Service_Page_Input] # bind the hook group [. hook_group] [... prev] 0: check_req_customer [. after] 0: after_demo

  

Recommendation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.