//
protected $bind = [];//
/**
*
* @access public
* @param array $options
*/
public function __construct($options = [])
{//
foreach ($options as $name => $item) {//
if (property_exists($this, $name)) {//
$this->$name = $item;//
}
}
if (is_null($this->filter)) {//
$this->filter = Config::get('default_filter');//
}
}
public function __call($method, $args)//
{
if (array_key_exists($method, self::$hook)) {//
array_unshift($args, $this);//
return call_user_func_array(self::$hook[$method], $args);//
} else {//
throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
}
}
/**
* Hook
* @access public
* @param string|array $method
* @param mixed $callback callable
* @return void
*/
public static function hook($method, $callback = null)
{// Hook
if (is_array($method)) {//
self::$hook = array_merge(self::$hook, $method);// hook , ,
} else {
self::$hook[$method] = $callback;// key value
}
}
/**
*
* @access public
* @param array $options
* @return \think\Request
*/
public static function instance($options = [])
{//
if (is_null(self::$instance)) {
self::$instance = new static($options);
}
return self::$instance;
}
/**
* URL
* @access public
* @param string $uri URL
* @param string $method
* @param array $params
* @param array $cookie
* @param array $files
* @param array $server
* @param string $content
* @return \think\Request
*/
public static function create($uri, $method = 'GET', $params = [], $cookie = [], $files = [], $server = [], $content = null)
{// URL URL method cookie files server content
$server['PATH_INFO'] = '';// server SERVER
$server['REQUEST_METHOD'] = strtoupper($method);//
$info = parse_url($uri);// url
if (isset($info['host'])) {// host
$server['SERVER_NAME'] = $info['host'];//
$server['HTTP_HOST'] = $info['host'];// ip
}
if (isset($info['scheme'])) {//
if ('https' === $info['scheme']) {
$server['HTTPS'] = 'on';//
$server['SERVER_PORT'] = 443;// 443
} else {
unset($server['HTTPS']);//
$server['SERVER_PORT'] = 80;// 80
}
}
if (isset($info['port'])) {// 80
$server['SERVER_PORT'] = $info['port'];//
$server['HTTP_HOST'] = $server['HTTP_HOST'] . ':' . $info['port'];// IP , IP+
}
if (isset($info['user'])) {//
$server['PHP_AUTH_USER'] = $info['user'];
}
if (isset($info['pass'])) {//
$server['PHP_AUTH_PW'] = $info['pass'];
}
if (!isset($info['path'])) {//
$info['path'] = '/';
}
$options = [];//
$queryString = '';//
if (isset($info['query'])) {//
parse_str(html_entity_decode($info['query']), $query);//
if (!empty($params)) {//
$params = array_replace($query, $params);//
$queryString = http_build_query($query, '', '&');// queryString
} else {//
$params = $query;
$queryString = $info['query'];//
}
} elseif (!empty($params)) {
$queryString = http_build_query($params, '', '&');//
}
$server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');// ,
$server['QUERY_STRING'] = $queryString;//
$options['cookie'] = $cookie;// cookie
$options['param'] = $params;//
$options['file'] = $files;//
$options['server'] = $server;//
$options['url'] = $server['REQUEST_URI']; //
$options['baseUrl'] = $info['path'];//
$options['pathinfo'] = '/' == $info['path'] ? '/' : ltrim($info['path'], '/');//
$options['method'] = $server['REQUEST_METHOD'];//
$options['domain'] = $info['scheme'] . '://' . $server['HTTP_HOST'];//
$options['content'] = $content;//
self::$instance = new self($options);//
return self::$instance;//
}
/**
*
* @access public
* @param string $domain
* @return string
*/
public function domain($domain = null)
{//
if (!is_null($domain)) {//
$this->domain = $domain;
return $this;
} elseif (!$this->domain) {
$this->domain = $this->scheme() . '://' . $this->host();
}
return $this->domain;
}// + ,
/**
* URL QUERY_STRING
* @access public
* @param string|true $url URL true
* @return string
*/
public function url($url = null)//
{
if (!is_null($url) && true !== $url) {
$this->url = $url;
return $this;
} elseif (!$this->url) {//
if (IS_CLI) {//
$this->url = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
} elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {//
$this->url = $_SERVER['HTTP_X_REWRITE_URL'];
} elseif (isset($_SERVER['REQUEST_URI'])) {//
$this->url = $_SERVER['REQUEST_URI'];
} elseif (isset($_SERVER['ORIG_PATH_INFO'])) {// pathinfo
$this->url = $_SERVER['ORIG_PATH_INFO'] . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
} else {
$this->url = '';// ,
}
}
return true === $url ? $this->domain() . $this->url : $this->url;// ,
}