Skip to content

Commit

Permalink
Adding an option parser to controller task.
Browse files Browse the repository at this point in the history
Converting the public and admin positional arguments into switches.
Updating the task and test cases to reflect the switches changes.
  • Loading branch information
markstory committed Oct 14, 2010
1 parent 5fd85c1 commit 03688bc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
46 changes: 35 additions & 11 deletions cake/console/libs/tasks/controller.php
Expand Up @@ -69,26 +69,22 @@ public function execute() {
}

$controller = $this->_controllerName($this->args[0]);
$actions = 'scaffold';
$actions = '';

if (!empty($this->args[1]) && ($this->args[1] == 'public' || $this->args[1] == 'scaffold')) {
if (!empty($this->params['public'])) {
$this->out(__('Baking basic crud methods for ') . $controller);
$actions = $this->bakeActions($controller);
} elseif (!empty($this->args[1]) && $this->args[1] == 'admin') {
$admin = $this->Project->getPrefix();
if ($admin) {
$this->out(sprintf(__('Adding %s methods'), $admin));
$actions = $this->bakeActions($controller, $admin);
}
$actions .= $this->bakeActions($controller);
}

if (!empty($this->args[2]) && $this->args[2] == 'admin') {
if (!empty($this->params['admin'])) {
$admin = $this->Project->getPrefix();
if ($admin) {
$this->out(sprintf(__('Adding %s methods'), $admin));
$actions .= "\n" . $this->bakeActions($controller, $admin);
}
}
if (empty($actions)) {
$actions = 'scaffold';
}

if ($this->bake($controller, $actions)) {
if ($this->_checkUnitTest()) {
Expand Down Expand Up @@ -431,6 +427,34 @@ public function getName($useDbConfig = null) {
return $controllerName;
}

/**
* get the option parser.
*
* @return void
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
return $parser->description(
__('Bake a controller for a model. Using options you can bake public, admin or both.')
)->addArgument('name', array(
'help' => __('Name of the controller to bake. Can use Plugin.name to bake controllers into plugins.')
))->addOption('public', array(
'help' => __('Bake a controller with basic crud actions (index, view, add, edit, delete).'),
'boolean' => true
))->addOption('admin', array(
'help' => __('Bake a controller with crud actions for one of the Routing.prefixes.'),
'boolean' => true
))->addOption('plugin', array(
'short' => 'p',
'help' => __('Plugin to bake the controller into.')
))->addOption('connection', array(
'short' => 'c',
'help' => __('The connection the controller\'s model is on.')
))->addSubcommand('all', array(
'help' => __('Bake all controllers with CRUD methods.')
))->epilog(__('Omitting all arguments and options will enter into an interactive mode.'));
}

/**
* Displays help contents
*
Expand Down
9 changes: 6 additions & 3 deletions cake/tests/cases/console/libs/tasks/controller.test.php
Expand Up @@ -590,7 +590,8 @@ public function testExecuteWithPublicParam() {
}
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
$this->Task->args = array('BakeArticles', 'public');
$this->Task->args = array('BakeArticles');
$this->Task->params = array('public' => true);

$filename = '/my/path/bake_articles_controller.php';
$expected = new PHPUnit_Framework_Constraint_Not(new PHPUnit_Framework_Constraint_PCREMatch('/\$scaffold/'));
Expand All @@ -612,7 +613,8 @@ public function testExecuteWithControllerAndBoth() {
$this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
$this->Task->args = array('BakeArticles', 'public', 'admin');
$this->Task->args = array('BakeArticles');
$this->Task->params = array('public' => true, 'admin' => true);

$filename = '/my/path/bake_articles_controller.php';
$this->Task->expects($this->once())->method('createFile')->with(
Expand All @@ -633,7 +635,8 @@ public function testExecuteWithControllerAndAdmin() {
$this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
$this->Task->args = array('BakeArticles', 'admin');
$this->Task->args = array('BakeArticles');
$this->Task->params = array('admin' => true);

$filename = '/my/path/bake_articles_controller.php';
$this->Task->expects($this->once())->method('createFile')->with(
Expand Down

0 comments on commit 03688bc

Please sign in to comment.