Skip to content

Commit

Permalink
modify controller task to render in one step
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AD7six committed Nov 25, 2014
1 parent 93a5d91 commit 5990733
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 55 deletions.
64 changes: 29 additions & 35 deletions src/Shell/Task/ControllerTask.php
Expand Up @@ -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
*
Expand All @@ -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();

Expand All @@ -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;

Expand Down
8 changes: 0 additions & 8 deletions src/Template/Bake/Controller/actions.ctp

This file was deleted.

4 changes: 3 additions & 1 deletion src/Template/Bake/Controller/controller.ctp
Expand Up @@ -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);
}
%>

}
3 changes: 2 additions & 1 deletion src/Template/Bake/Element/Controller/add.ctp
Expand Up @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions src/Template/Bake/Element/Controller/delete.ctp
Expand Up @@ -13,6 +13,7 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
%>

/**
* Delete method
*
Expand Down
3 changes: 2 additions & 1 deletion src/Template/Bake/Element/Controller/edit.ctp
Expand Up @@ -15,8 +15,9 @@

$belongsTo = $this->Bake->aliasExtractor($modelObj, 'BelongsTo');
$belongsToMany = $this->Bake->aliasExtractor($modelObj, 'BelongsToMany');
$compact = ["'" . $singularName . "'"];
%>
<% $compact = ["'" . $singularName . "'"]; %>

/**
* Edit method
*
Expand Down
1 change: 1 addition & 0 deletions src/Template/Bake/Element/Controller/index.ctp
Expand Up @@ -13,6 +13,7 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
%>

/**
* Index method
*
Expand Down
1 change: 1 addition & 0 deletions src/Template/Bake/Element/Controller/view.ctp
Expand Up @@ -19,6 +19,7 @@ $allAssociations = array_merge(
$this->Bake->aliasExtractor($modelObj, 'HasMany')
);
%>

/**
* View method
*
Expand Down
12 changes: 3 additions & 9 deletions tests/TestCase/Shell/Task/ControllerTaskTest.php
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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();
}

Expand All @@ -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);
}

Expand Down
118 changes: 118 additions & 0 deletions 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 changes: 14 additions & 0 deletions 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
*
Expand Down Expand Up @@ -86,3 +98,5 @@ public function delete($id = null) {
}
return $this->redirect(['action' => 'index']);
}

}

0 comments on commit 5990733

Please sign in to comment.