Browse Source

modify controller task to render in one step

Instead of baking actions seperately, and passing actions as a string to
the controller template - pass the array of action names directly to the
controller template
AD7six 11 years ago
parent
commit
59907332f7

+ 29 - 35
src/Shell/Task/ControllerTask.php

@@ -73,37 +73,6 @@ class ControllerTask extends BakeTask {
 	}
 
 /**
- * Bake scaffold actions
- *
- * @param string $controllerName Controller name
- * @return string Baked actions
- */
-	public function bakeActions($controllerName) {
-		if (!empty($this->params['no-actions'])) {
-			return '';
-		}
-		$currentModelName = $controllerName;
-		$plugin = $this->plugin;
-		if ($plugin) {
-			$plugin .= '.';
-		}
-
-		$modelObj = TableRegistry::get($currentModelName);
-
-		$pluralName = $this->_variableName($currentModelName);
-		$singularName = $this->_singularName($currentModelName);
-		$singularHumanName = $this->_singularHumanName($controllerName);
-		$pluralHumanName = $this->_variableName($controllerName);
-
-		$this->Template->set(compact(
-			'plugin', 'admin', 'pluralName', 'singularName',
-			'singularHumanName', 'pluralHumanName', 'modelObj', 'currentModelName'
-		));
-		$actions = $this->Template->generate('Controller/actions');
-		return $actions;
-	}
-
-/**
  * Assembles and writes a Controller file
  *
  * @param string $controllerName Controller name already pluralized and correctly cased.
@@ -112,7 +81,11 @@ class ControllerTask extends BakeTask {
 	public function bake($controllerName) {
 		$this->out("\n" . sprintf('Baking controller class for %s...', $controllerName), 1, Shell::QUIET);
 
-		$actions = $this->bakeActions($controllerName);
+		$actions = [];
+		if (empty($this->params['no-actions'])) {
+			$actions = ['index', 'view', 'add', 'edit', 'delete'];
+		}
+
 		$helpers = $this->getHelpers();
 		$components = $this->getComponents();
 
@@ -126,12 +99,33 @@ class ControllerTask extends BakeTask {
 			$namespace = str_replace('/', '\\', $this->plugin);
 		}
 
+		$currentModelName = $controllerName;
+		$plugin = $this->plugin;
+		if ($plugin) {
+			$plugin .= '.';
+		}
+
+		$modelObj = TableRegistry::get($currentModelName);
+
+		$pluralName = $this->_variableName($currentModelName);
+		$singularName = $this->_singularName($currentModelName);
+		$singularHumanName = $this->_singularHumanName($controllerName);
+		$pluralHumanName = $this->_variableName($controllerName);
+
 		$data = compact(
-			'prefix',
 			'actions',
-			'helpers',
+			'admin',
 			'components',
-			'namespace'
+			'currentModelName',
+			'helpers',
+			'modelObj',
+			'namespace',
+			'plugin',
+			'pluralHumanName',
+			'pluralName',
+			'prefix',
+			'singularHumanName',
+			'singularName'
 		);
 		$data['name'] = $controllerName;
 

+ 0 - 8
src/Template/Bake/Controller/actions.ctp

@@ -1,8 +0,0 @@
-<%
-$actions = ['index', 'view', 'add', 'edit', 'delete'];
-foreach($actions as $action) {
-	$out[] = trim($this->element('Controller/' . $action));
-}
-echo implode("\n\n", $out);
-%>
-

+ 3 - 1
src/Template/Bake/Controller/controller.ctp

@@ -37,7 +37,9 @@ class <%= $name %>Controller extends AppController {
 <%
 echo $this->Bake->arrayProperty('helpers', $helpers, ['indent' => false]);
 echo $this->Bake->arrayProperty('components', $components, ['indent' => false]);
-echo $actions;
+foreach($actions as $action) {
+	echo $this->element('Controller/' . $action);
+}
 %>
 
 }

+ 2 - 1
src/Template/Bake/Element/Controller/add.ctp

@@ -12,8 +12,9 @@
  * @since         3.0.0
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
+$compact = ["'" . $singularName . "'"];
 %>
-<% $compact = ["'" . $singularName . "'"]; %>
+
 /**
  * Add method
  *

+ 1 - 0
src/Template/Bake/Element/Controller/delete.ctp

@@ -13,6 +13,7 @@
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
 %>
+
 /**
  * Delete method
  *

+ 2 - 1
src/Template/Bake/Element/Controller/edit.ctp

@@ -15,8 +15,9 @@
 
 $belongsTo = $this->Bake->aliasExtractor($modelObj, 'BelongsTo');
 $belongsToMany = $this->Bake->aliasExtractor($modelObj, 'BelongsToMany');
+$compact = ["'" . $singularName . "'"];
 %>
-<% $compact = ["'" . $singularName . "'"]; %>
+
 /**
  * Edit method
  *

+ 1 - 0
src/Template/Bake/Element/Controller/index.ctp

@@ -13,6 +13,7 @@
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
 %>
+
 /**
  * Index method
  *

+ 1 - 0
src/Template/Bake/Element/Controller/view.ctp

@@ -19,6 +19,7 @@ $allAssociations = array_merge(
 	$this->Bake->aliasExtractor($modelObj, 'HasMany')
 );
 %>
+
 /**
  * View method
  *

+ 3 - 9
tests/TestCase/Shell/Task/ControllerTaskTest.php

@@ -176,12 +176,7 @@ class ControllerTaskTest extends TestCase {
 				$this->stringContains('class BakeArticlesController')
 			);
 		$result = $this->Task->bake('BakeArticles');
-
-		$this->assertTextContains('public function add(', $result);
-		$this->assertTextContains('public function index(', $result);
-		$this->assertTextContains('public function view(', $result);
-		$this->assertTextContains('public function edit(', $result);
-		$this->assertTextContains('public function delete(', $result);
+		$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
 	}
 
 /**
@@ -225,8 +220,7 @@ class ControllerTaskTest extends TestCase {
 			)->will($this->returnValue(true));
 
 		$result = $this->Task->bake('BakeArticles');
-		$this->assertContains('namespace ControllerTest\Controller;', $result);
-		$this->assertContains('use ControllerTest\Controller\AppController;', $result);
+		$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
 		Plugin::unload();
 	}
 
@@ -237,7 +231,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeActionsContent() {
-		$result = $this->Task->bakeActions('BakeArticles');
+		$result = $this->Task->bake('BakeArticles');
 		$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
 	}
 

+ 118 - 0
tests/bake_compare/Controller/testBakeActions.php

@@ -0,0 +1,118 @@
+<?php
+namespace App\Controller;
+
+use App\Controller\AppController;
+
+/**
+ * BakeArticles Controller
+ *
+ * @property App\Model\Table\BakeArticlesTable $BakeArticles
+ * @property CsrfComponent $Csrf
+ * @property AuthComponent $Auth
+ */
+class BakeArticlesController extends AppController {
+
+/**
+ * Helpers
+ *
+ * @var array
+ */
+	public $helpers = ['Html', 'Time'];
+
+/**
+ * Components
+ *
+ * @var array
+ */
+	public $components = ['Csrf', 'Auth'];
+
+/**
+ * Index method
+ *
+ * @return void
+ */
+	public function index() {
+		$this->paginate = [
+			'contain' => ['BakeUsers']
+		];
+		$this->set('bakeArticles', $this->paginate($this->BakeArticles));
+	}
+
+/**
+ * View method
+ *
+ * @param string|null $id
+ * @return void
+ * @throws \Cake\Network\Exception\NotFoundException
+ */
+	public function view($id = null) {
+		$bakeArticle = $this->BakeArticles->get($id, [
+			'contain' => ['BakeUsers', 'BakeTags', 'BakeComments']
+		]);
+		$this->set('bakeArticle', $bakeArticle);
+	}
+
+/**
+ * Add method
+ *
+ * @return void
+ */
+	public function add() {
+		$bakeArticle = $this->BakeArticles->newEntity($this->request->data);
+		if ($this->request->is('post')) {
+			if ($this->BakeArticles->save($bakeArticle)) {
+				$this->Flash->success('The bake article has been saved.');
+				return $this->redirect(['action' => 'index']);
+			} else {
+				$this->Flash->error('The bake article could not be saved. Please, try again.');
+			}
+		}
+		$bakeUsers = $this->BakeArticles->BakeUsers->find('list');
+		$bakeTags = $this->BakeArticles->BakeTags->find('list');
+		$this->set(compact('bakeArticle', 'bakeUsers', 'bakeTags'));
+	}
+
+/**
+ * Edit method
+ *
+ * @param string|null $id
+ * @return void
+ * @throws \Cake\Network\Exception\NotFoundException
+ */
+	public function edit($id = null) {
+		$bakeArticle = $this->BakeArticles->get($id, [
+			'contain' => ['BakeTags']
+		]);
+		if ($this->request->is(['patch', 'post', 'put'])) {
+			$bakeArticle = $this->BakeArticles->patchEntity($bakeArticle, $this->request->data);
+			if ($this->BakeArticles->save($bakeArticle)) {
+				$this->Flash->success('The bake article has been saved.');
+				return $this->redirect(['action' => 'index']);
+			} else {
+				$this->Flash->error('The bake article could not be saved. Please, try again.');
+			}
+		}
+		$bakeUsers = $this->BakeArticles->BakeUsers->find('list');
+		$bakeTags = $this->BakeArticles->BakeTags->find('list');
+		$this->set(compact('bakeArticle', 'bakeUsers', 'bakeTags'));
+	}
+
+/**
+ * Delete method
+ *
+ * @param string|null $id
+ * @return void
+ * @throws \Cake\Network\Exception\NotFoundException
+ */
+	public function delete($id = null) {
+		$bakeArticle = $this->BakeArticles->get($id);
+		$this->request->allowMethod(['post', 'delete']);
+		if ($this->BakeArticles->delete($bakeArticle)) {
+			$this->Flash->success('The bake article has been deleted.');
+		} else {
+			$this->Flash->error('The bake article could not be deleted. Please, try again.');
+		}
+		return $this->redirect(['action' => 'index']);
+	}
+
+}

