Skip to content

Commit

Permalink
Updating and refactoring help text for Cake console, adding support f…
Browse files Browse the repository at this point in the history
…or reverse-route lookups
  • Loading branch information
nateabele committed Feb 6, 2009
1 parent c636b28 commit c200fcb
Showing 1 changed file with 72 additions and 50 deletions.
122 changes: 72 additions & 50 deletions cake/console/libs/console.php
Expand Up @@ -72,15 +72,69 @@ function initialize() {
foreach ($this->models as $model) {
$this->out(" - {$model}");
}
$this->__loadRoutes();
$this->_loadRoutes();
}
/**
* Prints the help message
*
* @access public
*/
function help() {
$this->main('help');
$out = 'Console help:';
$out .= '-------------';
$out .= 'The interactive console is a tool for testing parts of your app before you';
$out .= 'write code.';
$out .= "\n";
$out .= 'Model testing:';
$out .= 'To test model results, use the name of your model without a leading $';
$out .= 'e.g. Foo->find("all")';
$out .= "\n";
$out .= 'To dynamically set associations, you can do the following:';
$out .= "\tModelA bind <association> ModelB";
$out .= "where the supported assocations are hasOne, hasMany, belongsTo, hasAndBelongsToMany";
$out .= "\n";
$out .= 'To dynamically remove associations, you can do the following:';
$out .= "\t ModelA unbind <association> ModelB";
$out .= "where the supported associations are the same as above";
$out .= "\n";
$out .= "To save a new field in a model, you can do the following:";
$out .= "\tModelA->save(array('foo' => 'bar', 'baz' => 0))";
$out .= "where you are passing a hash of data to be saved in the format";
$out .= "of field => value pairs";
$out .= "\n";
$out .= "To get column information for a model, use the following:";
$out .= "\tModelA columns";
$out .= "which returns a list of columns and their type";
$out .= "\n";
$out .= "\n";
$out .= 'Route testing:';
$out .= "\n";
$out .= 'To test URLs against your app\'s route configuration, type:';
$out .= "\n";
$out .= "\tRoute <url>";
$out .= "\n";
$out .= "where url is the path to your your action plus any query parameters,";
$out .= "minus the application's base path. For example:";
$out .= "\n";
$out .= "\tRoute /posts/view/1";
$out .= "\n";
$out .= "will return something like the following:";
$out .= "\n";
$out .= "\tarray (";
$out .= "\t [...]";
$out .= "\t 'controller' => 'posts',";
$out .= "\t 'action' => 'view',";
$out .= "\t [...]";
$out .= "\t)";
$out .= "\n";
$out .= 'Alternatively, you can use simple array syntax to test reverse';
$out .= 'To reload your routes config (config/routes.php), do the following:';
$out .= "\n";
$out .= "\tRoutes reload";
$out .= "\n";
$out .= 'To show all connected routes, do the following:';
$out .= "\tRoutes show";
$this->out($out);
}
/**
* Override main() to handle action
Expand All @@ -95,44 +149,7 @@ function main($command = null) {

switch ($command) {
case 'help':
$this->out('Console help:');
$this->out('-------------');
$this->out('The interactive console is a tool for testing parts of your app before you commit code');
$this->out('');
$this->out('Model testing:');
$this->out('To test model results, use the name of your model without a leading $');
$this->out('e.g. Foo->find("all")');
$this->out('');
$this->out('To dynamically set associations, you can do the following:');
$this->out("\tModelA bind <association> ModelB");
$this->out("where the supported assocations are hasOne, hasMany, belongsTo, hasAndBelongsToMany");
$this->out('');
$this->out('To dynamically remove associations, you can do the following:');
$this->out("\t ModelA unbind <association> ModelB");
$this->out("where the supported associations are the same as above");
$this->out('');
$this->out("To save a new field in a model, you can do the following:");
$this->out("\tModelA->save(array('foo' => 'bar', 'baz' => 0))");
$this->out("where you are passing a hash of data to be saved in the format");
$this->out("of field => value pairs");
$this->out('');
$this->out("To get column information for a model, use the following:");
$this->out("\tModelA columns");
$this->out("which returns a list of columns and their type");
$this->out('');
$this->out('Route testing:');
$this->out('To test URLs against your app\'s route configuration, type:');
$this->out("\tRoute <url>");
$this->out("where url is the path to your your action plus any query parameters, minus the");
$this->out("application's base path");
$this->out('');
$this->out('To reload your routes config (config/routes.php), do the following:');
$this->out("\tRoutes reload");
$this->out('');
$this->out('');
$this->out('To show all connected routes, do the following:');
$this->out("\tRoutes show");
$this->out('');
$this->help();
break;
case 'quit':
case 'exit':
Expand All @@ -155,7 +172,7 @@ function main($command = null) {
$association = $tmp[2];
$modelB = $tmp[3];

if ($this->__isValidModel($modelA) && $this->__isValidModel($modelB) && in_array($association, $this->associations)) {
if ($this->_isValidModel($modelA) && $this->_isValidModel($modelB) && in_array($association, $this->associations)) {
$this->{$modelA}->bindModel(array($association => array($modelB => array('className' => $modelB))), false);
$this->out("Created $association association between $modelA and $modelB");
} else {
Expand All @@ -182,7 +199,7 @@ function main($command = null) {
}
}

if ($this->__isValidModel($modelA) && $this->__isValidModel($modelB) && in_array($association, $this->associations) && $validCurrentAssociation) {
if ($this->_isValidModel($modelA) && $this->_isValidModel($modelB) && in_array($association, $this->associations) && $validCurrentAssociation) {
$this->{$modelA}->unbindModel(array($association => array($modelB)));
$this->out("Removed $association association between $modelA and $modelB");
} else {
Expand All @@ -197,7 +214,7 @@ function main($command = null) {
// Do we have a valid model?
list($modelToCheck, $tmp) = explode('->', $command);

if ($this->__isValidModel($modelToCheck)) {
if ($this->_isValidModel($modelToCheck)) {
$findCommand = "\$data = \$this->$command;";
@eval($findCommand);

Expand Down Expand Up @@ -249,7 +266,7 @@ function main($command = null) {
$command = str_replace($this->badCommandChars, "", $command);
list($modelToSave, $tmp) = explode("->", $command);

if ($this->__isValidModel($modelToSave)) {
if ($this->_isValidModel($modelToSave)) {
// Extract the array of data we are trying to build
list($foo, $data) = explode("->save", $command);
$badChars = array("(", ")");
Expand All @@ -262,7 +279,7 @@ function main($command = null) {
case (preg_match("/^(\w+) columns/", $command, $tmp) == true):
$modelToCheck = strip_tags(str_replace($this->badCommandChars, "", $tmp[1]));

if ($this->__isValidModel($modelToCheck)) {
if ($this->_isValidModel($modelToCheck)) {
// Get the column info for this model
$fieldsCommand = "\$data = \$this->{$modelToCheck}->getColumnTypes();";
@eval($fieldsCommand);
Expand All @@ -278,7 +295,7 @@ function main($command = null) {
break;
case (preg_match("/^routes\s+reload/i", $command, $tmp) == true):
$router =& Router::getInstance();
if (!$this->__loadRoutes()) {
if (!$this->_loadRoutes()) {
$this->out("There was an error loading the routes config. Please check that the file");
$this->out("exists and is free of parse errors.");
break;
Expand All @@ -289,6 +306,11 @@ function main($command = null) {
$router =& Router::getInstance();
$this->out(join("\n", Set::extract($router->routes, '{n}.0')));
break;
case (preg_match("/^route\s+(\(.*\))$/i", $command, $tmp) == true):
if ($url = eval('return array' . $tmp[1] . ';')) {
$this->out(Router::url($url));
}
break;
case (preg_match("/^route\s+(.*)/i", $command, $tmp) == true):
$this->out(var_export(Router::parse($tmp[1]), true));
break;
Expand All @@ -304,19 +326,19 @@ function main($command = null) {
*
* @param string $modelToCheck
* @return boolean true if is an available model, false otherwise
* @access private
* @access protected
*/
function __isValidModel($modelToCheck) {
function _isValidModel($modelToCheck) {
return in_array($modelToCheck, $this->models);
}
/**
* Reloads the routes configuration from config/routes.php, and compiles
* all routes found
*
* @return boolean True if config reload was a success, otherwise false
* @access private
* @access protected
*/
function __loadRoutes() {
function _loadRoutes() {
$router =& Router::getInstance();

$router->reload();
Expand Down

0 comments on commit c200fcb

Please sign in to comment.