Skip to content
This repository
Browse code

Updating and refactoring help text for Cake console, adding support f…

…or reverse-route lookups
  • Loading branch information...
commit c200fcb062feb60236a557bd6d89eb152809e699 1 parent c636b28
Nate Abele authored February 06, 2009

Showing 1 changed file with 72 additions and 50 deletions. Show diff stats Hide diff stats

  1. 122  cake/console/libs/console.php
122  cake/console/libs/console.php
@@ -72,7 +72,7 @@ function initialize() {
72 72
 		foreach ($this->models as $model) {
73 73
 			$this->out(" - {$model}");
74 74
 		}
75  
-		$this->__loadRoutes();
  75
+		$this->_loadRoutes();
76 76
 	}
77 77
 /**
78 78
  * Prints the help message
@@ -80,7 +80,61 @@ function initialize() {
80 80
  * @access public
81 81
  */
82 82
 	function help() {
83  
-		$this->main('help');
  83
+		$out  = 'Console help:';
  84
+		$out .= '-------------';
  85
+		$out .= 'The interactive console is a tool for testing parts of your app before you';
  86
+		$out .= 'write code.';
  87
+		$out .= "\n";
  88
+		$out .= 'Model testing:';
  89
+		$out .= 'To test model results, use the name of your model without a leading $';
  90
+		$out .= 'e.g. Foo->find("all")';
  91
+		$out .= "\n";
  92
+		$out .= 'To dynamically set associations, you can do the following:';
  93
+		$out .= "\tModelA bind <association> ModelB";
  94
+		$out .= "where the supported assocations are hasOne, hasMany, belongsTo, hasAndBelongsToMany";
  95
+		$out .= "\n";
  96
+		$out .= 'To dynamically remove associations, you can do the following:';
  97
+		$out .= "\t ModelA unbind <association> ModelB";
  98
+		$out .= "where the supported associations are the same as above";
  99
+		$out .= "\n";
  100
+		$out .= "To save a new field in a model, you can do the following:";
  101
+		$out .= "\tModelA->save(array('foo' => 'bar', 'baz' => 0))";
  102
+		$out .= "where you are passing a hash of data to be saved in the format";
  103
+		$out .= "of field => value pairs";
  104
+		$out .= "\n";
  105
+		$out .= "To get column information for a model, use the following:";
  106
+		$out .= "\tModelA columns";
  107
+		$out .= "which returns a list of columns and their type";
  108
+		$out .= "\n";
  109
+		$out .= "\n";
  110
+		$out .= 'Route testing:';
  111
+		$out .= "\n";
  112
+		$out .= 'To test URLs against your app\'s route configuration, type:';
  113
+		$out .= "\n";
  114
+		$out .= "\tRoute <url>";
  115
+		$out .= "\n";
  116
+		$out .= "where url is the path to your your action plus any query parameters,";
  117
+		$out .= "minus the application's base path.  For example:";
  118
+		$out .= "\n";
  119
+		$out .= "\tRoute /posts/view/1";
  120
+		$out .= "\n";
  121
+		$out .= "will return something like the following:";
  122
+		$out .= "\n";
  123
+		$out .= "\tarray (";
  124
+		$out .= "\t  [...]";
  125
+		$out .= "\t  'controller' => 'posts',";
  126
+		$out .= "\t  'action' => 'view',";
  127
+		$out .= "\t  [...]";
  128
+		$out .= "\t)";
  129
+		$out .= "\n";
  130
+		$out .= 'Alternatively, you can use simple array syntax to test reverse';
  131
+		$out .= 'To reload your routes config (config/routes.php), do the following:';
  132
+		$out .= "\n";
  133
+		$out .= "\tRoutes reload";
  134
+		$out .= "\n";
  135
+		$out .= 'To show all connected routes, do the following:';
  136
+		$out .= "\tRoutes show";
  137
+		$this->out($out);
84 138
 	}
