CodeIgniter框架钩子机制实现方法【hooks类】
本文实例讲述了CodeIgniter框架钩子机制实现方法。分享给大家供大家参考,具体如下:
记得上一次去到喜啦面试,面试官问我一个问题:codeigniter是如何实现钩子机制的?
当时答不上来,后来回来之后查了一些资料才明白,所以在这里记录一下:
codeigniter的钩子是这样实现的:首先在框架的核心文件system/core/CodeIniter.php文件的122行,载入Hooks类,接着在该文件中定义了几个挂载点,比如pre_system(129行)、post_controller_constructor(295行)等,并在这些挂载点上面执行hooks类的_call_hook()方法。
另附codeigniter的hooks类的源代码:
_initialize(); log_message('debug',"HooksClassInitialized"); } //-------------------------------------------------------------------- /** *InitializetheHooksPreferences * *@accessprivate *@returnvoid */ function_initialize() { $CFG=&load_class('Config','core'); //Ifhooksarenotenabledintheconfigfile //thereisnothingelsetodo if($CFG->item('enable_hooks')==FALSE) { return; } //Grabthe"hooks"definitionfile. //Iftherearenohooks,we'redone. if(defined('ENVIRONMENT')ANDis_file(APPPATH.'config/'.ENVIRONMENT.'/hooks.php')) { include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'); } elseif(is_file(APPPATH.'config/hooks.php')) { include(APPPATH.'config/hooks.php'); } if(!isset($hook)OR!is_array($hook)) { return; } $this->hooks=&$hook; $this->enabled=TRUE; } //-------------------------------------------------------------------- /** *CallHook * *Callsaparticularhook * *@accessprivate *@paramstringthehookname *@returnmixed */ function_call_hook($which='') { if(!$this->enabledOR!isset($this->hooks[$which])) { returnFALSE; } if(isset($this->hooks[$which][0])ANDis_array($this->hooks[$which][0])) { foreach($this->hooks[$which]as$val) { $this->_run_hook($val); } } else { $this->_run_hook($this->hooks[$which]); } returnTRUE; } //-------------------------------------------------------------------- /** *RunHook * *Runsaparticularhook * *@accessprivate *@paramarraythehookdetails *@returnbool */ function_run_hook($data) { if(!is_array($data)) { returnFALSE; } //----------------------------------- //Safety-Preventsrun-awayloops //----------------------------------- //Ifthescriptbeingcalledhappenstohavethesame //hookcallwithinitaloopcanhappen if($this->in_progress==TRUE) { return; } //----------------------------------- //Setfilepath //----------------------------------- if(!isset($data['filepath'])OR!isset($data['filename'])) { returnFALSE; } $filepath=APPPATH.$data['filepath'].'/'.$data['filename']; if(!file_exists($filepath)) { returnFALSE; } //----------------------------------- //Setclass/functionname //----------------------------------- $class=FALSE; $function=FALSE; $params=''; if(isset($data['class'])AND$data['class']!='') { $class=$data['class']; } if(isset($data['function'])) { $function=$data['function']; } if(isset($data['params'])) { $params=$data['params']; } if($class===FALSEAND$function===FALSE) { returnFALSE; } //----------------------------------- //Setthein_progressflag //----------------------------------- $this->in_progress=TRUE; //----------------------------------- //Calltherequestedclassand/orfunction //----------------------------------- if($class!==FALSE) { if(!class_exists($class)) { require($filepath); } $HOOK=new$class; $HOOK->$function($params); } else { if(!function_exists($function)) { require($filepath); } $function($params); } $this->in_progress=FALSE; returnTRUE; } } //ENDCI_Hooksclass /*EndoffileHooks.php*/ /*Location:./system/core/Hooks.php*/
可以看出codeigniter实现钩子机制的方式不够优雅,其实完全可以使用观察者模式来实现钩子机制,将挂载点当做监听的事件。
更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《ZendFrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。