ソースを参照

Added accessor method getView() to ViewVarsTrait.

ADmad 11 年 前
コミット
68b6628fed

+ 3 - 5
src/Controller/Controller.php

@@ -179,7 +179,7 @@ class Controller implements EventListener {
  *
  * @var string
  */
-	public $viewClass = 'Cake\View\View';
+	public $viewClass = null;
 
 /**
  * The path to this controllers view templates.
@@ -388,7 +388,7 @@ class Controller implements EventListener {
 			$this->autoRender = false;
 		}
 		if (!empty($request->params['bare'])) {
-			$this->autoLayout = false;
+			$this->getView()->autoLayout = false;
 		}
 	}
 
@@ -587,10 +587,8 @@ class Controller implements EventListener {
 			return $this->response;
 		}
 
-		$this->View = $this->createView();
-
 		$this->autoRender = false;
-		$this->response->body($this->View->render($view, $layout));
+		$this->response->body($this->getView()->render($view, $layout));
 		return $this->response;
 	}
 

+ 3 - 2
src/View/Cell.php

@@ -78,7 +78,7 @@ abstract class Cell {
  *
  * @var string
  */
-	public $viewClass = 'Cake\View\View';
+	public $viewClass = null;
 
 /**
  * The theme name that will be used to render.
@@ -153,7 +153,8 @@ abstract class Cell {
 			$template = $this->template;
 		}
 
-		$this->View = $this->createView();
+		$this->View = null;
+		$this->getView();
 
 		$this->View->layout = false;
 		$className = explode('\\', get_class($this));

+ 28 - 1
src/View/ViewVarsTrait.php

@@ -32,10 +32,37 @@ trait ViewVarsTrait {
 	public $viewVars = [];
 
 /**
+ * Get view instance
+ *
+ * @param string $viewClass View class name or null to use $viewClass
+ * @return \Cake\View\View
+ */
+	public function getView($viewClass = null) {
+		if ($viewClass === null && $this->View) {
+			return $this->View;
+		}
+
+		if ($viewClass !== null && $viewClass !== $this->viewClass) {
+			$this->viewClass = $viewClass;
+			unset($this->View);
+		}
+		if ($this->viewClass === null) {
+			$this->viewClass = App::className('App', 'View', 'View');
+			if ($this->viewClass === false) {
+				$this->viewClass = 'Cake\View\View';
+			}
+		}
+		if (empty($this->View)) {
+			$this->View = $this->createView();
+		}
+		return $this->View;
+	}
+
+/**
  * Constructs the view class instance based on object properties.
  *
  * @param string $viewClass Optional namespaced class name of the View class to instantiate.
- * @return View
+ * @return Cake\View\View
  */
 	public function createView($viewClass = null) {
 		if ($viewClass === null) {

+ 2 - 0
tests/TestCase/Controller/ControllerTest.php

@@ -362,9 +362,11 @@ class ControllerTest extends TestCase {
 		$this->assertRegExp('/posts index/', (string)$result);
 
 		$Controller->view = 'index';
+		$Controller->getView()->hasRendered = false;
 		$result = $Controller->render();
 		$this->assertRegExp('/posts index/', (string)$result);
 
+		$Controller->getView()->hasRendered = false;
 		$result = $Controller->render('/Element/test_element');
 		$this->assertRegExp('/this is the test element/', (string)$result);
 		$Controller->view = null;

+ 23 - 0
tests/test_app/TestApp/View/AppView.php

@@ -0,0 +1,23 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestApp\View;
+
+use Cake\View\View;
+
+/**
+ * App View class
+ *
+ */
+class AppView extends View {
+}