From 7b06ba2b3d74a41e086333135376a9b66a694d17 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 16 Jul 2009 23:12:47 -0400 Subject: [PATCH] Adding support for -admin flag when baking a controller's views. Using -admin will only bake admin methods. Refactoring some methods, removing newlines. --- cake/console/libs/tasks/view.php | 53 ++++++++---- .../cases/console/libs/tasks/view.test.php | 86 ++++++++++++++----- 2 files changed, 101 insertions(+), 38 deletions(-) diff --git a/cake/console/libs/tasks/view.php b/cake/console/libs/tasks/view.php index 40a81ea69cc..7634efff46e 100644 --- a/cake/console/libs/tasks/view.php +++ b/cake/console/libs/tasks/view.php @@ -88,7 +88,6 @@ class ViewTask extends Shell { */ function initialize() { } - /** * Execution method always used for tasks * @@ -129,6 +128,7 @@ function execute() { $this->bake($action, true); } else { $vars = $this->__loadController(); + $methods = $this->_methodsToBake(); $methods = array_diff( array_map('strtolower', get_class_methods($this->controllerName . 'Controller')), array_map('strtolower', get_class_methods('appcontroller')) @@ -136,9 +136,15 @@ function execute() { if (empty($methods)) { $methods = $this->scaffoldActions; } - $adminDelete = null; - $adminRoute = Configure::read('Routing.admin'); + if ($adminRoute && isset($this->params['admin'])) { + foreach ($methods as $i => $method) { + if (strpos($method, $adminRoute . '_') === false) { + unset($methods[$i]); + } + } + } + $adminDelete = null; if (!empty($adminRoute)) { $adminDelete = $adminRoute . '_delete'; } @@ -151,7 +157,30 @@ function execute() { } } } - +/** + * Get a list of actions that can / should have views baked for them. + * + * @return array Array of action names that should be baked + **/ + function _methodsToBake() { + $methods = array_diff( + array_map('strtolower', get_class_methods($this->controllerName . 'Controller')), + array_map('strtolower', get_class_methods('appcontroller')) + ); + if (empty($methods)) { + $methods = $this->scaffoldActions; + } + $adminRoute = Configure::read('Routing.admin'); + foreach ($methods as $i => $method) { + if ($method == 'delete' || $method = $adminRoute . '_delete' || $method{0} == '_') { + unset($methods[$i]); + } + if ($adminRoute && isset($this->params['admin']) && strpos($method, $adminRoute . '_') === false) { + unset($methods[$i]); + } + } + return $methods; + } /** * Bake All views for All controllers. * @@ -172,7 +201,6 @@ function all() { } } } - /** * Handles interactive baking * @@ -226,7 +254,6 @@ function __interactive() { $this->customAction(); } } - /** * Loads Controller and sets variables for the template * Available template variables @@ -283,7 +310,6 @@ function __loadController() { return compact('modelClass', 'schema', 'primaryKey', 'displayField', 'singularVar', 'pluralVar', 'singularHumanName', 'pluralHumanName', 'fields','associations'); } - /** * Bake a view file for each of the supplied actions * @@ -296,7 +322,6 @@ function bakeActions($actions, $vars) { $this->bake($action, $content); } } - /** * handle creation of baking a custom action view file * @@ -326,7 +351,6 @@ function customAction() { $this->out(__('Bake Aborted.', true)); } } - /** * Assembles and writes bakes the view file. * @@ -346,7 +370,6 @@ function bake($action, $content = '') { $filename = $path . $this->controllerPath . DS . Inflector::underscore($action) . '.ctp'; return $this->createFile($filename, $content); } - /** * Builds content from template and variables * @@ -385,7 +408,6 @@ function getContent($template = null, $vars = null) { $this->err(sprintf(__('Template for %s could not be found', true), $template)); return false; } - /** * Displays help contents * @@ -398,16 +420,18 @@ function help() { $this->out('Commands:'); $this->out(''); $this->out("view "); - $this->out("\twill read the given controller for methods"); + $this->out("\tWill read the given controller for methods"); $this->out("\tand bake corresponding views."); + $this->out("\tUsing the -admin flag will only bake views for actions"); + $this->out("\tthat begin with Routing.admin."); $this->out("\tIf var scaffold is found it will bake the CRUD actions"); $this->out("\t(index,view,add,edit)"); $this->out(''); $this->out("view "); - $this->out("\twill bake a template. core templates: (index, add, edit, view)"); + $this->out("\tWill bake a template. core templates: (index, add, edit, view)"); $this->out(''); $this->out("view