Browse Source

use the trait getView method

rather than a bespoke view creation method.

Add a minimalistic bake-theme and demonstrate bake themes work
AD7six 11 years ago
parent
commit
944c2cb410

+ 42 - 9
src/Shell/Task/TemplateTask.php

@@ -32,7 +32,10 @@ use Cake\View\ViewVarsTrait;
 class TemplateTask extends Shell {
 
 	use ConventionsTrait;
-	use ViewVarsTrait;
+
+	use ViewVarsTrait {
+		getView as _getView;
+	}
 
 /**
  * BakeView instance
@@ -42,16 +45,49 @@ class TemplateTask extends Shell {
 	public $View;
 
 /**
- * Initialize callback. Setup paths for the template task.
+ * Which view class to use for baking
  *
- * @return void
+ * @var string
  */
-	public function initialize() {
-		$this->View = new BakeView(new Request(), new Response());
+	public $viewClass = 'Cake\View\BakeView';
+
+/**
+ * Get view instance
+ *
+ * @param string $viewClass View class name or null to use $viewClass
+ * @return \Cake\View\View
+ * @throws \Cake\View\Exception\MissingViewException If view class was not found.
+ */
+	public function getView() {
+		if ($this->View) {
+			return $this->View;
+		}
+
+		$this->View = $this->_getView();
 		$this->View->theme = isset($this->params['template']) ? $this->params['template'] : '';
 		if ($this->View->theme === 'default') {
 			$this->View->theme = '';
 		}
+
+		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 \Cake\View\View
+ * @throws \Cake\View\Exception\MissingViewException If view class was not found.
+ */
+	public function createView($viewClass = null) {
+		if ($viewClass === null) {
+			$viewClass = $this->viewClass;
+		}
+		$className = App::className($viewClass, 'View');
+		if (!$className) {
+			throw new Exception\MissingViewException([$viewClass]);
+		}
+		return new $className(new Request(), new Response());
 	}
 
 /**
@@ -65,11 +101,8 @@ class TemplateTask extends Shell {
 		if ($vars !== null) {
 			$this->set($vars);
 		}
-		if (empty($this->View)) {
-			$this->initialize();
-		}
 
-		$this->View->set($this->viewVars);
+		$this->getView()->set($this->viewVars);
 
 		try {
 			return $this->View->render($template);

+ 18 - 3
tests/TestCase/Shell/Task/TemplateTaskTest.php

@@ -15,6 +15,7 @@
 namespace Cake\Test\TestCase\Shell\Task;
 
 use Cake\Core\App;
+use Cake\Core\Plugin;
 use Cake\Shell\Task\TemplateTask;
 use Cake\TestSuite\TestCase;
 
@@ -46,6 +47,7 @@ class TemplateTaskTest extends TestCase {
 	public function tearDown() {
 		parent::tearDown();
 		unset($this->Task);
+		Plugin::unload();
 	}
 
 /**
@@ -54,7 +56,6 @@ class TemplateTaskTest extends TestCase {
  * @return void
  */
 	public function testGenerate() {
-		$this->Task->initialize();
 		$this->Task->expects($this->any())->method('in')->will($this->returnValue(1));
 
 		$result = $this->Task->generate('classes/test_object', array('test' => 'foo'));
@@ -63,14 +64,28 @@ class TemplateTaskTest extends TestCase {
 	}
 
 /**
+ * test generate with an overriden template it gets used
+ *
+ * @return void
+ */
+	public function testGenerateWithTemplateOverride() {
+		Plugin::load('TestBakeTheme');
+		$this->Task->params['template'] = 'TestBakeTheme';
+		$this->Task->set(array(
+			'plugin' => 'Special'
+		));
+		$result = $this->Task->generate('config/routes');
+		$this->assertContains('These are my routes. There are many like them but these are my own.', $result);
+	}
+/**
  * test generate with a missing template in the chosen template.
  * ensure fallback to default works.
  *
  * @return void
  */
 	public function testGenerateWithTemplateFallbacks() {
-		$this->Task->initialize();
-		$this->Task->params['template'] = 'test';
+		Plugin::load('TestBakeTheme');
+		$this->Task->params['template'] = 'TestBakeTheme';
 		$this->Task->set(array(
 			'name' => 'Articles',
 			'table' => 'articles',

+ 9 - 0
tests/test_app/Plugin/TestBakeTheme/src/Template/Bake/config/routes.ctp

@@ -0,0 +1,9 @@
+<?php
+/**
+ * These are my routes. There are many like them but these are my own.
+ */
+use Cake\Routing\Router;
+
+Router::plugin('<%= $plugin %>', function($routes) {
+	$routes->fallbacks();
+});