/** * Error机制 私有函数,error输出 * @param string $error_type 错误类型 * @return object */ private function display($error_type) { $InitPHP_conf = InitPHP::getConfig(); if ($error_type == 'text') { $error = implode("\r\t", $this->error_data); exit($error); } elseif ($error_type == 'json') { exit(json_encode($this->error_data)); } elseif ($error_type == 'xml') { $xml = '<?xml version="1.0" encoding="utf-8"?>'; $xml .= '<return>'; foreach ($this->error_data as $v) { $xml .= '<error>' . $v . '</error>'; } $xml .= '</return>'; exit($xml); } elseif ($error_type == 'array') { exit(var_export($this->error_data)); } elseif ($error_type == 'html') { $error = $this->error_data; $template = InitPHP::getAppPath($InitPHP_conf['error']['template']); if ($template) { if (!file_exists($template)) { InitPHP::initError('error template is not exist'); } @(include $template); } else { InitPHP::initError('please set error template in initphp.conf.php'); } //扩展HTML错误输出 exit; } }
/** * 具体解析 * @param unknown_type $isPre */ private function parse($isPre = true) { $InitPHP_conf = InitPHP::getConfig(); $interceptor = $InitPHP_conf['interceptor']; $filePath = $interceptor['path']; //文件路径 $return = true; if (is_array($interceptor['rule']) && count($interceptor['rule']) > 0) { foreach ($interceptor['rule'] as $k => $v) { $file = ltrim($filePath, "/") . "/" . $v['file'] . $interceptor['postfix'] . '.php'; $class = $v['file'] . $interceptor['postfix']; //处理正则匹配 $regular = $v['regular']; if ($regular['m'] != "" && $regular['m'] != '*') { if (!preg_match($regular['m'], $this->m)) { continue; } } if ($regular['c'] != "" && $regular['c'] != '*') { if (!preg_match($regular['c'], $this->c)) { continue; } } if ($regular['a'] != "" && $regular['a'] != '*') { if (!preg_match($regular['a'], $this->a)) { continue; } } if (file_exists(InitPHP::getAppPath($file))) { InitPHP::import($file); $obj = InitPHP::loadclass($class); if ($isPre == true) { $ret = $obj->preHandle(); if ($ret == false) { $return = false; break; } } else { $obj->postHandle(); } } } } return $return; }
/** * 模板编译-设置模板信息 * Controller中使用方法:$this->view->set_template_config($config) * @param array $config 设置参数 * @return bool */ public function set_template_config($config) { if (!is_array($config)) { return false; } $appPath = InitPHP::getAppPath(''); //APP_PATH $config['template_path'] = $appPath . $config['template_path']; $config['template_c_path'] = $appPath . $config['template_c_path']; if (isset($config['theme']) && !empty($config['theme'])) { //模板主题实现 $config['template_path'] = $config['template_path'] . '/' . $config['theme']; $config['template_c_path'] = $config['template_c_path'] . '/' . $config['theme']; if ($config['is_compile'] == true) { $this->create_dir($config['template_c_path']); } //创建主题文件夹 } if (isset($config['template_path'])) { $this->template_path = $config['template_path']; } if (isset($config['template_c_path'])) { $this->template_c_path = $config['template_c_path']; } if (isset($config['template_type'])) { $this->template_type = $config['template_type']; } if (isset($config['template_c_type'])) { $this->template_c_type = $config['template_c_type']; } if (isset($config['template_tag_left'])) { $this->template_tag_left = $config['template_tag_left']; } if (isset($config['template_tag_right'])) { $this->template_tag_right = $config['template_tag_right']; } if (isset($config['is_compile'])) { $this->is_compile = $config['is_compile']; } $this->driver_config = $config['driver']; return true; }
/** * 框架hook插件机制-私有 * @param string $class 钩子的类名 * @param array $function 钩子方法名称 * @param string $data 传递的参数 * @return object */ private static function _hook($class, $function, $data = '') { $InitPHP_conf = InitPHP::getConfig(); if (preg_match('/[^a-z0-9\\-_.]/i', $class)) { return false; } $file_name = $InitPHP_conf['hook']['path'] . '/' . $class . $InitPHP_conf['hook']['file_postfix']; $file_name = InitPHP::getAppPath($file_name); $class_name = $class . $InitPHP_conf['hook']['class_postfix']; //类名 if (!file_exists($file_name)) { return false; } if (!isset(parent::$instance['inithook'][$class_name])) { require_once $file_name; if (!class_exists($class_name)) { return false; } $init_class = new $class_name(); parent::$instance['inithook'][$class_name] = $init_class; } if (!method_exists($class_name, $function)) { return false; } return parent::$instance['inithook'][$class_name]->{$function}($data); }
/** * 获取文件目录下所有文件 * @return object */ private function get_all_file() { $InitPHP_conf = InitPHP::getConfig(); $temp = array(); $path = InitPHP::getAppPath($InitPHP_conf['unittesting']['path']); if (is_dir($path)) { if ($dh = opendir($path)) { while (($file = readdir($dh)) !== false) { if ($file == '.' || $file == '..') { continue; } $temp[] = str_replace($InitPHP_conf['unittesting']['test_postfix'] . '.php', '', $file); } closedir($dh); } } return $temp; }
/** * 缓存工厂-获取不同缓存类型的对象句柄 * @param string $type 缓存类型 * @return obj */ private function get_cache_handle($type) { $InitPHP_conf = InitPHP::getConfig(); //需要设置文件缓存目录 $type = strtoupper($type); $type = in_array($type, $this->cache_type) ? $type : 'FILE'; switch ($type) { case 'FILE': if (isset(cacheInit::$instance['filecache'])) { return cacheInit::$instance['filecache']; } $filecache = $this->load_cache('filecache.Init.php', 'filecacheInit'); $filepath = InitPHP::getAppPath($InitPHP_conf['cache']['filepath']); $filecache->set_cache_path($filepath); cacheInit::$instance['filecache'] = $filecache; return $filecache; break; case 'MEM': if (isset(cacheInit::$instance['memcache'])) { return cacheInit::$instance['memcache']; } $mem = $this->load_cache('memcached.Init.php', 'memcachedInit'); $mem->add_server($InitPHP_conf['memcache']); //添加服务器 cacheInit::$instance['memcache'] = $mem; return $mem; break; case 'MYSQL': if (isset(cacheInit::$instance['mysqlcache'])) { return cacheInit::$instance['mysqlcache']; } $mysqlcache = $this->load_cache('mysqlcache.Init.php', 'mysqlcacheInit'); $mysqlcache->set_sql_handler($this->db_handle); cacheInit::$instance['mysqlcache'] = $mysqlcache; return $mysqlcache; break; case 'APC': if (isset(cacheInit::$instance['apc'])) { return cacheInit::$instance['apc']; } $filecache = $this->load_cache('apc.Init.php', 'apcInit'); break; case 'XCACHE': if (isset(cacheInit::$instance['xcache'])) { return cacheInit::$instance['xcache']; } $filecache = $this->load_cache('xcache.Init.php', 'xcacheInit'); break; case 'WINCACHE': if (isset(cacheInit::$instance['wincache'])) { return cacheInit::$instance['wincache']; } $filecache = $this->load_cache('wincache.Init.php', 'wincacheInit'); break; } }