Add EndResponseException and helper::die()

This commit is contained in:
Yurun
2021-12-21 09:47:31 +08:00
parent 397f0b587c
commit 2ca28f589f
4 changed files with 145 additions and 71 deletions
+81 -68
View File
@@ -12,6 +12,7 @@
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*/
include dirname(__DIR__) . '/exception/EndResponseException.class.php';
class baseRouter
{
/**
@@ -1831,88 +1832,100 @@ class baseRouter
*/
public function loadModule()
{
$appName = $this->appName;
$moduleName = $this->moduleName;
$methodName = $this->methodName;
try {
$appName = $this->appName;
$moduleName = $this->moduleName;
$methodName = $this->methodName;
/*
* 引入该模块的control文件。
* Include the control file of the module.
**/
$file2Included = $this->setActionExtFile() ? $this->extActionFile : $this->controlFile;
chdir(dirname($file2Included));
helper::import($file2Included);
/*
* 引入该模块的control文件。
* Include the control file of the module.
**/
$file2Included = $this->setActionExtFile() ? $this->extActionFile : $this->controlFile;
chdir(dirname($file2Included));
helper::import($file2Included);
/*
* 设置control的类名。
* Set the class name of the control.
**/
$className = class_exists("my$moduleName") ? "my$moduleName" : $moduleName;
if(!class_exists($className)) $this->triggerError("the control $className not found", __FILE__, __LINE__, $exit = true);
/*
* 设置control的类名。
* Set the class name of the control.
**/
$className = class_exists("my$moduleName") ? "my$moduleName" : $moduleName;
if(!class_exists($className)) $this->triggerError("the control $className not found", __FILE__, __LINE__, $exit = true);
/*
* 创建control类的实例。
* Create a instance of the control.
**/
$module = new $className();
if(!method_exists($module, $methodName)) $this->triggerError("the module $moduleName has no $methodName method", __FILE__, __LINE__, $exit = true);
$this->control = $module;
/*
* 创建control类的实例。
* Create a instance of the control.
**/
$module = new $className();
if(!method_exists($module, $methodName)) $this->triggerError("the module $moduleName has no $methodName method", __FILE__, __LINE__, $exit = true);
$this->control = $module;
/* include default value for module*/
$defaultValueFiles = glob($this->getTmpRoot() . "defaultvalue/*.php");
if($defaultValueFiles) foreach($defaultValueFiles as $file) include $file;
/* include default value for module*/
$defaultValueFiles = glob($this->getTmpRoot() . "defaultvalue/*.php");
if($defaultValueFiles) foreach($defaultValueFiles as $file) include $file;
/*
* 使用反射机制获取函数参数的默认值。
* Get the default settings of the method to be called using the reflecting.
*
* */
$defaultParams = array();
$methodReflect = new reflectionMethod($className, $methodName);
foreach($methodReflect->getParameters() as $param)
{
$name = $param->getName();
$default = '_NOT_SET';
if(isset($paramDefaultValue[$appName][$className][$methodName][$name]))
/*
* 使用反射机制获取函数参数的默认值。
* Get the default settings of the method to be called using the reflecting.
*
* */
$defaultParams = array();
$methodReflect = new reflectionMethod($className, $methodName);
foreach($methodReflect->getParameters() as $param)
{
$default = $paramDefaultValue[$appName][$className][$methodName][$name];
}
elseif(isset($paramDefaultValue[$className][$methodName][$name]))
{
$default = $paramDefaultValue[$className][$methodName][$name];
}
elseif($param->isDefaultValueAvailable())
{
$default = $param->getDefaultValue();
$name = $param->getName();
$default = '_NOT_SET';
if(isset($paramDefaultValue[$appName][$className][$methodName][$name]))
{
$default = $paramDefaultValue[$appName][$className][$methodName][$name];
}
elseif(isset($paramDefaultValue[$className][$methodName][$name]))
{
$default = $paramDefaultValue[$className][$methodName][$name];
}
elseif($param->isDefaultValueAvailable())
{
$default = $param->getDefaultValue();
}
$defaultParams[$name] = $default;
}
$defaultParams[$name] = $default;
/**
* 根据PATH_INFO或者GET方式设置请求的参数。
* Set params according PATH_INFO or GET.
*/
if($this->config->requestType != 'GET')
{
$this->setParamsByPathInfo($defaultParams);
}
else
{
$this->setParamsByGET($defaultParams);
}
if($this->config->framework->filterParam == 2)
{
$_GET = validater::filterParam($_GET, 'get');
$_COOKIE = validater::filterParam($_COOKIE, 'cookie');
}
/* 调用该方法 Call the method. */
call_user_func_array(array($module, $methodName), $this->params);
$this->checkAPIFile();
return $module;
} catch (EndResponseException $endResponseException) {
echo $endResponseException->getContent();
}
/**
* 根据PATH_INFO或者GET方式设置请求的参数。
* Set params according PATH_INFO or GET.
*/
if($this->config->requestType != 'GET')
if (isset($module))
{
$this->setParamsByPathInfo($defaultParams);
return $module;
}
else
{
$this->setParamsByGET($defaultParams);
return false;
}
if($this->config->framework->filterParam == 2)
{
$_GET = validater::filterParam($_GET, 'get');
$_COOKIE = validater::filterParam($_COOKIE, 'cookie');
}
/* 调用该方法 Call the method. */
call_user_func_array(array($module, $methodName), $this->params);
$this->checkAPIFile();
return $module;
}
/**
@@ -0,0 +1,45 @@
<?php
/**
* ZenTaoPHP的EndResponseException类。
* The EndResponseException class file of ZenTaoPHP framework.
*
* @package framework
*
* The author disclaims copyright to this source code. In place of
* a legal notice, here is a blessing:
*
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*/
class EndResponseException extends \Exception
{
/**
* 响应内容
*
* @var string
*/
private $content;
/**
* @param string $content
*
* @return sellf
*/
public static function create($content = '')
{
$exception = new self;
$exception->content = $content;
return $exception;
}
/**
* Get 响应内容
*
* @return string
*/
public function getContent()
{
return $this->content;
}
}
+11
View File
@@ -248,6 +248,17 @@ class helper extends baseHelper
if(empty($jsonDecode)) return $response;
return $jsonDecode;
}
/**
* 代替 die、exit 函数终止并输出
*
* @param string $content
* @return void
*/
public static function die($content)
{
throw EndResponseException::create($content);
}
}
/**
+8 -3
View File
@@ -816,14 +816,19 @@ class my extends control
*/
public function editProfile()
{
if($this->app->user->account == 'guest') die(js::alert('guest') . js::locate('back'));
if($this->app->user->account == 'guest')
{
echo js::alert('guest'), js::locate('back');
return;
}
if(!empty($_POST))
{
$_POST['account'] = $this->app->user->account;
$_POST['groups'] = $this->dao->select('`group`')->from(TABLE_USERGROUP)->where('account')->eq($this->post->account)->fetchPairs('group', 'group');
$this->user->update($this->app->user->id);
if(dao::isError()) die(js::error(dao::getError()));
die(js::locate($this->createLink('my', 'profile'), 'parent'));
if(dao::isError()) helper::die(js::error(dao::getError()));
echo js::locate($this->createLink('my', 'profile'), 'parent');
return;
}
$this->app->loadConfig('user');