Files
juipphp/fastphp/Fastphp.php

172 lines
5.3 KiB
PHP
Raw Normal View History

2020-10-03 17:23:32 +08:00
<?php
namespace fastphp;
//框架根目录
//use function Composer\Autoload\includeFile;
2020-11-04 15:40:58 +08:00
define('CORE_PATH', __DIR__);
2020-10-03 17:23:32 +08:00
/**
* fastphp框架核心
*/
class Fastphp
{
//配置内容
protected $config = [];
public function __construct($config)
{
$this->config = $config;
2020-11-04 15:40:58 +08:00
require CORE_PATH . '/func/common.php';
2020-10-03 17:23:32 +08:00
}
//运行程序
2020-11-04 15:40:58 +08:00
public function run()
{
2020-10-05 13:43:53 +08:00
global $is_script;
2020-11-04 15:40:58 +08:00
spl_autoload_register(array($this, 'loadClass'));
2020-10-11 19:23:42 +08:00
$this->origin();
2020-10-03 17:23:32 +08:00
$this->setReporting();
$this->unregisterGlobals();
$this->setDbConfig();
2020-11-04 15:40:58 +08:00
if (!$is_script) {
2020-10-05 13:43:53 +08:00
$this->route();
2020-11-04 15:40:58 +08:00
} else {
if (isset($_SERVER['SERVER_NAME'])) {
die;
}
2020-10-05 13:43:53 +08:00
}
2020-10-03 17:23:32 +08:00
}
2020-10-11 19:23:42 +08:00
//跨域请求处理
2020-11-04 15:40:58 +08:00
public function origin()
{
$allow_origin = $this->config['origin']; //跨域访问的时候才会存在此字段
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
2020-10-11 19:23:42 +08:00
if (in_array($origin, $allow_origin)) {
header('Access-Control-Allow-Origin:' . $origin);
header("Access-Control-Allow-Methods: *");
header('Access-Control-Allow-Headers:*');
header("Access-Control-Allow-Credentials: true");
} else {
return;
}
}
2020-10-03 17:23:32 +08:00
//路由处理
2020-11-04 15:40:58 +08:00
public function route()
{
2020-10-03 17:23:32 +08:00
$controllerName = $this->config['defaultController'];
$actionName = $this->config['defaultAction'];
$param = array();
$url = $_SERVER['REQUEST_URI'];
//清除?之后的内容
2020-11-04 15:40:58 +08:00
$position = strpos($url, '?');
$url = $position === false ? $url : substr($url, 0, $position);
2020-10-03 17:23:32 +08:00
//删除前后的/
2020-11-04 15:40:58 +08:00
$url = trim($url, '/');
2020-10-03 17:23:32 +08:00
2020-11-04 15:40:58 +08:00
if ($url) {
2020-10-03 17:23:32 +08:00
//使用 / 分割
2020-11-04 15:40:58 +08:00
$urlArray = explode('/', $url);
2020-10-03 17:23:32 +08:00
//删除空的数组元素
$urlArray = array_filter($urlArray);
//获取控制器名
2020-11-04 15:40:58 +08:00
$controllerName = array_shift($urlArray) . '\\controller\\' . ucfirst(array_shift($urlArray));
2020-10-03 17:23:32 +08:00
//获取动作名
2020-11-04 15:40:58 +08:00
$actionName = $urlArray ? array_shift($urlArray) : $actionName;
2020-10-03 17:23:32 +08:00
//获取url参数
2020-11-04 15:40:58 +08:00
$param = $urlArray ? $urlArray : array();
2020-10-03 17:23:32 +08:00
}
//判断控制器和操作是否存在
2020-11-04 15:40:58 +08:00
$controller = 'app\\' . $controllerName;
if (!class_exists($controller)) {
exit($controller . '控制器不存在!');
2020-10-03 17:23:32 +08:00
}
2020-11-04 15:40:58 +08:00
if (!method_exists($controller, $actionName)) {
exit($actionName . '方法不存在!');
2020-10-03 17:23:32 +08:00
}
//实例化控制器这个后续结合controller基类一起看
2020-11-04 15:40:58 +08:00
$dispatch = new $controller($controllerName, $actionName);
2020-10-03 17:23:32 +08:00
//以下等同于$dispatch->$actionName($param);
2020-11-04 15:40:58 +08:00
call_user_func_array(array($dispatch, $actionName), $param);
2020-10-03 17:23:32 +08:00
}
//检测开发环境
2020-11-04 15:40:58 +08:00
public function setReporting()
{
if (APP_DEBUG === true) {
2020-10-03 17:23:32 +08:00
error_reporting(E_ALL);
2020-11-04 15:40:58 +08:00
ini_set('display_errors', 'On');
2020-10-03 17:23:32 +08:00
} else {
error_reporting(E_ALL);
2020-11-04 15:40:58 +08:00
ini_set('display_errors', 'Off');
ini_set('log_errors', 'On');
2020-10-03 17:23:32 +08:00
}
}
//删除敏感字符
2020-11-04 15:40:58 +08:00
public function stripSlashesDeep($value)
{
$value = is_array($value) ? array_map(array($this, 'stripSlashesDeep'), $value) : stripSlashes($value);
2020-10-03 17:23:32 +08:00
return $value;
}
//检测自定义全局变量并移除
2020-11-04 15:40:58 +08:00
public function unregisterGlobals()
{
if (ini_get('register_globals')) {
$array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
foreach ($array as $value) {
foreach ($GLOBALS[$value] as $key => $var) {
if ($var === $GLOBALS[$key]) {
2020-10-03 17:23:32 +08:00
unset($GLOBALS[$key]);
}
}
}
}
}
//配置数据库信息
2020-11-04 15:40:58 +08:00
public function setDbConfig()
{
2020-11-17 10:18:30 +08:00
$formal_origin = $this->config['formal_origin']; //跨域访问的时候才会存在此字段
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
2020-11-17 10:29:49 +08:00
if (!in_array($origin, $formal_origin)) {
2020-11-04 15:40:58 +08:00
define('DB_HOST', $this->config['db']['host']);
define('DB_PORT', $this->config['db']['port']);
define('DB_NAME', $this->config['db']['dbname']);
define('DB_USER', $this->config['db']['username']);
define('DB_PASS', $this->config['db']['password']);
2020-11-17 10:29:49 +08:00
} else {
define('DB_HOST', $this->config['formal_db']['host']);
define('DB_PORT', $this->config['formal_db']['port']);
define('DB_NAME', $this->config['formal_db']['dbname']);
define('DB_USER', $this->config['formal_db']['username']);
define('DB_PASS', $this->config['formal_db']['password']);
2020-10-03 17:23:32 +08:00
}
}
//自动加载类
2020-11-04 15:40:58 +08:00
public function loadClass($className)
{
$vendor = substr($className, 0, strpos($className, '\\'));
2020-10-03 17:23:32 +08:00
$vendorDIR = $this->config['namespace'][$vendor];
2020-11-04 15:40:58 +08:00
$filePath = substr($className, strlen($vendor)) . '.php';
2020-10-03 17:23:32 +08:00
//包含应用文件
2020-11-04 15:40:58 +08:00
$file = strtr($vendorDIR . $filePath, '\\', DIRECTORY_SEPARATOR);
if (!is_file($file)) {
2020-10-03 17:23:32 +08:00
return;
}
include $file;
}
}