123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654 |
- <?php
- namespace Qii\Base;
- use Qii\Config\Consts;
- use Qii\Config\Register;
- use Qii\Request\Url;
- abstract class Request
- {
- public $host = '';
- public $module;
- public $controller;
- public $action;
- public $method;
- public $dispatcher;
- protected $params;
- protected $language;
- protected $_exception;
- protected $_baseUri = '';
- protected $uri = '';
- protected $dispatched = false;
- protected $routed = false;
- protected $forward = false;
- /**
- * @var Qii\Request\Url url
- */
- public $url;
- /**
- * 初始化参数,获取链接中对应的参数并存放到$this->params中
- *
- */
- public function __construct()
- {
- foreach ($_GET AS $key => $val) {
- $this->setParam($key, $val);
- }
- $rewriteRule = \Qii::getInstance()->appConfigure(Consts::APP_SITE_METHOD);
- //如果获取配置文件失败,就是会用默认短链的形式
- if ($rewriteRule == null) $rewriteRule = "Short";
- $this->url = new Url($rewriteRule);
- $this->host = IS_CLI ? '' : $_SERVER['HTTP_HOST'];
- $params = (array)$this->url->getParams();
- if(count($params) > 0) $this->params = array_merge($this->params, $params);
- $routeInfo = (array)$this->url->getPathArgs();
- $controller = $this->defaultController();
- $action = $this->defaultAction();
- //cli 模式处理
- if(IS_CLI) {
- $routeInfo = $this->url->request->getCliPathArgs();
- }
- if(count($routeInfo) > 1)
- {
- $action = array_pop($routeInfo);
- $controller = join("\\", $routeInfo);
- }
- else if(count($routeInfo) == 1 && !empty($routeInfo[0])) {
- $controller = $routeInfo[0];
- } //处理url中的数据
- if (ucwords($rewriteRule) == 'Short') {
- $this->setControllerName($controller);
- $this->setActionName($action);
- }
- return $this;
- }
- /**
- * 获取POST数据
- */
- public function post($name = null, $default = null)
- {
- return call_user_func_array(array($this->url, 'post'), func_get_args());
- }
- /**
- * 默认controller
- */
- public function defaultController()
- {
- return Register::get(Consts::APP_DEFAULT_CONTROLLER, 'index');
- }
- /**
- * 默认action
- */
- public function defaultAction()
- {
- return Register::get(Consts::APP_DEFAULT_ACTION, 'index');
- }
- /**
- * 获取当前数据处理
- */
- public function uri()
- {
- return $this->uri;
- }
- public function redirect($url)
- {
- ob_clean();
- header('Location:' . $url);
- }
- /**
- * isGet
- *
- * @param void
- * @return boolean
- */
- public function isGet()
- {
- return (strtoupper($this->method) == 'GET');
- }
- /**
- * isPost
- *
- * @param void
- * @return boolean
- */
- public function isPost()
- {
- return (strtoupper($this->method) == 'POST');
- }
- /**
- * isPut
- *
- * @param void
- * @return boolean
- */
- public function isPut()
- {
- return (strtoupper($this->method) == 'PUT');
- }
- /**
- * isHead
- *
- * @param void
- * @return boolean
- */
- public function isHead()
- {
- return (strtoupper($this->method) == 'HEAD');
- }
- /**
- * isOptions
- *
- * @param void
- * @return boolean
- */
- public function isOptions()
- {
- return (strtoupper($this->method) == 'OPTIONS');
- }
- /**
- * isCli
- *
- * @param void
- * @return boolean
- */
- public function isCli()
- {
- return (strtoupper($this->method) == 'CLI');
- }
- /**
- * isXmlHttpRequest
- *
- * @param void
- * @return boolean
- */
- public function isXmlHttpRequest()
- {
- return false;
- }
- /**
- * getServer
- *
- * @param string $name
- * @param mixed $default
- * @return mixed
- */
- public function getServer($name = null, $default = null)
- {
- if (is_null($name)) {
- return $_SERVER;
- } elseif (isset($_SERVER[$name])) {
- return $_SERVER[$name];
- }
- return $default;
- }
- /**
- * getEnv
- *
- * @param string $name
- * @param mixed $default
- * @return mixed
- */
- public function getEnv($name = null, $default = null)
- {
- if (is_null($name)) {
- return $_ENV;
- } elseif (isset($_ENV[$name])) {
- return $_ENV[$name];
- }
- return $default;
- }
- /**
- * setParam
- *
- * @param mixed $name
- * @param mixed $value
- * @return boolean | Qii_Request_Abstract
- */
- public function setParam($name, $value = null)
- {
- if (is_null($value)) {
- if (is_array($name)) {
- $this->params = array_merge($this->params, $name);
- return $this;
- }
- } elseif (is_string($name)) {
- $this->params[$name] = $value;
- return $this;
- }
- return false;
- }
- /**
- * getParam
- *
- * @param string $name
- * @param mixed $default
- * @return mixed
- */
- public function getParam($name, $dafault = null)
- {
- if (isset($this->params[$name])) {
- return $this->params[$name];
- }
- return $dafault;
- }
- /**
- * setParams
- *
- * @param array
- * @return boolean | Qii_Request_Abstract
- */
- public function setParams($params)
- {
- if (is_array($params)) {
- $this->params = $params;
- return $this;
- }
- return false;
- }
- /**
- * getParams
- *
- * @param void
- * @return array
- */
- public function getParams()
- {
- return $this->params;
- }
- /**
- * setException
- *
- * @param Exception $exception
- * @return boolean | Qii_Request_Abstract
- */
- public function setException($exception)
- {
- if (is_object($exception)
- && ($exception instanceof Exception)
- ) {
- $this->_exception = $exception;
- return $this;
- }
- return false;
- }
- /**
- * getException
- *
- * @param void
- * @return Exception
- */
- public function getException()
- {
- if (is_object($this->_exception)
- && ($this->_exception instanceof Exception)
- ) {
- return $this->_exception;
- }
- return null;
- }
- /**
- * getModuleName
- *
- * @param void
- * @return string
- */
- public function getModuleName()
- {
- return $this->module;
- }
- /**
- * getControllerName
- *
- * @param void
- * @return string
- */
- public function getControllerName()
- {
- return $this->controller;
- }
- /**
- * getActionName
- *
- * @param void
- * @return string
- */
- public function getActionName()
- {
- return $this->action;
- }
- /**
- * setModuleName
- *
- * @param string $name
- * @return boolean | Qii_Request_Abstract
- */
- public function setModuleName($name)
- {
- if (!is_string($name)) {
- trigger_error('Expect a string module name', E_USER_WARNING);
- return false;
- }
- $this->module = $name;
- return $this;
- }
- /**
- * setControllerName
- *
- * @param string $name
- * @return boolean | Qii_Request_Abstract
- */
- public function setControllerName($name)
- {
- if (!is_string($name)) {
- trigger_error('Expect a string controller name', E_USER_WARNING);
- return $this;
- }
- $this->controller = $name;
- return $this;
- }
- /**
- * setActionName
- *
- * @param string $name
- * @return boolean | Qii_Request_Abstract
- */
- public function setActionName($name)
- {
- if (!is_string($name)) {
- trigger_error('Expect a string action name', E_USER_WARNING);
- return false;
- }
- $this->action = $name;
- return $this;
- }
- /**
- * getMethod
- *
- * @param void
- * @return string
- */
- public function getMethod()
- {
- return $this->method;
- }
- /**
- * getLanguage
- *
- * @param void
- * @return string
- */
- public function getLanguage()
- {
- return $this->language;
- }
- /**
- * setBaseUri
- *
- * @param string $baseUri
- * @return boolean | Qii_Request_Abstract
- */
- public function setBaseUri($baseUri)
- {
- if ($baseUri && is_string($baseUri)) {
- $this->_baseUri = $baseUri;
- return $this;
- }
- return false;
- }
- /**
- * getBaseUri
- *
- * @param void
- * @return string
- */
- public function getBaseUri()
- {
- return $this->_baseUri;
- }
- /**
- * setRequestUri
- *
- * @param string $uri
- * @return boolean | Qii_Request_Abstract
- */
- public function setRequestUri($uri)
- {
- if (is_string($uri)) {
- $this->uri = $uri;
- return $this;
- }
- return false;
- }
- /**
- * getRequestUri
- *
- * @param void
- * @return string
- */
- public function getRequestUri()
- {
- return $this->uri;
- }
- /**
- * isDispatched
- *
- * @param void
- * @return boolean
- */
- public function isDispatched()
- {
- return (boolean)$this->dispatched;
- }
- /**
- * setDispatched
- *
- * @param boolean $flag
- * @return boolean | Qii_Request_Abstract
- */
- public function setDispatched($flag = true)
- {
- if (is_bool($flag)) {
- $this->dispatched = $flag;
- return $this;
- }
- return false;
- }
- /**
- * 设置dispatcher
- *
- * @param \Qii\Controller\Dispatcher $dispatcher
- */
- public function setDispatcher(\Qii\Base\Dispatcher $dispatcher)
- {
- $this->dispatcher = $dispatcher;
- }
- /**
- * isRouted
- *
- * @param void
- * @return boolean
- */
- public function isRouted()
- {
- return $this->routed;
- }
- /**
- * setRouted
- *
- * @param boolean $flag
- * @return boolean | \Qii\Request\Abstract
- */
- public function setRouted($flag = true)
- {
- if (is_bool($flag)) {
- $this->routed = $flag;
- return $this;
- }
- return false;
- }
- /**
- * 是否需要转发
- */
- public function isForward()
- {
- return $this->forward;
- }
- /**
- * 设置是否需要转发
- * @param bool $flag
- * @return $this
- */
- public function setForward($flag = true)
- {
- if (is_bool($flag)) {
- $this->forward = $flag;
- return $this;
- }
- return $this;
- }
- /**
- * __setbaseUri
- *
- * @param string $baseUri
- * @param string $requestUri
- * @return boolean
- */
- protected function _setBaseUri($baseUri, $requestUri = null)
- {
- if ($baseUri && is_string($baseUri)) {
- $this->_baseUri = $baseUri;
- return true;
- } elseif ($requestUri && is_string($requestUri)) {
- $scriptFileName = $this->getServer('SCRIPT_FILENAME');
- do {
- if ($scriptFileName && is_string($scriptFileName)) {
- $fileName = basename($scriptFileName, \Qii::getInstance()->appConfigure('ext', '.php'));
- $fileNameLen = strlen($fileName);
- $scriptName = $this->getServer('SCRIPT_NAME');
- if ($scriptName && is_string($scriptName)) {
- $script = basename($scriptName);
- if (strncmp($fileName, $script, $fileNameLen) == 0) {
- $basename = $scriptName;
- break;
- }
- }
- $phpSelfName = $this->getServer('PHP_SELF');
- if ($phpSelfName && is_string($phpSelfName)) {
- $phpSelf = basename($phpSelfName);
- if (strncmp($fileName, $phpSelf, $fileNameLen) == 0) {
- $basename = $phpSelfName;
- break;
- }
- }
- $orig_name = $this->getServer('ORIG_SCRIPT_NAME');
- if ($orig_name && is_string($orig_name)) {
- $orig = basename($orig_name);
- if (strncmp($fileName, $orig, $fileNameLen) == 0) {
- $basename = $orig_name;
- break;
- }
- }
- }
- } while (0);
- if ($basename && strstr($requestUri, $basename) == $requestUri) {
- $this->_baseUri = rtrim($basename, '/');
- return true;
- } elseif ($basename) {
- $dirname = rtrim(dirname($basename), '/');
- if ($dirname) {
- if (strstr($requestUri, $dirname) == $requestUri) {
- $this->_baseUri = $dirname;
- return true;
- }
- }
- }
- $this->_baseUri = '';
- return true;
- }
- return false;
- }
- /**
- * 获取uri的extension
- *
- * @return array|string|string[]
- */
- public function getExtension() {
- $extension = pathinfo($this->uri, PATHINFO_EXTENSION);
- return $extension?? "html";
- }
- /**
- * 对于不存在的方法默认调用url中的方法
- */
- public function __call($method, $args)
- {
- return call_user_func_array(array($this->url, $method), $args);
- }
- }
|