85 139
 /**
86 140
  * Override main() to handle action
@@ -95,44 +149,7 @@ function main($command = null) {
95 149
 
96 150
 			switch ($command) {
97 151
 				case 'help':
98  
-					$this->out('Console help:');
99  
-					$this->out('-------------');
100  
-					$this->out('The interactive console is a tool for testing parts of your app before you commit code');
101  
-					$this->out('');
102  
-					$this->out('Model testing:');
103  
-					$this->out('To test model results, use the name of your model without a leading $');
104  
-					$this->out('e.g. Foo->find("all")');
105  
-					$this->out('');
106  
-					$this->out('To dynamically set associations, you can do the following:');
107  
-					$this->out("\tModelA bind <association> ModelB");
108  
-					$this->out("where the supported assocations are hasOne, hasMany, belongsTo, hasAndBelongsToMany");
109  
-					$this->out('');
110  
-					$this->out('To dynamically remove associations, you can do the following:');
111  
-					$this->out("\t ModelA unbind <association> ModelB");
112  
-					$this->out("where the supported associations are the same as above");
113  
-					$this->out('');
114  
-					$this->out("To save a new field in a model, you can do the following:");
115  
-					$this->out("\tModelA->save(array('foo' => 'bar', 'baz' => 0))");
116  
-					$this->out("where you are passing a hash of data to be saved in the format");
117  
-					$this->out("of field => value pairs");
118  
-					$this->out('');
119  
-					$this->out("To get column information for a model, use the following:");
120  
-					$this->out("\tModelA columns");
121  
-					$this->out("which returns a list of columns and their type");
122  
-					$this->out('');
123  
-					$this->out('Route testing:');
124  
-					$this->out('To test URLs against your app\'s route configuration, type:');
125  
-					$this->out("\tRoute <url>");
126  
-					$this->out("where url is the path to your your action plus any query parameters, minus the");
127  
-					$this->out("application's base path");
128  
-					$this->out('');
129  
-					$this->out('To reload your routes config (config/routes.php), do the following:');
130  
-					$this->out("\tRoutes reload");
131  
-					$this->out('');
132  
-					$this->out('');
133  
-					$this->out('To show all connected routes, do the following:');
134  
-					$this->out("\tRoutes show");
135  
-					$this->out('');
  152
+					$this->help();
136 153
 				break;
137 154
 				case 'quit':
138 155
 				case 'exit':
@@ -155,7 +172,7 @@ function main($command = null) {
155 172
 					$association = $tmp[2];
156 173
 					$modelB = $tmp[3];
157 174
 
158  
-					if ($this->__isValidModel($modelA) && $this->__isValidModel($modelB) && in_array($association, $this->associations)) {
  175
+					if ($this->_isValidModel($modelA) && $this->_isValidModel($modelB) && in_array($association, $this->associations)) {
159 176
 						$this->{$modelA}->bindModel(array($association => array($modelB => array('className' => $modelB))), false);
160 177
 						$this->out("Created $association association between $modelA and $modelB");
161 178
 					} else {
@@ -182,7 +199,7 @@ function main($command = null) {
182 199
 						}
183 200
 					}
184 201
 
185  
-					if ($this->__isValidModel($modelA) && $this->__isValidModel($modelB) && in_array($association, $this->associations) && $validCurrentAssociation) {
  202
+					if ($this->_isValidModel($modelA) && $this->_isValidModel($modelB) && in_array($association, $this->associations) && $validCurrentAssociation) {
186 203
 						$this->{$modelA}->unbindModel(array($association => array($modelB)));
187 204
 						$this->out("Removed $association association between $modelA and $modelB");
188 205
 					} else {
@@ -197,7 +214,7 @@ function main($command = null) {
197 214
 					// Do we have a valid model?
198 215
 					list($modelToCheck, $tmp) = explode('->', $command);
199 216
 
200  
-					if ($this->__isValidModel($modelToCheck)) {
  217
+					if ($this->_isValidModel($modelToCheck)) {
201 218
 						$findCommand = "\$data = \$this->$command;";
202 219
 						@eval($findCommand);
203 220
 
@@ -249,7 +266,7 @@ function main($command = null) {
249 266
 					$command = str_replace($this->badCommandChars, "", $command);
250 267
 					list($modelToSave, $tmp) = explode("->", $command);
251 268
 
252  
-					if ($this->__isValidModel($modelToSave)) {
  269
+					if ($this->_isValidModel($modelToSave)) {
253 270
 						// Extract the array of data we are trying to build
254 271
 						list($foo, $data) = explode("->save", $command);
255 272
 						$badChars = array("(", ")");
@@ -262,7 +279,7 @@ function main($command = null) {
262 279
 				case (preg_match("/^(\w+) columns/", $command, $tmp) == true):
263 280
 					$modelToCheck = strip_tags(str_replace($this->badCommandChars, "", $tmp[1]));
264 281
 
265  
-					if ($this->__isValidModel($modelToCheck)) {
  282
+					if ($this->_isValidModel($modelToCheck)) {
266 283
 						// Get the column info for this model
267 284
 						$fieldsCommand = "\$data = \$this->{$modelToCheck}->getColumnTypes();";
268 285
 						@eval($fieldsCommand);
@@ -278,7 +295,7 @@ function main($command = null) {
278 295
 				break;
279 296
 				case (preg_match("/^routes\s+reload/i", $command, $tmp) == true):
280 297
 					$router =& Router::getInstance();
281  
-					if (!$this->__loadRoutes()) {
  298
+					if (!$this->_loadRoutes()) {
282 299
 						$this->out("There was an error loading the routes config.  Please check that the file");
283 300
 						$this->out("exists and is free of parse errors.");
284 301
 						break;
@@ -289,6 +306,11 @@ function main($command = null) {
289 306
 					$router =& Router::getInstance();
290 307
 					$this->out(join("\n", Set::extract($router->routes, '{n}.0')));
291 308
 				break;
  309
+				case (preg_match("/^route\s+(\(.*\))$/i", $command, $tmp) == true):
  310
+					if ($url = eval('return array' . $tmp[1] . ';')) {
  311
+						$this->out(Router::url($url));
  312
+					}
  313
+				break;
292 314
 				case (preg_match("/^route\s+(.*)/i", $command, $tmp) == true):
293 315
 					$this->out(var_export(Router::parse($tmp[1]), true));
294 316
 				break;
@@ -304,9 +326,9 @@ function main($command = null) {
304 326
  *
305 327
  * @param string $modelToCheck
306 328
  * @return boolean true if is an available model, false otherwise
307  
- * @access private
  329
+ * @access protected
308 330
  */
309  
-	function __isValidModel($modelToCheck) {
  331
+	function _isValidModel($modelToCheck) {
310 332
 		return in_array($modelToCheck, $this->models);
311 333
 	}
312 334
 /**
@@ -314,9 +336,9 @@ function __isValidModel($modelToCheck) {
314 336
  * all routes found
315 337
  *
316 338
  * @return boolean True if config reload was a success, otherwise false
317  
- * @access private
  339
+ * @access protected
318 340
  */
319  
-	function __loadRoutes() {
  341
+	function _loadRoutes() {
320 342
 		$router =& Router::getInstance();
321 343
 
322 344
 		$router->reload();

0 notes on commit c200fcb

Please sign in to comment.
Something went wrong with that request. Please try again.