Jelajahi Sumber

set render to response

Zhu Jinhui 7 tahun lalu
induk
melakukan
25f18301d6
2 mengubah file dengan 88 tambahan dan 75 penghapusan
  1. 29 26
      src/Base/Controller.php
  2. 59 49
      src/Base/Response.php

+ 29 - 26
src/Base/Controller.php

@@ -2,6 +2,7 @@
 /**
  * 控制器基类
  */
+
 namespace Qii\Base;
 
 use \Qii\Autoloader\Psr4;
@@ -64,13 +65,13 @@ abstract class Controller
      * @var bool
      */
     public $enableView = false;
-
+    
     /**
      * 是否启用Model
      * @var bool
      */
     public $enableDB = false;
-
+    
     public function __construct()
     {
         $this->load = Psr4::getInstance()->loadClass('\Qii\Autoloader\Loader');
@@ -88,19 +89,19 @@ abstract class Controller
         if ($this->enableView) {
             $this->view = $this->setView();
         }
-
+        
         if (!$this->beforeRun()) {
             exit();
         }
     }
-
+    
     /**
      * 启用view后调用初始化View方法
      */
     protected function initView()
     {
     }
-
+    
     /**
      * 设置view
      *
@@ -120,14 +121,13 @@ abstract class Controller
         $viewEngine = Psr4::getInstance()->loadClass('\Qii\View\Loader');
         $viewEngine->setView($engine, $policy);
         $this->view = $viewEngine->getView();
-        if(method_exists($this, 'initView'))
-        {
+        if (method_exists($this, 'initView')) {
             $this->initView();
         }
         $this->response->setRender($this->view);
-        return $this->view ;
+        return $this->view;
     }
-
+    
     /**
      * 设置缓存
      *
@@ -146,7 +146,7 @@ abstract class Controller
         $loader = new \Qii\Cache\Loader($engine);
         return $this->cache->$engine = $loader->initialization($policy);
     }
-
+    
     /**
      * 获取缓存的策略
      * @param String $cache 缓存的内容
@@ -158,7 +158,7 @@ abstract class Controller
         if (!$cache) return $data;
         $cacheInfo = Register::getAppConfigure(Register::get(Consts::APP_INI_FILE), $cache);
         if (!$cacheInfo) return $data;
-
+        
         $servers = explode(";", $cacheInfo['servers']);
         $ports = explode(";", $cacheInfo['ports']);
         for ($i = 0; $i < count($servers); $i++) {
@@ -166,6 +166,7 @@ abstract class Controller
         }
         return $data;
     }
+    
     /**
      * 开启数据库操作
      */
@@ -173,7 +174,7 @@ abstract class Controller
     {
         return $this->db = Psr4::getInstance()->loadClass('\Qii\Driver\Model');
     }