+ 14 - 0
tests/bake_compare/Controller/testBakeActionsContent.php

@@ -1,3 +1,15 @@
+<?php
+namespace App\Controller;
+
+use App\Controller\AppController;
+
+/**
+ * BakeArticles Controller
+ *
+ * @property App\Model\Table\BakeArticlesTable $BakeArticles
+ */
+class BakeArticlesController extends AppController {
+
 /**
  * Index method
  *
@@ -86,3 +98,5 @@
 		}
 		return $this->redirect(['action' => 'index']);
 	}
+
+}

+ 102 - 0
tests/bake_compare/Controller/testBakeWithPlugin.php

@@ -0,0 +1,102 @@
+<?php
+namespace ControllerTest\Controller;
+
+use ControllerTest\Controller\AppController;
+
+/**
+ * BakeArticles Controller
+ *
+ * @property ControllerTest\Model\Table\BakeArticlesTable $BakeArticles
+ */
+class BakeArticlesController extends AppController {
+
+/**
+ * Index method
+ *
+ * @return void
+ */
+	public function index() {
+		$this->paginate = [
+			'contain' => ['BakeUsers']
+		];
+		$this->set('bakeArticles', $this->paginate($this->BakeArticles));
+	}
+
+/**
+ * View method
+ *
+ * @param string|null $id
+ * @return void
+ * @throws \Cake\Network\Exception\NotFoundException
+ */
+	public function view($id = null) {
+		$bakeArticle = $this->BakeArticles->get($id, [
+			'contain' => ['BakeUsers', 'BakeTags', 'BakeComments']
+		]);
+		$this->set('bakeArticle', $bakeArticle);
+	}
+
+/**
+ * Add method
+ *
+ * @return void
+ */
+	public function add() {
+		$bakeArticle = $this->BakeArticles->newEntity($this->request->data);
+		if ($this->request->is('post')) {
+			if ($this->BakeArticles->save($bakeArticle)) {
+				$this->Flash->success('The bake article has been saved.');
+				return $this->redirect(['action' => 'index']);
+			} else {
+				$this->Flash->error('The bake article could not be saved. Please, try again.');
+			}
+		}
+		$bakeUsers = $this->BakeArticles->BakeUsers->find('list');
+		$bakeTags = $this->BakeArticles->BakeTags->find('list');
+		$this->set(compact('bakeArticle', 'bakeUsers', 'bakeTags'));
+	}
+
+/**
+ * Edit method
+ *
+ * @param string|null $id
+ * @return void
+ * @throws \Cake\Network\Exception\NotFoundException
+ */
+	public function edit($id = null) {
+		$bakeArticle = $this->BakeArticles->get($id, [
+			'contain' => ['BakeTags']
+		]);
+		if ($this->request->is(['patch', 'post', 'put'])) {
+			$bakeArticle = $this->BakeArticles->patchEntity($bakeArticle, $this->request->data);
+			if ($this->BakeArticles->save($bakeArticle)) {
+				$this->Flash->success('The bake article has been saved.');
+				return $this->redirect(['action' => 'index']);
+			} else {
+				$this->Flash->error('The bake article could not be saved. Please, try again.');
+			}
+		}
+		$bakeUsers = $this->BakeArticles->BakeUsers->find('list');
+		$bakeTags = $this->BakeArticles->BakeTags->find('list');
+		$this->set(compact('bakeArticle', 'bakeUsers', 'bakeTags'));
+	}
+
+/**
+ * Delete method
+ *
+ * @param string|null $id
+ * @return void
+ * @throws \Cake\Network\Exception\NotFoundException
+ */
+	public function delete($id = null) {
+		$bakeArticle = $this->BakeArticles->get($id);
+		$this->request->allowMethod(['post', 'delete']);
+		if ($this->BakeArticles->delete($bakeArticle)) {
+			$this->Flash->success('The bake article has been deleted.');
+		} else {
+			$this->Flash->error('The bake article could not be deleted. Please, try again.');
+		}
+		return $this->redirect(['action' => 'index']);
+	}
+
+}