一步步搭建自己的轻量级MVCphp框架-(四)一个国产轻量级框架Amysql源码分析(3) 总进程对象

AmysqlProcess类,框架的总进程对象 ./Amysql/Amysql.php

下面还是和以前一样,先上代码~

class AmysqlProcess {

    public $AmysqlController;
public $ControllerName;
public $ActionName;
public $ControllerFile; function ProcessStart()
{
global $Config;
if ($Config['HttpPath'])
{
$GETParam = (strlen($_SERVER['REQUEST_URI']) > strlen($_SERVER['SCRIPT_NAME'])) ? explode('/', trim(str_ireplace($_SERVER['SCRIPT_NAME'], '', $_SERVER['REQUEST_URI']), '/')) : '';
$GETCount = count($GETParam);
if($GETCount > 1)
for ($i=2; $i<$GETCount; ++$i) $_GET[$GETParam[$i]] = isset($GETParam[++$i]) ? $GETParam[$i] : '';
} $magic_quotes = function_exists('get_magic_quotes_gpc') ? get_magic_quotes_gpc() : false; // 环境是否有过滤
if($Config['Filter']) // 开启过滤
{
( !$magic_quotes && (Amysql::filter($_GET, 'addslashes') && Amysql::filter($_POST, 'addslashes') && Amysql::filter($_COOKIE, 'addslashes') && Amysql::filter($_FILES, 'addslashes')) );
}
else
{
( $magic_quotes && (Amysql::filter($_GET, 'stripslashes') && Amysql::filter($_POST, 'stripslashes') && Amysql::filter($_COOKIE, 'stripslashes') && Amysql::filter($_FILES, 'stripslashes')) );
} $this -> ControllerName = !empty($GETParam[0]) ? $GETParam[0] : ( (isset($_GET[$Config['UrlControllerName']]) && !empty($_GET[$Config['UrlControllerName']])) ? $_GET[$Config['UrlControllerName']] : 'index');
$this -> ControllerName = str_replace(_PathTag, DIRECTORY_SEPARATOR, $this -> ControllerName);
$this -> ActionName = !empty($GETParam[1]) ? $GETParam[1] : ( (isset($_GET[$Config['UrlActionName']]) && !empty($_GET[$Config['UrlActionName']])) ? $_GET[$Config['UrlActionName']] : 'IndexAction'); if(!preg_match('/^[A-Za-z](\w)*$/',$this -> ControllerName) || !preg_match('/^[A-Za-z](\w)*$/',$this -> ActionName))
Amysql::AmysqlNotice('路由地址非法!'); $this -> ControllerFile = _Controller . $this -> ControllerName . '.php';
} function ControllerStart()
{
if(!in_array($this -> ActionName, get_class_methods($this -> ControllerName), true))
Amysql::AmysqlNotice($this -> ActionName . ' 方法不存在'); define("ControllerName", $this->ControllerName);
define("ActionName", $this -> ActionName); $this -> AmysqlController = new $this->ControllerName($_GET); // 实例控制器
$this -> AmysqlController -> {$this -> ActionName}(); // 执行方法
}
}

首先来说一下这个ProcessStart()进程开始的方法

在这个方法的第一句就是使用global来引入一个全局变量$Config,这个$Config是一个数组,储存了很多框架的初始化配置,详细可以点击这里看一下~

之后便是使用$Config['HttpPath']进行判断是否开启 index.php/Controller/Action/name/value 这样的URL模式,这个在config配置文件里面也说过。

如果开始这样的URL模式,那么就对这个URL进行拆解,然后把内容放入到$_GET这个超全局变量里面。


现在针对$_SERVER['REQUEST_URI']和$_SERVER['SCRIPT_NAME']进行一下详细说明

REQUEST_URI 用来指定要访问的页面,包含后面的参数。

SCRIPT_NAME 包含当前脚本的路径,是不带参数的。

这里的参数就是指在URL地址?后面的部分

例如下面这几行代码

<?php
echo 'REQUEST_URI : '.$_SERVER['REQUEST_URI']."<hr>";
echo 'SCRIPT_NAME : '.$_SERVER['SCRIPT_NAME'];

我访问http://127.0.0.1/cs/uri.php/aaa/bbb它输出的结果是

一步步搭建自己的轻量级MVCphp框架-(四)一个国产轻量级框架Amysql源码分析(3) 总进程对象


下面详细分析一下这一句的含义

(strlen($_SERVER['REQUEST_URI']) > strlen($_SERVER['SCRIPT_NAME'])) ? explode('/', trim(str_ireplace($_SERVER['SCRIPT_NAME'], '', $_SERVER['REQUEST_URI']), '/')) : '';

这一句的主体是一个三元运算符,条件是strlen($_SERVER['REQUEST_URI']) > strlen($_SERVER['SCRIPT_NAME']),就是说当URL地址中含有参数时那么就对这个参数进行分割,否则就返回一个空格。

对参数进行分割的时候首先使用str_ireplace吧URL地址中的SCRIPT_NAME这一部分给去掉,只留下参数部分。

比如上面的那一个示例,现在使用下面这行代码输出一下

<?php
echo str_ireplace($_SERVER['SCRIPT_NAME'], '', $_SERVER['REQUEST_URI']);

一步步搭建自己的轻量级MVCphp框架-(四)一个国产轻量级框架Amysql源码分析(3) 总进程对象

所以就只剩下参数了~

而trim它的作用就是把URL前后有或没有/的都变为不带有/的,比如说/aaa/bbb/就给转换成aaa/bbb

然后再使用explode函数把这个字符串按照"/"进行分割拆解并放入数组$GETParam中。

之所以后面的for循环是从2开始是因为$GETParam[0]代表的是控制器,$GETParam[1]代表的是方法。这些都不是参数,放到$_GET里面的都是一些参数信息


下面的控制器名和方法名都是先检测$GETParam是否存在,如果存在就用$GETParam中的,如果不存在那么就检测按照在config中定义的控制器名称和方法名称去$_GET超全局变量中寻找,如果找不到那么就使用默认的。


下面说一下ControllerStart()函数,这个函数是用来开启控制器的,根据控制器名来创建新的对象放到$this -> AmysqlController中。

这个创建对象使用了一个自动加载魔术函数__autoload(这个函数被写到了function.php文件中。。开始一直都没有找到。。/(ㄒoㄒ)/)

function __autoload($className)
{
if(!file_exists(_Controller . $className . '.php')) Amysql::AmysqlNotice('控制器文件不存在');
include_once _Controller . $className . '.php';
if(!class_exists($className)) Amysql::AmysqlNotice('控制器不存在');
}

这一段的分析就到此结束了~

上一篇:Backbone,Marionette,Talent学习笔记


下一篇:backbone入门示例