* Support PHP8.1.

This commit is contained in:
鲁飞
2023-04-23 11:21:09 +00:00
committed by 朱金勇
parent 2ed6472bc8
commit 045289d4c7
15 changed files with 617 additions and 510 deletions
+33 -40
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* 禅道API的entry类。
* The entry class file of ZenTao API.
@@ -83,11 +83,10 @@ class baseEntry
* Get request data(POST or PUT)
*
* @param string $key
* @param mixed $defaultValue
* @access public
* @return mixed
*/
public function request($key, $defaultValue = '')
public function request(string $key, $defaultValue = '')
{
if(isset($this->requestBody->$key)) return $this->requestBody->$key;
return $defaultValue;
@@ -98,11 +97,10 @@ class baseEntry
* Get request params.
*
* @param string $key
* @param mixed $defaultValue
* @access public
* @return mixed
*/
public function param($key, $defaultValue = '')
public function param(string $key, $defaultValue = '')
{
if(isset($_GET[$key])) return $_GET[$key];
return $defaultValue;
@@ -113,11 +111,10 @@ class baseEntry
* Set request param.
*
* @param string|array $key if is array, set params by its key-value pairs.
* @param mixed $value
* @access public
* @return void
*/
public function setParam($key, $value = null)
public function setParam(string|array $key, $value = null)
{
if(is_array($key))
{
@@ -202,11 +199,10 @@ class baseEntry
* Send response data
*
* @param int $code
* @param mixed $data
* @access public
* @return string
*/
public function send($code, $data = '')
public function send(int $code, $data = '')
{
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
@@ -227,7 +223,7 @@ class baseEntry
* @access public
* @return string
*/
public function sendError($code, $msg)
public function sendError(int $code, string $msg)
{
$response = new stdclass();
$response->error = $msg;
@@ -244,7 +240,7 @@ class baseEntry
* @access public
* @return string
*/
public function sendSuccess($code, $msg)
public function sendSuccess(int $code, string $msg)
{
$response = new stdclass();
$response->message = $msg;
@@ -259,7 +255,7 @@ class baseEntry
* @access public
* @return string
*/
public function send400($message = 'error')
public function send400(string $message = 'error')
{
return $this->sendError(400, $message);
}
@@ -284,7 +280,7 @@ class baseEntry
* @access public
* @return object
*/
public function loadController($moduleName, $methodName)
public function loadController(string $moduleName, string $methodName)
{
ob_start();
@@ -315,7 +311,7 @@ class baseEntry
}
$file2Included = $isExt ? $app->extActionFile : $app->controlFile;
chdir(dirname($file2Included));
chdir(dirname((string) $file2Included));
helper::import($file2Included);
}
@@ -341,7 +337,7 @@ class baseEntry
* @access public
* @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object.
*/
public function loadModel($moduleName = '', $appName = '')
public function loadModel(string $moduleName = '', string $appName = '')
{
if(empty($moduleName)) $moduleName = $this->app->moduleName;
if(empty($appName)) $appName = $this->app->appName;
@@ -400,8 +396,8 @@ class baseEntry
public function getData()
{
$output = helper::removeUTF8Bom(ob_get_clean());
$output = json_decode($output);
if(isset($output->data)) $output->data = json_decode($output->data);
$output = json_decode((string) $output);
if(isset($output->data)) $output->data = json_decode((string) $output->data);
return $output;
}
@@ -411,7 +407,6 @@ class baseEntry
* Add data to $_POST.
*
* @param string $key
* @param mixed $value
* @access public
* @return void
*/
@@ -425,11 +420,10 @@ class baseEntry
* Batch set data to $_POST.
*
* @param string $fields
* @param mixed $object
* @access public
* @return void
*/
public function batchSetPost($fields, $object = '')
public function batchSetPost(string $fields, $object = '')
{
$fields = explode(',', $fields);
foreach($fields as $field)
@@ -457,11 +451,10 @@ class baseEntry
* Make sure the fields is not empty.
*
* @param string $fields
* @param mixed $object
* @access public
* @return void
*/
public function requireFields($fields)
public function requireFields(string $fields)
{
$fields = explode(',', $fields);
foreach($fields as $field)
@@ -469,7 +462,7 @@ class baseEntry
if(!isset($_POST[$field]))
{
$module = $this->app->moduleName;
$name = isset($this->app->lang->$module->$field) ? $this->app->lang->$module->$field : $field;
$name = $this->app->lang->$module->$field ?? $field;
throw EndResponseException::create($this->sendError(400, sprintf($this->app->lang->error->notempty, $name)));
}
}
@@ -484,7 +477,7 @@ class baseEntry
* @access public
* @return object|array
*/
public function format($data, $fields)
public function format(object|array $data, string $fields)
{
if(is_array($data))
{
@@ -504,7 +497,7 @@ class baseEntry
* @access public
* @return object
*/
private function formatFields(&$object, $fields)
private function formatFields(object &$object, string $fields)
{
$fields = explode(',', $fields);
@@ -523,7 +516,7 @@ class baseEntry
$isArray = true;
$type = substr($type, $pos + 1);
}
else if(strpos($type, 'array') !== false)
else if(str_contains($type, 'array'))
{
$isArray = true;
$type = 'object';
@@ -532,7 +525,7 @@ class baseEntry
/* Format value. */
if(!$isArray)
{
$object->$key = $this->cast(trim($object->$key, ','), $type);
$object->$key = $this->cast(trim((string) $object->$key, ','), $type);
continue;
}
@@ -559,11 +552,11 @@ class baseEntry
* Filter fields.
*
* @param object $object
* @param array $filters
* @param string $allowable
* @access public
* @return object
*/
public function filterFields($object, $allowable = '')
public function filterFields(object $object, string $allowable = '')
{
if(empty($allowable)) return $object;
if(is_string($allowable)) $allowable = explode(',', $allowable);
@@ -571,7 +564,7 @@ class baseEntry
$filtered = new stdclass();
foreach($allowable as $field)
{
$field = trim($field);
$field = trim((string) $field);
if(empty($field)) continue;
if(!isset($object->$field)) continue;
$filtered->$field = $object->$field;
@@ -583,12 +576,12 @@ class baseEntry
/**
* Format user.
*
* @param string $account
* @param array $users
* @param string $account
* @param array|object $users
* @access public
* @return array
*/
public function formatUser($account, $users)
public function formatUser(string $account, array|object $users)
{
$user = array();
$user['account'] = $account;
@@ -606,7 +599,7 @@ class baseEntry
* @access public
* @return mixed
*/
private function cast($value, $type)
private function cast($value, string $type)
{
switch($type)
{
@@ -615,7 +608,7 @@ class baseEntry
if($timeFormat == 'utc')
{
if(!$value or $value == '0000-00-00 00:00:00') return null;
return gmdate("Y-m-d\TH:i:s\Z", strtotime($value));
return gmdate("Y-m-d\TH:i:s\Z", strtotime((string) $value));
}
return $value;
case 'date':
@@ -626,7 +619,7 @@ class baseEntry
case 'int':
return (int) $value;
case 'idList':
$values = explode(',', $value);
$values = explode(',', (string) $value);
if(empty($values)) return array();
$idList = array();
@@ -636,7 +629,7 @@ class baseEntry
}
return $idList;
case 'stringList':
$values = explode(',', $value);
$values = explode(',', (string) $value);
if(empty($values)) return array();
$stringList = array();
@@ -654,7 +647,7 @@ class baseEntry
if(empty($this->users)) $this->users = $this->dao->select('id,account,avatar,realname')->from(TABLE_USER)->fetchAll('account');
return zget($this->users, $value, null);
case 'userList':
$values = explode(',', $value);
$values = explode(',', (string) $value);
if(empty($values)) return array();
$userList = array();
@@ -679,7 +672,7 @@ class baseEntry
* @access public
* @return mixed
*/
public function fetch($entry, $method, $params = array())
public function fetch(string $entry, string $method, array $params = array())
{
include($this->app->appRoot . "api/{$this->app->version}/entries/" . strtolower($entry) . ".php");
@@ -711,7 +704,7 @@ class baseEntry
* @access public
* @return void
*/
public function resetOpenApp($tab)
public function resetOpenApp(string $tab)
{
$_COOKIE['tab'] = $tab;
$this->app->tab = $tab;
+35 -35
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* ZenTaoAPI的helper类。
* The helper class file of ZenTao API.
@@ -10,10 +10,10 @@
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*/
include dirname(dirname(__FILE__)) . '/base/helper.class.php';
include dirname(__FILE__, 2) . '/base/helper.class.php';
class helper extends baseHelper
{
public static function getViewType($source = false)
public static function getViewType(bool $source = false)
{
global $config, $app;
if($config->requestType != 'GET')
@@ -21,10 +21,10 @@ class helper extends baseHelper
$pathInfo = $app->getPathInfo();
if(!empty($pathInfo))
{
$dotPos = strrpos($pathInfo, '.');
$dotPos = strrpos((string) $pathInfo, '.');
if($dotPos)
{
$viewType = substr($pathInfo, $dotPos + 1);
$viewType = substr((string) $pathInfo, $dotPos + 1);
}
else
{
@@ -46,8 +46,8 @@ class helper extends baseHelper
}
if($source and isset($viewType)) return $viewType;
if(isset($viewType) and strpos($config->views, ',' . $viewType . ',') === false) $viewType = $config->default->view;
return isset($viewType) ? $viewType : $config->default->view;
if(isset($viewType) and !str_contains((string) $config->views, ',' . $viewType . ',')) $viewType = $config->default->view;
return $viewType ?? $config->default->view;
}
/**
@@ -58,19 +58,19 @@ class helper extends baseHelper
* @access public
* @return bool
*/
public static function hasFeature($feature)
public static function hasFeature(string $feature)
{
global $config;
if(strpos($feature, '_') !== false)
if(str_contains($feature, '_'))
{
$code = explode('_', $feature);
$code = $code[0] . ucfirst($code[1]);
return strpos(",$config->disabledFeatures,", ",{$code},") === false;
return !str_contains(",$config->disabledFeatures,", ",{$code},");
}
else
{
if($feature == 'product' or $feature == 'scrum' or $feature == 'waterfall') return strpos(",$config->disabledFeatures,", ",{$feature},") === false;
if($feature == 'product' or $feature == 'scrum' or $feature == 'waterfall') return !str_contains(",$config->disabledFeatures,", ",{$feature},");
$hasFeature = false;
foreach($config->featureGroup as $group => $modules)
@@ -90,7 +90,7 @@ class helper extends baseHelper
}
}
}
return $hasFeature && strpos(",$config->disabledFeatures,", ",{$feature},") === false;
return $hasFeature && !str_contains(",$config->disabledFeatures,", ",{$feature},");
}
}
@@ -103,13 +103,13 @@ class helper extends baseHelper
* @access public
* @return string
*/
public static function jsonEncode4Parse($data, $options = 0)
public static function jsonEncode4Parse(array $data, int $options = 0)
{
$json = json_encode($data);
if($options) $json = str_replace(array("'", '"'), array('\u0027', '\u0022'), $json);
$escapers = array("\\", "/", "\"", "'", "\n", "\r", "\t", "\x08", "\x0c", "\\\\u");
$replacements = array("\\\\", "\\/", "\\\"", "\'", "\\n", "\\r", "\\t", "\\f", "\\b", "\\u");
$escapers = array("\\", "/", "\"", "'", "\n", "\r", "\t", "\x08", "\x0c", "\\\\u");
$replacements = array("\\\\", "\\/", "\\\"", "\'", "\\n", "\\r", "\\t", "\\f", "\\b", "\\u");
return str_replace($escapers, $replacements, $json);
}
@@ -123,7 +123,7 @@ class helper extends baseHelper
* @access public
* @return string
*/
public static function convertEncoding($string, $fromEncoding, $toEncoding = 'utf-8')
public static function convertEncoding(string $string, string $fromEncoding, string $toEncoding = 'utf-8')
{
$toEncoding = str_replace('utf8', 'utf-8', $toEncoding);
if(function_exists('mb_convert_encoding'))
@@ -133,7 +133,7 @@ class helper extends baseHelper
if($position !== false) $toEncoding = substr($toEncoding, 0, $position);
/* Check string encoding. */
$encodings = array_merge(array('GB2312','GBK','BIG5'), mb_list_encodings());
$encodings = array_merge(array('GB2312', 'GBK', 'BIG5'), mb_list_encodings());
$encoding = strtolower(mb_detect_encoding($string, $encodings));
if($encoding == $toEncoding) return $string;
return mb_convert_encoding($string, $toEncoding, $encoding);
@@ -155,10 +155,8 @@ class helper extends baseHelper
*
* @param string $begin
* @param string $end
*
* @return bool|float
*/
public static function workDays($begin, $end)
public static function workDays(string $begin, string $end): bool|float
{
$begin = strtotime($begin);
$end = strtotime($end);
@@ -180,7 +178,7 @@ class helper extends baseHelper
* @access public
* @return string
*/
public static function unify($string, $to = ',')
public static function unify(string $string, string $to = ',')
{
$labels = array('_', '、', ' ', '-', '?', '@', '&', '%', '~', '`', '+', '*', '/', '\\', ',', '。');
$string = str_replace($labels, $to, $string);
@@ -190,27 +188,29 @@ class helper extends baseHelper
/**
* Create url of issue.
*
* @param string $module
* @param string $method
* @param string $vars
* @param string $module
* @param string $method
* @param string|array $vars
* @param string $viewType
* @param bool $onlyBody
* @static
* @access public
* @return string
*/
static public function createLink($moduleName, $methodName = 'index', $vars = '', $viewType = 'json', $onlyBody = false)
static public function createLink(string $moduleName, string $methodName = 'index', string|array $vars = '', string $viewType = 'json', bool $onlyBody = false)
{
global $config;
$link = parent::createLink($moduleName, $methodName, $vars, $viewType);
$pos = strpos($link, '.php');
$pos = strpos((string) $link, '.php');
/* The requestTypes are: GET, PATH_INFO2, PATH_INFO */
if($config->requestType == 'GET')
{
$link = $config->webRoot . 'index' . substr($link, $pos);
$link = $config->webRoot . 'index' . substr((string) $link, $pos);
}
elseif($config->requestType == 'PATH_INFO2')
{
$link = substr($link, $pos + 4);
$link = substr((string) $link, $pos + 4);
}
return common::getSysURL() . $link;
}
@@ -221,9 +221,9 @@ class helper extends baseHelper
* Check exist onlybody param.
*
* @access public
* @return void
* @return bool
*/
function isonlybody()
function isonlybody(): bool
{
return helper::inOnlyBodyMode();
}
@@ -231,12 +231,12 @@ function isonlybody()
/**
* Format time.
*
* @param int $time
* @param string $time
* @param string $format
* @access public
* @return void
* @return string
*/
function formatTime($time, $format = '')
function formatTime(string $time, string $format = '')
{
$time = str_replace('0000-00-00', '', $time);
$time = str_replace('00:00:00', '', $time);
@@ -248,11 +248,11 @@ function formatTime($time, $format = '')
/**
* Fix for session error.
*
* @param int $class
* @param string $class
* @access protected
* @return void
*/
function autoloader($class)
function autoloader(string $class)
{
if(!class_exists($class))
{
+17 -17
View File
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* 禅道API的api类。
* The api class file of ZenTao API.
@@ -10,7 +10,7 @@
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*/
include dirname(dirname(__FILE__)) . '/router.class.php';
include dirname(__FILE__, 2) . '/router.class.php';
class api extends router
{
/**
@@ -76,11 +76,11 @@ class api extends router
* @access public
* @return void
*/
public function __construct($appName = 'api', $appRoot = '')
public function __construct(string $appName = 'api', string $appRoot = '')
{
parent::__construct($appName, $appRoot);
$this->httpMethod = strtolower($_SERVER['REQUEST_METHOD']);
$this->httpMethod = strtolower((string) $_SERVER['REQUEST_METHOD']);
/*
$documentRoot = zget($_SERVER, 'CONTEXT_DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT']);
@@ -91,7 +91,7 @@ class api extends router
if(strpos($this->path, '?') > 0) $this->path = strstr($this->path, '?', true);
*/
$this->path = trim(substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'api.php') + 7), '/');
$this->path = trim(substr((string) $_SERVER['REQUEST_URI'], strpos((string) $_SERVER['REQUEST_URI'], 'api.php') + 7), '/');
if(strpos($this->path, '?') > 0) $this->path = strstr($this->path, '?', true);
$subPos = $this->path ? strpos($this->path, '/') : false;
@@ -107,26 +107,26 @@ class api extends router
* Parse request path, find entry and action.
*
* @param array $routes
* @access private
* @access public
* @return void
*/
public function route($routes)
public function route(array $routes)
{
foreach($routes as $route => $target)
{
$patternAsRegex = preg_replace_callback(
'#:([\w]+)\+?#',
array($this, 'matchesCallback'),
$this->matchesCallback(...),
str_replace(')', ')?', $route)
);
if(substr($route, -1) === '/') $patternAsRegex .= '?';
if(str_ends_with($route, '/')) $patternAsRegex .= '?';
/* Cache URL params' names and values if this route matches the current HTTP request. */
if(!preg_match('#^' . $patternAsRegex . '$#', $this->path, $paramValues)) continue;
/* Set module and action */
$this->entry = $target;
$this->action = strtolower($_SERVER['REQUEST_METHOD']);
$this->action = strtolower((string) $_SERVER['REQUEST_METHOD']);
/* Set params */
foreach($this->paramNames as $name)
@@ -154,11 +154,11 @@ class api extends router
*
* Parse params of route to regular expression.
*
* @param string $param
* @param string $m
* @access protected
* @return string
*/
protected function matchesCallback($m)
protected function matchesCallback(string $m)
{
$this->paramNames[] = $m[1];
return '(?P<' . $m[1] . '>[^/]+)';
@@ -230,7 +230,7 @@ class api extends router
* @access public
* @return void
*/
public function loadApiConfig($configPath)
public function loadApiConfig(string $configPath)
{
global $config;
include($this->appRoot . "api/$this->version/config/$configPath.php");
@@ -260,17 +260,17 @@ class api extends router
* @access public
* @return string
*/
public function formatData($output)
public function formatData(string $output)
{
/* If the version exists, return output directly. */
if($this->version) return $output;
$output = json_decode($output);
$output = json_decode((string) $output);
$data = new stdClass();
$data->status = isset($output->status) ? $output->status : $output->result;
$data->status = $output->status ?? $output->result;
if(isset($output->message)) $data->message = $output->message;
if(isset($output->data)) $data->data = json_decode($output->data);
if(isset($output->data)) $data->data = json_decode((string) $output->data);
if(isset($output->id)) $data->id = $output->id;
$output = json_encode($data);