Skip to content

Commit 2b0f45b

Browse files
committed
Add controller option.
This option allows you to bake views based on a model for a different controller. For example bake Tasks related views into the Timesheet controller.
1 parent dba6d32 commit 2b0f45b

File tree

2 files changed

+87
-39
lines changed

2 files changed

+87
-39
lines changed

src/Console/Command/Task/ViewTask.php

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ class ViewTask extends BakeTask {
5555
*/
5656
public $controllerClass = null;
5757

58+
/**
59+
* Name of the table views are being baked against.
60+
*
61+
* @var string
62+
*/
63+
public $tableName = null;
64+
5865
/**
5966
* The template file to use
6067
*
@@ -119,7 +126,11 @@ public function execute() {
119126
return $this->all();
120127
}
121128

122-
$this->controller($this->args[0]);
129+
$controller = null;
130+
if (!empty($this->params['controller'])) {
131+
$controller = $this->params['controller'];
132+
}
133+
$this->controller($this->args[0], $controller);
123134

124135
if (isset($this->args[1])) {
125136
$this->template = $this->args[1];
@@ -148,12 +159,16 @@ public function execute() {
148159
/**
149160
* Set the controller related properties.
150161
*
151-
* @param string $name The controller name.
162+
* @param string $table The table/model that is being baked.
163+
* @param string $controller The controller name if specified.
152164
* @return void
153165
*/
154-
public function controller($name) {
155-
$name = $this->_controllerName($name);
156-
$this->controllerName = $name;
166+
public function controller($table, $controller = null) {
167+
$this->tableName = $this->_controllerName($table);
168+
if (empty($controller)) {
169+
$controller = $this->tableName;
170+
}
171+
$this->controllerName = $controller;
157172

158173
$plugin = $prefix = null;
159174
if (!empty($this->params['plugin'])) {
@@ -162,7 +177,7 @@ public function controller($name) {
162177
if (!empty($this->params['prefix'])) {
163178
$prefix = $this->params['prefix'] . '/';
164179
}
165-
$this->controllerClass = App::className($plugin . $prefix . $name, 'Controller', 'Controller');
180+
$this->controllerClass = App::className($plugin . $prefix . $controller, 'Controller', 'Controller');
166181
}
167182

168183
/**
@@ -238,46 +253,30 @@ public function all() {
238253
* @return array Returns an variables to be made available to a view template
239254
*/
240255
protected function _loadController() {
241-
if (!$this->controllerName) {
242-
$this->err(__d('cake_console', 'Controller not found'));
243-
}
244-
245256
$plugin = null;
246257
if ($this->plugin) {
247258
$plugin = $this->plugin . '.';
248259
}
249260

250-
if (class_exists($this->controllerClass)) {
251-
$controllerObj = new $this->controllerClass();
252-
$controllerObj->plugin = $this->plugin;
253-
$controllerObj->constructClasses();
254-
$modelClass = $controllerObj->modelClass;
255-
$modelObj = $controllerObj->{$modelClass};
256-
} else {
257-
$modelObj = TableRegistry::get($this->controllerName);
258-
}
261+
$modelObj = TableRegistry::get($this->tableName);
259262

260-
$primaryKey = [];
261-
$displayField = null;
262263
$singularVar = Inflector::variable(Inflector::singularize($this->controllerName));
263264
$singularHumanName = $this->_singularHumanName($this->controllerName);
264-
$fields = $schema = $keyFields = $associations = [];
265-
266-
if ($modelObj) {
267-
$primaryKey = (array)$modelObj->primaryKey();
268-
$displayField = $modelObj->displayField();
269-
$singularVar = $this->_singularName($this->controllerName);
270-
$singularHumanName = $this->_singularHumanName($this->controllerName);
271-
$schema = $modelObj->schema();
272-
$fields = $schema->columns();
273-
$associations = $this->_associations($modelObj);
274-
$keyFields = [];
275-
if (!empty($associations['BelongsTo'])) {
276-
foreach ($associations['BelongsTo'] as $assoc) {
277-
$keyFields[$assoc['foreignKey']] = $assoc['variable'];
278-
}
265+
266+
$primaryKey = (array)$modelObj->primaryKey();
267+
$displayField = $modelObj->displayField();
268+
$singularVar = $this->_singularName($this->controllerName);
269+
$singularHumanName = $this->_singularHumanName($this->controllerName);
270+
$schema = $modelObj->schema();
271+
$fields = $schema->columns();
272+
$associations = $this->_associations($modelObj);
273+
$keyFields = [];
274+
if (!empty($associations['BelongsTo'])) {
275+
foreach ($associations['BelongsTo'] as $assoc) {
276+
$keyFields[$assoc['foreignKey']] = $assoc['variable'];
279277
}
280278
}
279+
281280
$pluralVar = Inflector::variable($this->controllerName);
282281
$pluralHumanName = $this->_pluralHumanName($this->controllerName);
283282

@@ -439,6 +438,8 @@ public function getOptionParser() {
439438
'boolean' => true,
440439
'short' => 'f',
441440
'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
441+
])->addOption('controller', [
442+
'help' => __d('cake_console', 'The controller name if you have a controller that does not follow conventions.')
442443
])->addOption('prefix', [
443444
'help' => __d('cake_console', 'The routing prefix to generate views for.'),
444445
])->addSubcommand('all', [

tests/TestCase/Console/Command/Task/ViewTaskTest.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ public function testController() {
158158
public function testControllerVariations($name) {
159159
$this->Task->controller($name);
160160
$this->assertEquals('ViewTaskComments', $this->Task->controllerName);
161+
$this->assertEquals('ViewTaskComments', $this->Task->tableName);
161162
}
162163

163164
/**
@@ -169,6 +170,7 @@ public function testControllerPlugin() {
169170
$this->Task->params['plugin'] = 'TestPlugin';
170171
$this->Task->controller('Tests');
171172
$this->assertEquals('Tests', $this->Task->controllerName);
173+
$this->assertEquals('Tests', $this->Task->tableName);
172174
$this->assertEquals(
173175
'TestPlugin\Controller\TestsController',
174176
$this->Task->controllerClass
@@ -184,6 +186,7 @@ public function testControllerPrefix() {
184186
$this->Task->params['prefix'] = 'Admin';
185187
$this->Task->controller('Posts');
186188
$this->assertEquals('Posts', $this->Task->controllerName);
189+
$this->assertEquals('Posts', $this->Task->tableName);
187190
$this->assertEquals(
188191
'TestApp\Controller\Admin\PostsController',
189192
$this->Task->controllerClass
@@ -192,12 +195,28 @@ public function testControllerPrefix() {
192195
$this->Task->params['plugin'] = 'TestPlugin';
193196
$this->Task->controller('Comments');
194197
$this->assertEquals('Comments', $this->Task->controllerName);
198+
$this->assertEquals('Comments', $this->Task->tableName);
195199
$this->assertEquals(
196200
'TestPlugin\Controller\Admin\CommentsController',
197201
$this->Task->controllerClass
198202
);
199203
}
200204

205+
/**
206+
* test controller with a non-conventional controller name
207+
*
208+
* @return void
209+
*/
210+
public function testControllerWithOverride() {
211+
$this->Task->controller('Comments', 'Posts');
212+
$this->assertEquals('Posts', $this->Task->controllerName);
213+
$this->assertEquals('Comments', $this->Task->tableName);
214+
$this->assertEquals(
215+
'TestApp\Controller\PostsController',
216+
$this->Task->controllerClass
217+
);
218+
}
219+
201220
/**
202221
* Test getPath()
203222
*
@@ -313,6 +332,7 @@ public function testGetContentWithRoutingPrefix() {
313332
*/
314333
public function testBakeView() {
315334
$this->Task->controllerName = 'ViewTaskComments';
335+
$this->Task->tableName = 'ViewTaskComments';
316336
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
317337

318338
$this->Task->expects($this->at(0))
@@ -332,6 +352,7 @@ public function testBakeView() {
332352
*/
333353
public function testBakeEdit() {
334354
$this->Task->controllerName = 'ViewTaskComments';
355+
$this->Task->tableName = 'ViewTaskComments';
335356
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
336357

337358
$this->Task->expects($this->at(0))->method('createFile')
@@ -352,6 +373,7 @@ public function testBakeEdit() {
352373
*/
353374
public function testBakeIndex() {
354375
$this->Task->controllerName = 'ViewTaskComments';
376+
$this->Task->tableName = 'ViewTaskComments';
355377
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
356378

357379
$this->Task->expects($this->at(0))->method('createFile')
@@ -369,6 +391,7 @@ public function testBakeIndex() {
369391
*/
370392
public function testBakeWithNoTemplate() {
371393
$this->Task->controllerName = 'ViewTaskComments';
394+
$this->Task->tableName = 'ViewTaskComments';
372395
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
373396

374397
$this->Task->expects($this->never())->method('createFile');
@@ -382,6 +405,7 @@ public function testBakeWithNoTemplate() {
382405
*/
383406
public function testBakeActions() {
384407
$this->Task->controllerName = 'ViewTaskComments';
408+
$this->Task->tableName = 'ViewTaskComments';
385409
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
386410

387411
$this->Task->expects($this->at(0))
@@ -411,6 +435,7 @@ public function testBakeActions() {
411435
*/
412436
public function testCustomAction() {
413437
$this->Task->controllerName = 'ViewTaskComments';
438+
$this->Task->tableName = 'ViewTaskComments';
414439
$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
415440

416441
$this->Task->expects($this->any())->method('in')
@@ -495,12 +520,34 @@ public function testExecuteWithController() {
495520
* @return void
496521
*/
497522
public static function nameVariations() {
498-
return array(array('ViewTaskComments'), array('ViewTaskComment'), array('view_task_comment'));
523+
return [['ViewTaskComments'], ['ViewTaskComment'], ['view_task_comment']];
524+
}
525+
526+
/**
527+
* test `cake bake view $table --controller Blog`
528+
*
529+
* @return void
530+
*/
531+
public function testExecuteWithControllerFlag() {
532+
$this->Task->args[0] = 'Posts';
533+
$this->Task->params['controller'] = 'Blog';
534+
535+
$this->Task->expects($this->exactly(4))
536+
->method('createFile');
537+
538+
$views = array('index.ctp', 'view.ctp', 'add.ctp', 'edit.ctp');
539+
foreach ($views as $i => $view) {
540+
$this->Task->expects($this->at($i))->method('createFile')
541+
->with(
542+
TMP . 'Blog/' . $view,
543+
$this->anything()
544+
);
545+
}
546+
$this->Task->execute();
499547
}
500548

501549
/**
502-
* test `cake bake view $controller --admin`
503-
* Which only bakes admin methods, not non-admin methods.
550+
* test `cake bake view $controller --prefix Admin`
504551
*
505552
* @return void
506553
*/

0 commit comments

Comments
 (0)