-
+    
     /**
      * 获取view
      *
@@ -183,7 +184,7 @@ abstract class Controller
     {
         return $this->view;
     }
-
+    
     /**
      * 设置 response
      * @param $request
@@ -192,6 +193,7 @@ abstract class Controller
     {
         return $this->response = $response;
     }
+    
     /**
      * 设置request
      * @param $request
@@ -200,8 +202,8 @@ abstract class Controller
     {
         return $this->request = $request;
     }
-
-
+    
+    
     /**
      * 只要继承的方法调用parent::__construct()就开始执行
      * 此方法如果返回false,将不再往下继续执行
@@ -210,22 +212,23 @@ abstract class Controller
     {
         return true;
     }
-
+    
     /**
      * 执行完dispatch后调用
      */
     protected function afterRun()
     {
-        if(!$this->response || !is_object($this->response))
-        {
+        if (!$this->response || !is_object($this->response)) {
             return;
         }
-        if($this->response instanceof \Qii\Base\Response)
-        {
+        if ($this->response instanceof \Qii\Base\Response) {
+            if ($this->response->needRender() && $this->view && $this->view instanceof \Qii\View\Intf) {
+                $this->response->setRender($this->view);
+            }
             $this->response->response();
         }
     }
-
+    
     /**
      * 转发
      * @param String $controller
@@ -239,7 +242,7 @@ abstract class Controller
         \Qii::getInstance()->dispatcher->setRequest($this->request);
         return call_user_func_array(array(\Qii::getInstance()->dispatcher, 'dispatch'), func_get_args());
     }
-
+    
     /**
      * 获取当前使用的controller
      *
@@ -249,7 +252,7 @@ abstract class Controller
     {
         return get_called_class();
     }
-
+    
     /**
      * 获取 request 方法
      * @return Qii_Request_Http
@@ -258,7 +261,7 @@ abstract class Controller
     {
         return $this->request;
     }
-
+    
     /**
      * 获取response类
      * @return mixed
@@ -267,7 +270,7 @@ abstract class Controller
     {
         return $this->response;
     }
-
+    
     /**
      * 设置forward
      * @param string $controller controller名
@@ -279,7 +282,7 @@ abstract class Controller
         $this->request->setActionName($action);
         $this->request->setForward(true);
     }
-
+    
     /**
      * afterRun 和 forward 执行
      */

+ 59 - 49
src/Base/Response.php

@@ -1,4 +1,5 @@
 <?php
+
 namespace Qii\Base;
 
 class Response
@@ -9,9 +10,9 @@ class Response
     const DEFAULT_BODY = 'html';
     const FORMAT_JSON = 'json';
     const FORMAT_HTML = 'html';
-
+    
     public static $render = null;
-
+    
     /**
      * Body content
      * @var array
@@ -19,7 +20,7 @@ class Response
     protected $body = array();
     
     /**
-     * data 
+     * data
      * @param array $data
      */
     protected $data = array();
@@ -28,19 +29,20 @@ class Response
      * @var array
      */
     protected $headers = array();
-
+    
     /**
      * Determine to send the headers or not
      * @var unknown_type
      */
     protected $_sendHeader = false;
-	
     
-	public function __construct($data = array())
-	{
+    
+    public function __construct($data = array())
+    {
         $this->format = isset($data['format']) ? isset($data['format']) : self::FORMAT_HTML;
-		$this->data = $data;
-	}
+        $this->data = $data;
+    }
+    
     /**
      * 设置页面渲染类
      * @param \Qii\View\Intf $render 渲染类
@@ -49,7 +51,7 @@ class Response
     {
         \Qii\Base\Response::$render = $render;
     }
-
+    
     /**
      * Append content to the body content
      *
@@ -65,10 +67,10 @@ class Response
         if (!isset($this->body[$key])) {
             $this->body[$key] = '';
         }
-        $this->body[$key] .= (string) $body;
+        $this->body[$key] .= (string)$body;
         return $this;
     }
-
+    
     /**
      * Clear the entire body
      *
@@ -86,7 +88,7 @@ class Response
         }
         return true;
     }
-
+    
     /**
      * Clear headers
      *
@@ -97,7 +99,7 @@ class Response
         $this->headers = array();
         return $this;
     }
-
+    
     /**
      * Return the body content
      *
@@ -111,7 +113,7 @@ class Response
         }
         return array_key_exists($key, $this->body) ? $this->body[$key] : null;
     }
-
+    
     /**
      * Return array of headers; see {@link $headers} for format
      *
@@ -121,7 +123,7 @@ class Response
     {
         return $this->headers;
     }
-
+    
     /**
      * Prepend content the body
      *
@@ -140,7 +142,18 @@ class Response
         $this->body[$key] = $body . $this->body[$key];
         return $this;
     }
-
+    /**
+     * 是否需要view来渲染页面  
+     */
+    public function needRender()
+    {
+        if(is_array($this->data['body']) && isset($this->data['body']['tpl']))
+        {
+            return true;
+        }
+        return false;
+    }
+    
     /**
      * Send the response, including all headers
      *
@@ -148,33 +161,29 @@ class Response
      */
     public function response()
     {
-        if($this->data && isset($this->data['body']))
-        {
-            switch($this->data['format'])
-            {
+        if ($this->data && isset($this->data['body'])) {
+            switch ($this->data['format']) {
                 case self::FORMAT_JSON:
                     $this->setHeader('Content-Type', 'text/json');
                     $this->sendHeaders();
                     echo json_encode($this->data['body'], JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
-                break;
+                    break;
                 default:
-					$body = $this->data['body'];
-					if(is_array($this->data['body'])) {
-						$body = '';
-						if(isset($this->data['body']['render']) && $this->data['body']['render'] instanceof  \Qii\View\Intf)
-						{
+                    $body = $this->data['body'];
+                    if (is_array($this->data['body'])) {
+                        $body = '';
+                        if (isset($this->data['body']['render']) && $this->data['body']['render'] instanceof \Qii\View\Intf) {
                             \Qii\Base\Response::$render = $this->data['body']['render'];
-						}
-
-                        if(\Qii\Base\Response::$render != null && \Qii\Base\Response::$render instanceof  \Qii\View\Intf)
-                        {
+                        }
+                        
+                        if (\Qii\Base\Response::$render != null && \Qii\Base\Response::$render instanceof \Qii\View\Intf) {
                             $tplData = isset($this->data['body']['tplData']) ? $this->data['body']['tplData'] : [];
                             \Qii\Base\Response::$render->assign($tplData);
                             $body = \Qii\Base\Response::$render->fetch($this->data['body']['tpl']);
                         }
-					}
-                    echo (IS_CLI ? (new \Qii\Response\Cli())->stdout($body) : $body);
-                break;
+                    }
+                    echo(IS_CLI ? (new \Qii\Response\Cli())->stdout($body) : $body);
+                    break;
             }
             return;
         }
@@ -185,11 +194,12 @@ class Response
             echo IS_CLI ? new \Qii\Response\Cli($body) : $body;
         }
     }
-
+    
     public function setAllHeaders()
     {
         return false;
     }
+    
     /**
      * Set body content
      *
@@ -202,10 +212,10 @@ class Response
         if (!strlen($key)) {
             $key = self::DEFAULT_BODY;
         }
-        $this->body[$key] = (string) $body;
+        $this->body[$key] = (string)$body;
         return $this;
     }
-
+    
     /**
      * Set a header
      *
@@ -219,9 +229,9 @@ class Response
      */
     public function setHeader($name, $value, $replace = false)
     {
-        $name  = $this->_normalizeHeader($name);
-        $value = (string) $value;
-
+        $name = $this->_normalizeHeader($name);
+        $value = (string)$value;
+        
         if ($replace) {
             foreach ($this->headers as $key => $header) {
                 if ($name == $header['name']) {
@@ -229,16 +239,16 @@ class Response
                 }
             }
         }
-
+        
         $this->headers[] = array(
-            'name'    => $name,
-            'value'   => $value,
+            'name' => $name,
+            'value' => $value,
             'replace' => $replace
         );
-
+        
         return $this;
     }
-
+    
     /**
      * Set redirect URL
      *
@@ -252,7 +262,7 @@ class Response
         $this->setHeader('Location', $url, true);
         return $this;
     }
-
+    
     /**
      * Magic __toString functionality
      *
@@ -267,7 +277,7 @@ class Response
         $this->response();
         return ob_get_clean();
     }
-
+    
     /**
      * Normalize a header name
      *
@@ -278,12 +288,12 @@ class Response
      */
     protected function _normalizeHeader($name)
     {
-        $filtered = str_replace(array('-', '_'), ' ', (string) $name);
+        $filtered = str_replace(array('-', '_'), ' ', (string)$name);
         $filtered = ucwords(strtolower($filtered));
         $filtered = str_replace(' ', '-', $filtered);
         return $filtered;
     }
-
+    
     /**
      * Send all headers
      *