Parcourir la source

Update: URL And Application

Zhu Jinhui il y a 6 ans
Parent
commit
58a1d8cc10
2 fichiers modifiés avec 53 ajouts et 15 suppressions
  1. 28 15
      src/Application.php
  2. 25 0
      src/Request/Url.php

+ 28 - 15
src/Application.php

@@ -1,4 +1,5 @@
 <?php
+
 namespace Qii;
 
 use \Qii\Base\Dispatcher;
@@ -12,7 +13,7 @@ use \Qii\Config\Register;
 use \Qii\Config\Consts;
 use \Qii\Config\Setting;
 
-class Application 
+class Application
 {
     /**
      * 存储网站配置文件内容
@@ -27,7 +28,8 @@ class Application
     /**
      * @var string $workspace 工作目录
      */
-    private static $workspace = './';/**
+    private static $workspace = './';
+    /**
      * @var string $env 环境变量
      */
     public static $env = 'product';
@@ -45,25 +47,25 @@ class Application
      */
     public $dispatcher = null;
 
-	public function __construct()
-	{
+    public function __construct()
+    {
         $this->helper = Psr4::getInstance()->loadClass('\Qii\Autoloader\Helper');
-	}
+    }
 
     /**
      * 初始化本实例对象
      *
      * @return object
      */
-	public static function getInstance()
-	{
+    public static function getInstance()
+    {
         $args = func_get_args();
         if (count($args) > 0) {
             $className = array_shift($args);
             return Psr4::getInstance($className);
         }
-	    return Factory::getInstance('\Qii\Application');
-	}
+        return Factory::getInstance('\Qii\Application');
+    }
 
     /**
      * 设置网站运行环境
@@ -76,6 +78,7 @@ class Application
         self::$env = $env;
         return $this;
     }
+
     /**
      * 设置缓存文件路径
      * @param string $path 缓存路径
@@ -120,6 +123,7 @@ class Application
 
         return $this;
     }
+
     /**
      * 获取指定路径的缓存绝对路径
      * @param string $path 路径
@@ -135,6 +139,7 @@ class Application
         }
         return $dir . DS . $path;
     }
+
     /**
      * 获取网站运行环境
      *
@@ -144,6 +149,7 @@ class Application
     {
         return self::$env;
     }
+
     /**
      * 获取当前工作目录
      */
@@ -151,6 +157,7 @@ class Application
     {
         return self::$workspace;
     }
+
     /**
      * 获取网站的配置文件
      * @return Config\Mix
@@ -208,7 +215,7 @@ class Application
         Register::rewriteConfig($iniFile, $key, $val);
         return $this;
     }
-    
+
     /**
      * 设置指定的前缀是否使用命名空间
      * @param string $prefix 前缀
@@ -275,6 +282,7 @@ class Application
         );
         return $this;
     }
+
     /**
      * 设置缓存
      *
@@ -299,7 +307,8 @@ class Application
      * @param String $cache 缓存的内容
      * @return multitype:multitype:Ambigous <>
      */
-    public function getCachePolicy($cache) {
+    public function getCachePolicy($cache)
+    {
         $data = array();
         if (!$cache) return $data;
         $cacheInfo = Register::getAppConfigure(Register::get(Consts::APP_INI_FILE), $cache);
@@ -312,6 +321,7 @@ class Application
         }
         return $data;
     }
+
     /**
      * 设置view
      *
@@ -319,7 +329,8 @@ class Application
      * @param array $policy
      * @return mixed
      */
-    public function setView($engine = 'smarty', $policy = array()) {
+    public function setView($engine = 'smarty', $policy = array())
+    {
         $viewConfigure = \Qii::appConfigure('view');
         //如果之前实例化过相同的就不再实例化
         if (!$engine) $engine = $viewConfigure['engine'];
@@ -331,6 +342,7 @@ class Application
         $viewEngine->setView($engine, $policy);
         return $viewEngine;
     }
+
     /**
      * 设置数据库使用的文件
      *
@@ -405,6 +417,7 @@ class Application
         $this->request->setActionName($rewrite['action']);
         return $this;
     }
+
     /**
      * sprintf 格式化语言错误信息内容
      *
@@ -422,8 +435,8 @@ class Application
      * @return $this
      * @throws \Exception
      */
-	public function run()
-	{
+    public function run()
+    {
         $this->helper->load(self::$workspace);
         $this->dispatcher = Psr4::getInstance()->loadClass('\Qii\Base\Dispatcher');
         if (!$this->dispatcher instanceof \Qii\Base\Dispatcher) {
@@ -448,5 +461,5 @@ class Application
         $this->dispatcher->dispatch();
         $this->request->setDispatched(true);
         return $this;
-	}
+    }
 }

+ 25 - 0
src/Request/Url.php

@@ -60,6 +60,31 @@ class Url
         return \Qii\Request\Url::getInstance($rewriteRule)->request->getCurrentURL();
     }
 
+    /**
+     * 返回web host
+     * @return mixed
+     */
+    public static function getHost()
+    {
+        $rewriteRule = \Qii::getInstance()->appConfigure(\Qii\Config\Consts::APP_SITE_METHOD);
+        return \Qii\Request\Url::getInstance($rewriteRule)->request->getWebHost();
+    }
+
+    /**
+     * 获取subdomain
+     * @return string
+     */
+    public static function getSubdomain()
+    {
+        $rewriteRule = \Qii::getInstance()->appConfigure(\Qii\Config\Consts::APP_SITE_METHOD);
+        $host = \Qii\Request\Url::getInstance($rewriteRule)->request->getWebHost();
+        $domain = parse_url($host)['host'] ?? "";
+
+        if(stristr($domain,".")) {
+            return explode(".", $domain)[0];
+        }
+        return "";
+    }
     /**
      * 获取网站路径的URL
      * @return string