Skip to content

Commit

Permalink
持续改进
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonweicn committed Jul 16, 2017
1 parent bad284e commit 09f21c5
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 53 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
@@ -1,5 +1,12 @@
# CHANGES #

## Version 1.0.11 released. ( 2017-07-16 ) ##

* 改进转换伪静态地址分隔符的机制
* 优化路由处理伪静态时的性能
* 优化部分核心类的属性
* 优化框架内存占用

## Version 1.0.10 released. ( 2017-07-15 ) ##

* 新增支持使用“_”字符作为伪静态URL中的分隔符
Expand Down
20 changes: 2 additions & 18 deletions MiniFramework/Library/Mini/Action.php
Expand Up @@ -44,17 +44,10 @@ class Action
/**
* Request实例
*
* @var mixed
* @var Request
*/
protected $_request;

/**
* Action Instance
*
* @var Action
*/
protected static $_instance;

/**
* 构造
*
Expand All @@ -64,7 +57,6 @@ class Action
*/
function __construct()
{
self::$_instance = $this;
$this->view = new View();
$this->params = Params::getInstance();
$this->_request = Request::getInstance();
Expand All @@ -80,7 +72,7 @@ function __construct()
* @param mixed $variable
* @param mixed $value
*/
protected function assign($variable, $value)
final protected function assign($variable, $value)
{
$this->view->assign($variable, $value);
}
Expand All @@ -102,12 +94,4 @@ final protected function _forward($action, $controller = null, array $params = N

App::getInstance()->dispatch();
}

/**
* 获取Action实例
*/
public static function getInstance()
{
return self::$_instance;
}
}
9 changes: 3 additions & 6 deletions MiniFramework/Library/Mini/App.php
Expand Up @@ -113,7 +113,7 @@ protected function __construct()
public function run()
{
$requestParams = $this->_request->parseRequestParams($this->_router->getRouteType());

unset($this->_router);
if (! empty($requestParams)) {
$this->_params->setParams($requestParams);
}
Expand All @@ -132,17 +132,15 @@ public function run()
*/
public function dispatch()
{
$request = Request::getInstance();
$this->controller = $request->_controller;
$this->action = $request->_action;
$this->controller = $this->_request->_controller;
$this->action = $this->_request->_action;

$controllerName = ucfirst($this->controller);
$isApi = (REST_ON === true && $controllerName == 'Api') ? true : false;

if ($isApi === true) {

$apiName = ucfirst($this->action);

$headers = $this->_request->getHeaders();
if (isset($headers['Ver']) && preg_match("/^\d+$/", $headers['Ver'])) {
$apiName .= '_V' . $headers['Ver'];
Expand All @@ -165,7 +163,6 @@ public function dispatch()
throw new Exceptions('Api "' . $apiName . '" does not exist.', 404);
}
} else {

$controllerFile = APP_PATH . DS . 'Controller' . DS . $controllerName . '.php';

if (! file_exists($controllerFile)) {
Expand Down
3 changes: 3 additions & 0 deletions MiniFramework/Library/Mini/Params.php
Expand Up @@ -99,6 +99,9 @@ protected function __construct()
}
}
}

private function __clone()
{}

/**
* 获得一个整型变量
Expand Down
57 changes: 45 additions & 12 deletions MiniFramework/Library/Mini/Request.php
Expand Up @@ -74,7 +74,14 @@ class Request
*
* @var array
*/
protected $_headers = array();
private $_headers = array();

/**
* 预处理过的REQUEST_URI
*
* @var string
*/
private $_requestUri;

/**
* 获取实例
Expand All @@ -89,6 +96,9 @@ public static function getInstance()
return self::$_instance;
}

private function __clone()
{}

/**
* 从$_SERVER['PHP_SELF']中提取基础地址
*
Expand Down Expand Up @@ -197,24 +207,16 @@ public function parseRequestParams($routeType)
}
} elseif ($routeType == 'rewrite') {

$requestUri = '';
$requestUri = $this->getRequestUri();

if (empty($_SERVER['QUERY_STRING'])) {
$requestUri = $_SERVER['REQUEST_URI'];
} else {
$requestUri = str_replace('?' . $_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
if (! empty($_SERVER['QUERY_STRING'])) {
$queryStringArray = $this->getQueryStringArray();
}

if ($requestUri != $this->_baseUrl) {
$requestUri = str_replace($this->_baseUrl, '', $requestUri);
}

if (strtolower((substr($requestUri, -5))) == '.html') {
$requestUri = substr($requestUri, 0, -5);
$requestUri = str_replace('_', '/', $requestUri);
}

$uriArray = explode('/', $requestUri);

$array = null;
Expand Down Expand Up @@ -250,7 +252,7 @@ public function parseRequestParams($routeType)

/**
* 获取请求Header信息数组
*
*
* @param string $name
* @return multitype:
*/
Expand All @@ -270,4 +272,35 @@ public function getHeaders($name = null)

return $this->_headers;
}

/**
* 获取预处理后的REQUEST_URI
*
* @return string
*/
public function getRequestUri()
{
if ($this->_requestUri === null) {
$requestUri = '';
if (empty($_SERVER['QUERY_STRING'])) {
$requestUri = $_SERVER['REQUEST_URI'];
} else {
$requestUri = str_replace('?' . $_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
}

// 遇到.html结尾时,转换可能存在的伪静态分隔符
if (strtolower((substr($requestUri, - 5))) == '.html') {
$requestUri = substr($requestUri, 0, - 5);
$pos = strrpos($requestUri, '/');
$dir = substr($requestUri, 0, $pos);
$file = substr($requestUri, $pos);
$file = str_replace('_', '/', $file);
$requestUri = $dir . $file;
}

$this->_requestUri = $requestUri;
}

return $this->_requestUri;
}
}
16 changes: 1 addition & 15 deletions MiniFramework/Library/Mini/Router.php
Expand Up @@ -154,25 +154,11 @@ public function getRouteType()
*/
private function parseUrlToArray()
{
$requestUri = '';

if (empty($_SERVER['QUERY_STRING'])) {
$requestUri = $_SERVER['REQUEST_URI'];
} else {
$requestUri = str_replace('?' . $_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
}

$requestUri = $this->_request->getRequestUri();
$baseUrl = $this->_request->getBaseUrl();

if ($requestUri != $baseUrl) {
$requestUri = str_replace($baseUrl, '', $requestUri);
}

if (strtolower((substr($requestUri, -5))) == '.html') {
$requestUri = substr($requestUri, 0, -5);
$requestUri = str_replace('_', '/', $requestUri);
}

$uriArray = explode('/', $requestUri);

return $uriArray;
Expand Down
4 changes: 2 additions & 2 deletions MiniFramework/Library/Mini/View.php
Expand Up @@ -112,7 +112,7 @@ public function assign($variable, $value)
/**
* 显示
*/
public function display()
final public function display()
{
$view = APP_PATH . DS . 'View' . DS;
$view .= strtolower($this->_controller) . DS . $this->_action . '.php';
Expand Down Expand Up @@ -144,7 +144,7 @@ public function display()
* (true | false)
* @return string
*/
public function render($script, $check = true)
final public function render($script, $check = true)
{
if ($check === true) {
if (! file_exists($script)) {
Expand Down

0 comments on commit 09f21c5

Please sign in to comment.