From 59907332f7841c5774372122338ec33ecc6e4934 Mon Sep 17 00:00:00 2001 From: AD7six Date: Tue, 25 Nov 2014 22:54:23 +0000 Subject: [PATCH] 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 --- src/Shell/Task/ControllerTask.php | 64 +++++----- src/Template/Bake/Controller/actions.ctp | 8 -- src/Template/Bake/Controller/controller.ctp | 4 +- src/Template/Bake/Element/Controller/add.ctp | 3 +- .../Bake/Element/Controller/delete.ctp | 1 + src/Template/Bake/Element/Controller/edit.ctp | 3 +- .../Bake/Element/Controller/index.ctp | 1 + src/Template/Bake/Element/Controller/view.ctp | 1 + .../Shell/Task/ControllerTaskTest.php | 12 +- .../Controller/testBakeActions.php | 118 ++++++++++++++++++ .../Controller/testBakeActionsContent.php | 14 +++ .../Controller/testBakeWithPlugin.php | 102 +++++++++++++++ 12 files changed, 276 insertions(+), 55 deletions(-) delete mode 100644 src/Template/Bake/Controller/actions.ctp create mode 100644 tests/bake_compare/Controller/testBakeActions.php create mode 100644 tests/bake_compare/Controller/testBakeWithPlugin.php diff --git a/src/Shell/Task/ControllerTask.php b/src/Shell/Task/ControllerTask.php index ca2df9bf937..0a7b3b1893e 100644 --- a/src/Shell/Task/ControllerTask.php +++ b/src/Shell/Task/ControllerTask.php @@ -72,37 +72,6 @@ public function all() { } } -/** - * 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 * @@ -112,7 +81,11 @@ public function bakeActions($controllerName) { 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 @@ public function bake($controllerName) { $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; diff --git a/src/Template/Bake/Controller/actions.ctp b/src/Template/Bake/Controller/actions.ctp deleted file mode 100644 index 242917ff822..00000000000 --- a/src/Template/Bake/Controller/actions.ctp +++ /dev/null @@ -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); -%> - diff --git a/src/Template/Bake/Controller/controller.ctp b/src/Template/Bake/Controller/controller.ctp index bc6bc47177f..460b82e8950 100644 --- a/src/Template/Bake/Controller/controller.ctp +++ b/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); +} %> } diff --git a/src/Template/Bake/Element/Controller/add.ctp b/src/Template/Bake/Element/Controller/add.ctp index e54789f3ae8..7a3f6e0b412 100644 --- a/src/Template/Bake/Element/Controller/add.ctp +++ b/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 * diff --git a/src/Template/Bake/Element/Controller/delete.ctp b/src/Template/Bake/Element/Controller/delete.ctp index bc210151d78..cd97dd9d971 100644 --- a/src/Template/Bake/Element/Controller/delete.ctp +++ b/src/Template/Bake/Element/Controller/delete.ctp @@ -13,6 +13,7 @@ * @license http://www.opensource.org/licenses/mit-license.php MIT License */ %> + /** * Delete method * diff --git a/src/Template/Bake/Element/Controller/edit.ctp b/src/Template/Bake/Element/Controller/edit.ctp index 3c47ca3652e..28bf40500a0 100644 --- a/src/Template/Bake/Element/Controller/edit.ctp +++ b/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 * diff --git a/src/Template/Bake/Element/Controller/index.ctp b/src/Template/Bake/Element/Controller/index.ctp index 4beb78eb415..7dcf2b64073 100644 --- a/src/Template/Bake/Element/Controller/index.ctp +++ b/src/Template/Bake/Element/Controller/index.ctp @@ -13,6 +13,7 @@ * @license http://www.opensource.org/licenses/mit-license.php MIT License */ %> + /** * Index method * diff --git a/src/Template/Bake/Element/Controller/view.ctp b/src/Template/Bake/Element/Controller/view.ctp index 5e58ecd5e7f..448a40987fb 100644 --- a/src/Template/Bake/Element/Controller/view.ctp +++ b/src/Template/Bake/Element/Controller/view.ctp @@ -19,6 +19,7 @@ $allAssociations = array_merge( $this->Bake->aliasExtractor($modelObj, 'HasMany') ); %> + /** * View method * diff --git a/tests/TestCase/Shell/Task/ControllerTaskTest.php b/tests/TestCase/Shell/Task/ControllerTaskTest.php index d5326519d79..5a91b7c089a 100644 --- a/tests/TestCase/Shell/Task/ControllerTaskTest.php +++ b/tests/TestCase/Shell/Task/ControllerTaskTest.php @@ -176,12 +176,7 @@ public function testBakeActions() { $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 @@ public function testBakeWithPlugin() { )->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 @@ public function testBakeWithPlugin() { * @return void */ public function testBakeActionsContent() { - $result = $this->Task->bakeActions('BakeArticles'); + $result = $this->Task->bake('BakeArticles'); $this->assertSameAsFile(__FUNCTION__ . '.php', $result); } diff --git a/tests/bake_compare/Controller/testBakeActions.php b/tests/bake_compare/Controller/testBakeActions.php new file mode 100644 index 00000000000..4aa08237449 --- /dev/null +++ b/tests/bake_compare/Controller/testBakeActions.php @@ -0,0 +1,118 @@ +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']); + } + +} diff --git a/tests/bake_compare/Controller/testBakeActionsContent.php b/tests/bake_compare/Controller/testBakeActionsContent.php index eb5eaec0467..c27c3763d25 100644 --- a/tests/bake_compare/Controller/testBakeActionsContent.php +++ b/tests/bake_compare/Controller/testBakeActionsContent.php @@ -1,3 +1,15 @@ +redirect(['action' => 'index']); } + +} diff --git a/tests/bake_compare/Controller/testBakeWithPlugin.php b/tests/bake_compare/Controller/testBakeWithPlugin.php new file mode 100644 index 00000000000..c857ae232b1 --- /dev/null +++ b/tests/bake_compare/Controller/testBakeWithPlugin.php @@ -0,0 +1,102 @@ +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']); + } + +}