From 03688bc2ff846420c815283b3ab6c3699b5d34dd Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 12 Oct 2010 23:23:18 -0400 Subject: [PATCH] Adding an option parser to controller task. Converting the public and admin positional arguments into switches. Updating the task and test cases to reflect the switches changes. --- cake/console/libs/tasks/controller.php | 46 ++++++++++++++----- .../console/libs/tasks/controller.test.php | 9 ++-- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/cake/console/libs/tasks/controller.php b/cake/console/libs/tasks/controller.php index 21f53b0dff2..5f33a8cf968 100644 --- a/cake/console/libs/tasks/controller.php +++ b/cake/console/libs/tasks/controller.php @@ -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()) { @@ -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 * diff --git a/cake/tests/cases/console/libs/tasks/controller.test.php b/cake/tests/cases/console/libs/tasks/controller.test.php index 43c758291c8..849cf908864 100644 --- a/cake/tests/cases/console/libs/tasks/controller.test.php +++ b/cake/tests/cases/console/libs/tasks/controller.test.php @@ -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/')); @@ -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( @@ -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(