diff --git a/cake/console/libs/console.php b/cake/console/libs/console.php index 9d7f9aa4930..e04ba782c61 100644 --- a/cake/console/libs/console.php +++ b/cake/console/libs/console.php @@ -72,7 +72,7 @@ function initialize() { foreach ($this->models as $model) { $this->out(" - {$model}"); } - $this->__loadRoutes(); + $this->_loadRoutes(); } /** * Prints the help message @@ -80,7 +80,61 @@ function initialize() { * @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 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 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 "; + $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 @@ -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 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 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 "); - $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': @@ -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 { @@ -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 { @@ -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); @@ -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("(", ")"); @@ -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); @@ -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; @@ -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; @@ -304,9 +326,9 @@ 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); } /** @@ -314,9 +336,9 @@ function __isValidModel($modelToCheck) { * 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();