Skip to content

Commit

Permalink
Add new implementation for helper and component generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 20, 2014
1 parent b9aa687 commit 3db08e5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 30 deletions.
86 changes: 62 additions & 24 deletions src/Console/Command/Task/ControllerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,8 @@ public function execute() {
}

$controller = $this->_controllerName($this->args[0]);
$this->out(__d('cake_console', 'Baking basic crud methods for ') . $controller);
$actions = $this->bakeActions($controller);

$this->bake($controller, $actions);
$this->bakeTest($controller);
$this->bake($controller);
}

/**
Expand Down Expand Up @@ -303,26 +300,45 @@ public function bakeActions($controllerName, $admin = null) {
* Assembles and writes a Controller file
*
* @param string $controllerName Controller name already pluralized and correctly cased.
* @param string $actions Actions to add, or set the whole controller to use $scaffold (set $actions to 'scaffold')
* @param array $helpers Helpers to use in controller
* @param array $components Components to use in controller
* @return string Baked controller
*/
public function bake($controllerName, $actions = '', $helpers = null, $components = null) {
public function bake($controllerName) {
$this->out("\n" . __d('cake_console', 'Baking controller class for %s...', $controllerName), 1, Shell::QUIET);

$isScaffold = ($actions === 'scaffold') ? true : false;
$actions = $this->bakeActions($controllerName);
$helpers = $this->getHelpers();
$components = $this->getComponents();
$prefix = $this->params['prefix'];

$this->Template->set([
'plugin' => $this->plugin,
'pluginPath' => empty($this->plugin) ? '' : $this->plugin . '.'
]);

if (!in_array('Paginator', (array)$components)) {
$components[] = 'Paginator';
$namespace = Configure::read('App.namespace');
$pluginPath = '';
if ($this->plugin) {
$namespace = $this->plugin;
$pluginPath = $this->plugin . '.';
}
$data = compact(
'actions', 'helpers', 'components',
'prefix', 'namespace', 'pluginPath'
);
$data['name'] = $controllerName;

$this->bakeController($controllerName, $data);
$this->bakeTest($controllerName);
}

public function bakeController($controllerName, $data) {
$data += [
'name' => null,
'namespace' => null,
'prefix' => null,
'actions' => null,
'helpers' => null,
'components' => null,
'plugin' => null,
'pluginPath' => null,
];
$this->Template->set($data);

$this->Template->set(compact('controllerName', 'actions', 'helpers', 'components', 'isScaffold'));
$contents = $this->Template->generate('classes', 'controller');

$path = $this->getPath();
Expand Down Expand Up @@ -350,15 +366,37 @@ public function bakeTest($className) {
}

/**
* Interact with the user and get a list of additional helpers
* Get the list of components for the controller.
*
* @return array Helpers that the user wants to use.
* @return array
*/
public function doHelpers() {
return $this->_doPropertyChoices(
__d('cake_console', "Would you like this controller to use other helpers\nbesides HtmlHelper and FormHelper?"),
__d('cake_console', "Please provide a comma separated list of the other\nhelper names you'd like to use.\nExample: 'Text, Js, Time'")
);
public function getComponents() {
$components = [];
if (!empty($this->params['components'])) {
$components = explode(',', $this->params['components']);
$components = array_values(array_filter(array_map('trim', $components)));
}
if (!in_array('Paginator', $components)) {
$components[] = 'Paginator';
}
return $components;
}

/**
* Get the list of helpers for the controller.
*
* @return array
*/
public function getHelpers() {
$helpers = [];
if (!empty($this->params['helpers'])) {
$helpers = explode(',', $this->params['helpers']);
$helpers = array_values(array_filter(array_map('trim', $helpers)));
}
if (count($helpers) && !in_array('Form', $helpers)) {
$helpers[] = 'Form';
}
return $helpers;
}

/**
Expand Down
36 changes: 30 additions & 6 deletions tests/TestCase/Console/Command/Task/ControllerTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,39 @@ public function testListAll() {
}

/**
* test helper interactions
* test component generation
*
* @return void
*/
public function testDoHelpersNo() {
$this->markTestIncomplete();
$this->Task->expects($this->any())->method('in')->will($this->returnValue('n'));
$result = $this->Task->doHelpers();
$this->assertSame(array(), $result);
public function testGetComponents() {
$result = $this->Task->getComponents();
$this->assertSame(['Paginator'], $result);

$this->Task->params['components'] = ' , Security, , Csrf';
$result = $this->Task->getComponents();
$this->assertSame(['Security', 'Csrf', 'Paginator'], $result);

$this->Task->params['components'] = ' Paginator , Security, , Csrf';
$result = $this->Task->getComponents();
$this->assertSame(['Paginator', 'Security', 'Csrf'], $result);
}

/**
* test helper generation
*
* @return void
*/
public function testGetHelpers() {
$result = $this->Task->getHelpers();
$this->assertSame([], $result);

$this->Task->params['helpers'] = ' , Session , , Number';
$result = $this->Task->getHelpers();
$this->assertSame(['Session', 'Number', 'Form'], $result);

$this->Task->params['helpers'] = ' Session , Number , , Form';
$result = $this->Task->getHelpers();
$this->assertSame(['Session', 'Number', 'Form'], $result);
}

/**
Expand Down

0 comments on commit 3db08e5

Please sign in to comment.