




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
-PHPCMS学习记录-从index.php开始解读代码:总共3行代码,完成了很多事。define(PHPCMS_PATH, dirname(_FILE_).DIRECTORY_SEPARATOR);include PHPCMS_PATH./phpcms/base.php;pc_base:creat_app();第一句,定义phpcms站点根目录PHPCMS_PATH,注意这里定义的目录常量不是服务器根目录,而是phpcms系统的根目录。_FILE_返回当前文件的绝对路径,dirname(_FILE_)则可以取得当前文件的绝对目录;DIRECTORY_SEPARATOR显示当前系统的分隔符;第二句,引入一个base.php文件,定义了一个pc_base类,从字面意思上可以看出这应该是个基础类,事实亦如此。根据下文猜想pc应该是phpcms的缩写。引入文件的时候完成了一些基础常量的定义和公用函数库的加载等基础配置。第三句,这句代码完成了系统的初始化,非常强大!下面细细道来它的强大之处:先来看这个creat_app()方法,定义如下:public static function creat_app() return self:load_sys_class(application);那么又来看load_sys_class()方法:public static function load_sys_class($classname, $path = , $initialize = 1) return self:_load_class($classname, $path, $initialize);接下来是_load_class()方法:private static function _load_class($classname, $path = , $initialize = 1) static $classes = array();if (empty($path) $path = libs.DIRECTORY_SEPARATOR.classes;$key = md5($path.$classname);if (isset($classes$key) if (!empty($classes$key) return $classes$key; else return true;if (file_exists(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.class.php) include PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.class.php;$name = $classname;if ($my_path = self:my_path(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.class.php) include $my_path;$name = MY_.$classname;if ($initialize) $classes$key = new $name; else $classes$key = true;return $classes$key; else return false;颠来倒去的一大堆,实际就是加载类函数并实例化这个类,但是值得赞赏和学习的是这个函数逻辑之严密,由此phpcms框架的成熟性可见一斑。这里加载的类函数是PC_PATH.libs.DIRECTORY_SEPARATOR.classes.DIRECTORY_SEPARATOR.application.class.php,PC_PATH就是你的phpcms系统下的phpcms文件目录,有点绕,反正不是你的系统根目录就对了,这里印证了上面pc缩写是phpcms的说法接着上面,现在实例化了application类,那么我们就去看看这个类。首先有个构造函数:public function _construct() $param = pc_base:load_sys_class(param);define(ROUTE_M, $param-route_m();define(ROUTE_C, $param-route_c();define(ROUTE_A, $param-route_a();$this-init();第一句:加载系统函数,去查base.php文件,load_sys_class()在上面用过,这句就是加载param.class.php并实例化一个对象赋给变量$param。转到param类,有构造函数如下:public function _construct() if(!get_magic_quotes_gpc() $_POST = new_addslashes($_POST);$_GET = new_addslashes($_GET);$_REQUEST = new_addslashes($_REQUEST);$_COOKIE = new_addslashes($_COOKIE);$this-route_config = pc_base:load_config(route, SITE_URL) ? pc_base:load_config(route, SITE_URL) : pc_base:load_config(route, default);if(isset($this-route_configdataPOST) & is_array($this-route_configdataPOST) foreach($this-route_configdataPOST as $_key = $_value) if(!isset($_POST$_key) $_POST$_key = $_value;if(isset($this-route_configdataGET) & is_array($this-route_configdataGET) foreach($this-route_configdataGET as $_key = $_value) if(!isset($_GET$_key) $_GET$_key = $_value;if(isset($_GETpage) $_GETpage = max(intval($_GETpage),1);return true;这个函数先做了gpc和r四种传递参数的转义处理,可以预防SQL注入攻击,然后是pc_base:load_config()的加载路由配置,这里加载的是默认配置。最后是根据gpc来的参数设置变量。转回来,第二句:调用对象方法$param-route_m()获得模型名第三句:调用对象方法$param-route_c获得控制器名第四句:调用对象方法$param-route_a获得行为名最后一句:$this-init();初始化。如下:private function init() $controller = $this-load_controller();if (method_exists($controller, ROUTE_A) if (preg_match(/_/i, ROUTE_A) exit(You are visiting the action is to protect the private action); else call_user_func(array($controller, ROUTE_A); else exit(Action does not exist.);$this-load_controller()方法如下:private function load_controller($filename = , $m = ) if (empty($filename) $filename = ROUTE_C;if (empty($m) $m = ROUTE_M;$filepath = PC_PATH.modules.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR.$filename.php;if (file_exists($filepath) $classname = $filename;include $filepath;if ($mypath = pc_base:my_path($filepath) $classname = MY_.$filename;include $mypath;if(class_exists($classname)return new $classname;elseexit(Controller does not exist.); else exit(Controller does not exist.);故这个初始化方法第一步调用了application类的load_controller()函数,该函数加载了ROUTE_M模型名下的ROUTE_C控制器并实例化;第二步执行了ROUTE_C控制器中的ROUTE_A方法。(ROUTE_M、ROUTE_C和ROUTE_A在前面有定义,ROUTE_M=content,ROUTE_C=index,ROUTE_A=init)第一步中,在实例化index控制器时有构造函数:function _construct() $this-db = pc_base:load_model(content_model);$this-_userid = param:get_cookie(_userid);$this-_username = param:get_cookie(_username);$this-_groupid = param:get_cookie(_groupid);先加载了content模型,然后初始化了一些成员变量,content模型的构造函数如下:public function _construct() $this-db_config = pc_base:load_config(database);$this-db_setting = default;parent:_construct();$this-url = pc_base:load_app_class(url, content);$this-siteid = get_siteid();加载数据库配置文件,调用父类的构造函数,如下:public function _construct() if (!isset($this-db_config$this-db_setting) $this-db_setting = default;$this-table_name = $this-db_config$this-db_settingtablepre.$this-table_name;$this-db_tablepre = $this-db_config$this-db_settingtablepre;$this-db = db_factory:get_instance($this-db_config)-get_database($this-db_setting);作用是配置数据库相关的成员变量。$this-url = pc_base:load_app_class(url, content);加载应用类url,构造函数如下:public function _construct() $this-urlrules = getcache(urlrules,commons);self:set_siteid();$this-categorys = getcache(category_content_.$this-siteid,commons);$this-html_root = pc_base:load_config(system,html_root);作用是配置链接相关的成员变量。第二步中,调用了index控制器中的init()方法,如下:public function init() if(isset($_GETsiteid) $siteid = intval($_GETsiteid); else $siteid = 1;$siteid = $GLOBALSsiteid = max($siteid,1);define(SITEID, $siteid);$_userid = $this-_userid;$_username = $this-_username;$_groupid = $this-_groupid;/SEO$SEO = seo($siteid);$sitelist = getcache(sitelist,commons);$default_style = $sitelist$siteiddefault_style;$CATEGORYS = getcache(category_content_.$siteid,commons);include template(content,index,$default_style);重点来了,这个方法中使用的template()方法是获取视图模版的,其中包含了phpcms模板引擎的关键。该方法在global.func.php,如下:function template($module = content, $template = index, $style = ) if(strpos($module, plugin/)!= false) $plugin = str_replace(plugin/, , $module);return p_template($plugin, $template,$style);$module = str_replace(/, DIRECTORY_SEPARATOR, $module);if(!empty($style) & preg_match(/(a-z0-9-_+)/is,$style) elseif (empty($style) & !defined(STYLE) if(defined(SITEID) $siteid = SITEID; else $siteid = param:get_cookie(siteid);if (!$siteid) $siteid = 1;$sitelist = getcache(sitelist,commons);if(!empty($siteid) $style = $sitelist$siteiddefault_style; elseif (empty($style) & defined(STYLE) $style = STYLE; else $style = default;if(!$style) $style = default;$template_cache = pc_base:load_sys_class(template_cache);$compiledtplfile = PHPCMS_PATH.caches.DIRECTORY_SEPARATOR.caches_template.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.php;if(file_exists(PC_PATH.templates.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.html) if(!file_exists($compiledtplfile) | (filemtime(PC_PATH.templates.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.html) filemtime($compiledtplfile) $template_cache-template_compile($module, $template, $style); else $compiledtplfile = PHPCMS_PATH.caches.DIRECTORY_SEPARATOR.caches_template.DIRECTORY_SEPARATOR.default.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.php;if(!file_exists($compiledtplfile) | (file_exists(PC_PATH.templates.DIRECTORY_SEPARATOR.default.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.html) & filemtime(PC_PATH.templates.DIRECTORY_SEPARATOR.default.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.html) filemtime($compiledtplfile) $template_cache-template_compile($module, $template, default); elseif (!file_exists(PC_PATH.templates.DIRECTORY_SEPARATOR.default.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.html) showmessage(Template does not exist.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.html);return $compiledtplfile;关键在$compiledtplfile,也是这个函数的返回值,它是解析之后的模版文件(我称之为模版缓存文件),由$template_cache-template_compile($module, $template, $style)得来,当模版缓存文件没有生成或者.html的模版文件的最后修改时间大于模版缓存文件的最后修改时间时(即是刚刚修改过模版文件),就会执行template_compile()方法,对模版文件进行编译解析生成模版缓存文件。下面来看编译是怎么完成的,template_compile()方法如下:public function template_compile($module, $template, $style = default) if(strpos($module, /)= false) $tplfile = $_tpl = PC_PATH.templates.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.html; elseif (strpos($module, yp/) != false) $module = str_replace(/, DIRECTORY_SEPARATOR, $module);$tplfile = $_tpl = PC_PATH.templates.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.html; else $plugin = str_replace(plugin/, , $module);$module = str_replace(/, DIRECTORY_SEPARATOR, $module);$tplfile = $_tpl = PC_PATH.plugin.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.templates.DIRECTORY_SEPARATOR.$template.html;if ($style != default & !file_exists ( $tplfile ) $style = default;$tplfile = PC_PATH.templates.DIRECTORY_SEPARATOR.default.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.html;if (! file_exists ( $tplfile ) showmessage ( templates.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.html is not exists! );$content = file_get_contents ( $tplfile );$filepath = CACHE_PATH.caches_template.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR; if(!is_dir($filepath) mkdir($filepath, 0777, true); $compiledtplfile = $filepath.$template.php;$content = $this-template_parse($content);$strlen = file_put_contents ( $compiledtplfile, $content );chmod ( $compiledtplfile, 0777 );return $strlen;首先定义了一个$tplfile,即是模版文件路径,然后把这个文件读入一个字符串$content中,然后用template_parse()方法对这个字符串进行解析。最后把解析之后的这个字符串写入$compiledtplfile,即是模版缓存文件。现在我们来看template_parse()解析方法,如下:public function template_parse($str) $str = preg_replace ( /templates+(.+)/, , $str );$str = preg_replace ( /includes+(.+)/, , $str );$str = preg_replace ( /phps+(.+)/, , $str );$str = preg_replace ( /ifs+(.+?)/, , $str );$str = preg_replace ( /else/, , $str );$str = preg_replace ( /elseifs+(.+?)/, ,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业风险管理流程工具
- 想象作文衣柜里的争吵400字9篇范文
- 市场营销策略分析报告市场趋势与竞争策略版
- 文档资料管理系统与操作指南
- 2025【合同范本】设备采购合同格式
- 2025年江苏省南京市雨花台区中考语文模拟试卷
- 2025年分期付款家电购买合同详
- 2025年河南省继续教育公需科目试题及答案
- 共享出行新模式2025年城市交通拥堵治理策略评估报告
- 2025成品钢材供货的合同范本
- 数据可视化课程建设经验交流陈为课件
- 二级减速器计算说明书
- 厨房设备施工方案
- 《比热容》说课-完整版课件
- 北京市各县区乡镇行政村村庄村名明细
- 各种轴载换算计算方法
- (高职)《会展策划》(第三版)ppt课件(完整版)
- 商超类企业抖音代运营方案(综合)
- 海上保险法课堂笔记(国航上课版)
- 精选文档大跨度梁板混凝土浇筑方案
- 数学算24点题目
评论
0/150
提交评论