Main.php文件
<?php if(!defined(‘APP‘)){ exit(‘APP does not defined‘); } //必须先定义APP常量 if(DBUG==1){ ini_set(‘display_errors‘,1); error_reporting(E_ALL ^ E_NOTICE); }else{ ini_set(‘display_errors‘,0); error_reporting(0); } //判断DEBUG是否设置为1,为1开启错误提示,其余关闭错误提示 /* if(ini_get(‘register_globals‘)){ exit(‘php.ini register_globals must is off!‘); } //必须关闭注册为全局变量的使用 if(ini_get(‘session_auto_start‘)!=0){ exit(‘php.ini session_auto_start must is 0 !‘); } //必须关闭session的自动启动 if(!defined(‘STDIN‘) && !in_array(getsrv(‘REQUEST_METHOD‘),array(‘GET‘,‘POST‘,‘PUT‘,‘DELETE‘,‘OPTIONS‘,‘HEAD‘))){ exit(‘Invalid Request Method‘); } */ unset($_REQUEST,$HTTP_ENV_VARS,$HTTP_POST_VARS,$HTTP_GET_VARS,$HTTP_COOKIE_VARS,$HTTP_SERVER_VARS,$HTTP_POST_FILES); //过滤REQUEST和旧版本的php参数 define(‘VERSION‘,‘5.0.0.1403150‘); define(‘SYSDIR‘,realpath(__DIR__)); //__DIR__是php5.4以后才有的,之前的版本可以用dirname(__FILE__)实现 define(‘ABSROOT‘,substr(SYSDIR,0,-7)); if(!defined(‘APPIN‘)){ define(‘APPIN‘,‘App‘); } //定义应用的顶级目录,默认为App if(!defined(‘APPNAME‘)){ define(‘APPNAME‘,APP); } //定义应用的别名,默认APPNAME==APP // // define(‘APPROOT‘,ABSROOT.‘/‘.APPIN); //定义应用的顶级目录的绝对路径 define(‘APPDIR‘,APPROOT.‘/‘.APPNAME); //定义应用的绝对路径 define(‘APPNS‘,APPIN.‘\\‘.APPNAME); //定义命名空间,如App\\test date_default_timezone_set(‘Etc/GMT-8‘); //定义默认的时区 require SYSDIR . ‘/Core/common.php‘; //加载System/Core/common.php 文件 // spl_autoload_register(‘autoload‘); //注册自动加载函数 // // //$App = new System\Core\App(); //实例化应用类App //$App->run(); //运行App
common.php
<?php function autoload($className){ $classPath = ABSROOT . ‘/‘ . str_replace(‘\\‘,‘/‘,$className) . ‘.php‘; //根据className得到文件的路径,把\替换成/ if(is_file($classPath)){ //文件存在,加载文件 require $classPath; }else{ throw new \Exception($className.‘ does not exists.‘,404); //抛出异常 } } //$callback 1:NULL,调用函数。2:不为false,绑定函数,3,false,接触绑定 //$value 1,事件存在,为参数,2,事件不存在,为默认值 function event($event,$value=NULL,$callback=NULL){ static $_eventStore; if($callback===NULL){ if(isset($_eventStore[$event])){ foreach($_eventStore[$event] as $function){ $value = call_user_func($function,$value); } } return $value; }else{ if($callback){ $_eventStore[$event][] = $callback; }else{ unset($_eventStore[$event]); } } } function event2($event,$callback=NULL,$params=‘‘){ static $_eventMaps; if($callback === NULL){ foreach($_eventMaps[$event] as $function){ call_user_func_array($function,$params); } }else{ if(is_callable($callback)){ $_eventMaps[$event][] = $callback; }elseif(!$callback){ unset($_eventMaps[$event]); }else{ exit(‘error‘); } } } //$key=NULL,返回$_configStore; //$key=array,$_configStore = $key; //isset($_configStore[$key])?$_configStore[$key]:$defaultValue function config_item($key=NULL,$defaultValue=NULL){ static $_configStore; if($key === NULL){ return $_configStore; } if(is_array($key)){ $_configStore=$key; return ; } if(!isset($_configStore[$key])){ return $defaultValue; } return $_configStore[$key]; } function config_item2($key=‘‘,$value=‘‘){ static $_configMaps=array(); if(is_array($key)){ $_configMaps = array_merge($_configMaps,$key); }elseif($key == ‘‘){ return $_configMaps; }elseif($value == ‘‘){ return $_configMaps[$key]; }else{ $_configMaps[$key] = $value; } } //$key===NULL,return $_globalStore //$value===NULL,return $_globalStore[$key] //is_array($key)?$_globalStore+=$key:$_globalStore[$key]=$value; function global_item($key=NULL,$value=NULL){ static $_globalStore = array(); if($key === NULL){ return $_globalStore; } if($value===NULL){ if(isset($_globalStore[$key])){ return $_globalStore[$key]; } return NULL; }else{ if(is_array($key)){ $_globalStore += $key; }else{ $_globalStore[$key] = $value; } } } function global_item($key=NULL,$value=NULL){ static $_globalStore = array(); if($key === NULL){ return $_globalStore; } if($value===NULL){ if(isset($_globalStore[$key])){ return $_globalStore[$key]; } return NULL; }else{ if(is_array($key)){ $_globalStore+=$key; }else{ $_globalStore[$key] = $value; } } } //$expire<0,del($key); // function cache_item($key,$value=NULL,$expire=864000){i global $App; if($expire < 0){ return $App->Cache->del($key); } if($value===NULL){ return $App->Cache->get($key,$expire); }else{ $App->Cache->set($key,$value,$expire); } } function cache_item2($key,$value,$expire){ global $App; if($expire < 0){ return $App->Cache->del($key); } if($value===NULL){ return $App->Cache->get($key,$expire); }else{ return $App->Cache->set($key,$value,$expire); } } function get_item($key,$itemOption=NULL){ if($itemOption === TRUE){ if(isset($_GET[$key]))?numf($_GET[$key]):0; }else{ if(isset($_GET[$key]))?(is_string($_GET[$key])?strf($_GET[$key],$itemOption):$_GET[$key]):‘‘; } } function post_item($key,$itemOption=NULL){ if($itemOption === TRUE){ return isset($_POST[$key])?numf($_POST[$key]):0; }else{ return isset($_POST[$key])?(is_string($_POST[$key])?strf($_POST[$key],$itemOption):$_POST[$key]):‘‘; } } function cookie_item($key,$value=NULL,$expire=8640000){ $key = config_item(‘cookie_pre‘) . $key; if($value === NULL){ return isset($_COOKIE[$key])?trim($_COOKIE[$key]):‘‘; }else{ setcookie($key,$value,$expire?global_item(‘timestamp‘)+$expire : 0,‘/‘,config_item(‘cookie_domain‘)); } } function cookie_item2($key,$value=NULL,$expire=8640000){ $key=config_item(‘cookie_pre‘).$key; if($value===NULL){ return isset($_COOKIE[$key])?trim($_COOKIE[$key]):‘‘; }else{ setcookie($key,$value,$expire?global_item(‘timestamp‘)+$expire:0,‘/‘,config_item(‘cookie_domain‘)); } } function getsrv($key){ return isset($_SERVER[$key])?$_SERVER[$key]:‘‘; } function addslashess($var){ if(is_array($var)){ foreach($var as $key=>$val){ $var[$key] = addslashess($val); } }else{ $var = addslashes($var); } return $var; } function safe_filter($var){ return addslashess($var); } function numf($var){ return is_numeric($var) ? $var:intval($var); } function strf($var,$allowableTags=NULL){ return trim(($allowableTags)?$var:strip_tags($var,$allowableTags)) } function arrayf($var,$separator=‘,‘){ return is_array($var)?$var:explode($separator,$var); }
时间: 2024-10-21 05:48:09