PHPCMSV9 亂解讀 之 PHPCMS V9的MVC
作者:admin 日期:2013-07-07
標 題: PHPCMSV9 亂解讀 之 PHPCMS V9的MVC
作 者: web開發網[http://www.zeroplace.cn]
時 間: 2013/07/07 21:15:00
鏈 接: http://www.zeroplace.cn/article.asp?id=853
說 明: 轉載請保留本段文字
現在的程序設計基本上都講求MVC,數據與模板分離,單一入口。
- /**
- * 加載函數庫
- * @param string $func 函數庫名
- * @param string $path 地址
- */
- private static function _load_func($func, $path = "') {
- static $funcs = array();
- if (emptyempty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'functions';
- $path .= DIRECTORY_SEPARATOR.$func.'.func.php';
- $key = md5($path);
- if (isset($funcs[$key])) return true;
- if (file_exists(PC_PATH.$path)) {
- include PC_PATH.$path;
- } else {
- $funcs[$key] = false;
- return false;
- }
- $funcs[$key] = true;
- return true;
- }
- /**
- * 加載函數庫
- * @param string $func 函數庫名
- * @param string $path 地址
- */
- private static function _auto_load_func($path = "') {
- if (emptyempty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'functions'.DIRECTORY_SEPARATOR.'autoload';
- $path .= DIRECTORY_SEPARATOR.'*.func.php';
- $auto_funcs = glob(PC_PATH.DIRECTORY_SEPARATOR.$path);
- if(!emptyempty($auto_funcs) && is_array($auto_funcs)) {
- foreach($auto_funcs as $func_path) {
- include $func_path;
- }
- }
- }
- /**
- * 加載配置文件
- * @param string $file 配置文件
- * @param string $key 要獲取的配置鍵
- * @param string $default 默認配置。當獲取配置項目失敗時該值發生作用。
- * @param boolean $reload 強制重新加載。
- */
- public static function load_config($file, $key = "', $default = '', $reload = false) {
- static $configs = array();
- if (!$reload && isset($configs[$file])) {
- if (emptyempty($key)) {
- return $configs[$file];
- } elseif (isset($configs[$file][$key])) {
- return $configs[$file][$key];
- } else {
- return $default;
- }
- }
- $path = CACHE_PATH.'configs'.DIRECTORY_SEPARATOR.$file.'.php';
- if (file_exists($path)) {
- $configs[$file] = include $path;
- }
- if (emptyempty($key)) {
- return $configs[$file];
- } elseif (isset($configs[$file][$key])) {
- return $configs[$file][$key];
- } else {
- return $default;
- }
- }
- /**
- * 加載類文件函數
- * @param string $classname 類名
- * @param string $path 擴展地址
- * @param intger $initialize 是否初始化
- */
- private static function _load_class($classname, $path = "', $initialize = 1) {
- static $classes = array();
- if (emptyempty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'classes';
- $key = md5($path.$classname);
- if (isset($classes[$key])) {
- if (!emptyempty($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;
- }
- }
- /**
- * 加載控制器
- * @param string $filename
- * @param string $m
- * @return obj
- */
- private function load_controller($filename = "', $m = '') {
- if (emptyempty($filename)) $filename = ROUTE_C;
- if (emptyempty($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;
- }else{
- exit('Controller does not exist.');
- }
- } else {
- exit('Controller does not exist.');
- }
- }
- //內容頁
- public function show() {
- $catid = intval($_GET["catid']);
- $id = intval($_GET['id']);
- ...
- include template('content',$template);
- }
- /**
- * 模板調用
- *
- * @param $module
- * @param $template
- * @param $istag
- * @return unknown_type
- */
- 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(!emptyempty($style) && preg_match('/([a-z0-9\-_]+)/is',$style)) {
- } elseif (emptyempty($style) && !defined('STYLE')) {
- if(defined('SITEID')) {
- $siteid = SITEID;
- } else {
- $siteid = param::get_cookie('siteid');
- }
- if (!$siteid) $siteid = 1;
- $sitelist = getcache('sitelist','commons');
- if(!emptyempty($siteid)) {
- $style = $sitelist[$siteid]['default_style'];
- }
- } elseif (emptyempty($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;
- }
- /**
- * 編譯模板
- *
- * @param $module 模塊名稱
- * @param $template 模板文件名
- * @param $istag 是否爲標籤模板
- * @return unknown
- */
- 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;
- }
- /**
- * 解析模板
- *
- * @param $str 模板內容
- * @return ture
- */
- public function template_parse($str) {
- $str = preg_replace ( "/\{template\s+(.+)\}/", "<?php include template(\\1); ?>", $str );
- $str = preg_replace ( "/\{include\s+(.+)\}/", "<!--p include \\1;-->", $str );
- $str = preg_replace ( "/\{php\s+(.+)\}/", "<!--p \\-->", $str );
- $str = preg_replace ( "/\{if\s+(.+?)\}/", "<!--p if(\\1) {-->", $str );
- $str = preg_replace ( "/\{else\}/", "<!--p } else {-->", $str );
- $str = preg_replace ( "/\{elseif\s+(.+?)\}/", "<!--p } elseif (\\1) {-->", $str );
- $str = preg_replace ( "/\{\/if\}/", "<!--p }-->", $str );
- //for 循環
- $str = preg_replace("/\{for\s+(.+?)\}/","<!--p for(\\1) {-->",$str);
- $str = preg_replace("/\{\/for\}/","<!--p }-->",$str);
- //++ --
- $str = preg_replace("/\{\+\+(.+?)\}/","<!--p ++\\1;-->",$str);
- $str = preg_replace("/\{\-\-(.+?)\}/","<!--p ++\\1;-->",$str);
- $str = preg_replace("/\{(.+?)\+\+\}/","<!--p \\1++;-->",$str);
- $str = preg_replace("/\{(.+?)\-\-\}/","",$str);
- $str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\}/", "<!--p \$n=1;if(is_array(\\1)) foreach(\\1 AS \\2) {-->", $str );
- $str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<!--p \$n=1; if(is_array(\\1)) foreach(\\1 AS \\2--> \\3) { ?>", $str );
- $str = preg_replace ( "/\{\/loop\}/", "<!--p \$n++;}unset(\$n);-->", $str );
- $str = preg_replace ( "/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<!--p echo \\1-->", $str );
- $str = preg_replace ( "/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<!--p echo \\1-->", $str );
- $str = preg_replace ( "/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "<!--p echo \\1-->", $str );
- $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\"\"\$\x7f-\xff]+)\}/es", "\$this->addquote('<!--p echo \\1-->')",$str);
- $str = preg_replace ( "/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<!--p echo \\1-->", $str );
- $str = preg_replace("/\{pc:(\w+)\s+([^}]+)\}/ie", "self::pc_tag('$1','$2', '$0')", $str);
- $str = preg_replace("/\{\/pc\}/ie", "self::end_pc_tag()", $str);
- $str = "<!--p defined('IN_PHPCMS') or exit('No permission resources.');-->" . $str;
- return $str;
- }
- /**
- * 加載數據模型
- * @param string $classname 類名
- */
- public static function load_model($classname) {
- return self::_load_class($classname,"model');
- }
- defined("IN_PHPCMS') or exit('No permission resources.');
- pc_base::load_sys_class('model', '', 0);
- class admin_model extends model {
- public function __construct() {
- $this->db_config = pc_base::load_config('database');
- $this->db_setting = 'default';
- $this->table_name = 'admin';
- parent::__construct();
- }
- }