2007-03-09 23:15:03 +00:00
|
|
|
<?php
|
2007-03-27 05:10:42 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
2007-03-09 23:15:03 +00:00
|
|
|
/**
|
2007-03-27 05:10:42 +00:00
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2007-03-09 23:15:03 +00:00
|
|
|
*
|
2007-03-27 05:10:42 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
|
* @package cake
|
2007-05-14 01:14:41 +00:00
|
|
|
* @subpackage cake.cake.console.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.5012
|
2007-03-27 05:10:42 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @package cake
|
2007-05-14 01:14:41 +00:00
|
|
|
* @subpackage cake.cake.console.libs
|
2007-03-09 23:15:03 +00:00
|
|
|
*/
|
2007-05-12 21:18:07 +00:00
|
|
|
class ConsoleShell extends Shell {
|
2007-05-24 03:03:15 +00:00
|
|
|
var $associations = array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany');
|
2007-05-28 16:06:12 +00:00
|
|
|
var $badCommandChars = array('$', ';');
|
2007-05-25 03:38:40 +00:00
|
|
|
|
2007-05-24 15:26:50 +00:00
|
|
|
function initialize() {
|
|
|
|
$this->models = @loadModels();
|
|
|
|
foreach ($this->models as $model) {
|
2007-03-27 05:10:42 +00:00
|
|
|
$class = Inflector::camelize(r('.php', '', $model));
|
2007-05-24 15:26:50 +00:00
|
|
|
$this->models[$model] = $class;
|
|
|
|
$this->{$class} =& new $class();
|
2007-03-27 05:10:42 +00:00
|
|
|
}
|
2007-05-24 15:26:50 +00:00
|
|
|
$this->out('Model classes:');
|
|
|
|
$this->out('--------------');
|
|
|
|
|
|
|
|
foreach ($this->models as $model) {
|
|
|
|
$this->out(" - {$model}");
|
|
|
|
}
|
|
|
|
}
|
2007-05-25 03:38:40 +00:00
|
|
|
|
2007-05-24 15:26:50 +00:00
|
|
|
function main() {
|
2007-03-09 23:15:03 +00:00
|
|
|
|
2007-03-27 05:10:42 +00:00
|
|
|
while (true) {
|
|
|
|
$command = trim($this->in(''));
|
2007-03-23 02:00:10 +00:00
|
|
|
|
2007-03-27 05:10:42 +00:00
|
|
|
switch($command) {
|
2007-05-24 03:03:15 +00:00
|
|
|
case 'help':
|
|
|
|
$this->out('Console help:');
|
|
|
|
$this->out('-------------');
|
|
|
|
$this->out('The interactive console is a tool for testing models before you commit code');
|
|
|
|
$this->out('');
|
|
|
|
$this->out('To test for results, use the name of your model without a leading $');
|
|
|
|
$this->out('e.g. Foo->findAll()');
|
2007-05-25 19:52:30 +00:00
|
|
|
$this->out('');
|
|
|
|
$this->out('To dynamically set associations, you can do the following:');
|
2007-05-28 16:06:12 +00:00
|
|
|
$this->out("\tModelA bind <association> ModelB");
|
2007-05-25 19:52:30 +00:00
|
|
|
$this->out("where the supported assocations are hasOne, hasMany, belongsTo, hasAndBelongsToMany");
|
2007-05-28 16:06:12 +00:00
|
|
|
$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");
|
2007-05-29 21:07:39 +00:00
|
|
|
$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");
|
2007-05-30 01:49:00 +00:00
|
|
|
$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");
|
2007-05-24 03:03:15 +00:00
|
|
|
break;
|
2007-03-27 05:10:42 +00:00
|
|
|
case 'quit':
|
|
|
|
case 'exit':
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case 'models':
|
|
|
|
$this->out('Model classes:');
|
2007-05-24 16:34:07 +00:00
|
|
|
$this->hr();
|
2007-05-24 15:26:50 +00:00
|
|
|
foreach ($this->models as $model) {
|
2007-03-27 05:10:42 +00:00
|
|
|
$this->out(" - {$model}");
|
|
|
|
}
|
|
|
|
break;
|
2007-05-28 16:06:12 +00:00
|
|
|
case (preg_match("/^(\w+) bind (\w+) (\w+)/", $command, $tmp) == true):
|
|
|
|
foreach ($tmp as $data) {
|
|
|
|
$data = strip_tags($data);
|
|
|
|
$data = str_replace($this->badCommandChars, "", $data);
|
|
|
|
}
|
2007-06-21 14:38:46 +00:00
|
|
|
|
2007-05-28 16:06:12 +00:00
|
|
|
$modelA = $tmp[1];
|
|
|
|
$association = $tmp[2];
|
|
|
|
$modelB = $tmp[3];
|
|
|
|
|
|
|
|
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 {
|
|
|
|
$this->out("Please verify you are using valid models and association types");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case (preg_match("/^(\w+) unbind (\w+) (\w+)/", $command, $tmp) == true):
|
|
|
|
foreach ($tmp as $data) {
|
|
|
|
$data = strip_tags($data);
|
|
|
|
$data = str_replace($this->badCommandChars, "", $data);
|
|
|
|
}
|
2007-06-21 14:38:46 +00:00
|
|
|
|
2007-05-28 16:06:12 +00:00
|
|
|
$modelA = $tmp[1];
|
|
|
|
$association = $tmp[2];
|
|
|
|
$modelB = $tmp[3];
|
2007-06-21 14:38:46 +00:00
|
|
|
|
2007-05-28 16:06:12 +00:00
|
|
|
// Verify that there is actually an association to unbind
|
|
|
|
$currentAssociations = $this->{$modelA}->getAssociated();
|
|
|
|
$validCurrentAssociation = false;
|
|
|
|
|
|
|
|
foreach ($currentAssociations as $model => $currentAssociation) {
|
|
|
|
if ($model == $modelB && $association == $currentAssociation) {
|
|
|
|
$validCurrentAssociation = true;
|
2007-05-24 03:03:15 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-09 23:15:03 +00:00
|
|
|
|
2007-05-28 16:06:12 +00:00
|
|
|
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 {
|
|
|
|
$this->out("Please verify you are using valid models, valid current association, and valid association types");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case (strpos($command, "->find") > 0):
|
|
|
|
// Remove any bad info
|
|
|
|
$command = strip_tags($command);
|
|
|
|
$command = str_replace($this->badCommandChars, "", $command);
|
|
|
|
|
|
|
|
// Do we have a valid model?
|
|
|
|
list($modelToCheck, $tmp) = explode('->', $command);
|
2007-06-21 14:38:46 +00:00
|
|
|
|
|
|
|
if ($this->isValidModel($modelToCheck)) {
|
2007-05-28 16:06:12 +00:00
|
|
|
$findCommand = "\$data = \$this->$command;";
|
|
|
|
@eval($findCommand);
|
2007-05-25 21:45:39 +00:00
|
|
|
|
2007-05-28 16:06:12 +00:00
|
|
|
if (is_array($data)) {
|
|
|
|
foreach ($data as $idx => $results) {
|
|
|
|
if (is_numeric($idx)) { // findAll() output
|
|
|
|
foreach ($results as $modelName => $result) {
|
|
|
|
$this->out("$modelName");
|
|
|
|
|
|
|
|
foreach ($result as $field => $value) {
|
2007-05-25 19:52:30 +00:00
|
|
|
if (is_array($value)) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($value as $field2 => $value2) {
|
2007-05-25 19:52:30 +00:00
|
|
|
$this->out("\t$field2: $value2");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->out("");
|
|
|
|
} else {
|
|
|
|
$this->out("\t$field: $value");
|
|
|
|
}
|
|
|
|
}
|
2007-05-24 03:03:15 +00:00
|
|
|
}
|
2007-05-28 16:06:12 +00:00
|
|
|
} else { // find() output
|
|
|
|
$this->out($idx);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($results as $field => $value) {
|
2007-05-28 16:06:12 +00:00
|
|
|
if (is_array($value)) {
|
|
|
|
foreach ($value as $field2 => $value2) {
|
|
|
|
$this->out("\t$field2: $value2");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->out("");
|
|
|
|
} else {
|
|
|
|
$this->out("\t$field: $value");
|
|
|
|
}
|
|
|
|
}
|
2007-05-25 19:52:30 +00:00
|
|
|
}
|
2007-05-25 03:38:40 +00:00
|
|
|
}
|
2007-05-28 16:06:12 +00:00
|
|
|
} else {
|
|
|
|
$this->out("\nNo result set found");
|
2007-03-27 05:10:42 +00:00
|
|
|
}
|
2007-05-28 16:06:12 +00:00
|
|
|
} else {
|
|
|
|
$this->out("$modelToCheck is not a valid model");
|
2007-03-27 05:10:42 +00:00
|
|
|
}
|
2007-05-28 16:06:12 +00:00
|
|
|
|
2007-05-29 21:07:39 +00:00
|
|
|
break;
|
|
|
|
case (strpos($command, '->save') > 0):
|
|
|
|
// Validate the model we're trying to save here
|
|
|
|
$command = strip_tags($command);
|
|
|
|
$command = str_replace($this->badCommandChars, "", $command);
|
|
|
|
list($modelToSave, $tmp) = explode("->", $command);
|
2007-06-21 14:38:46 +00:00
|
|
|
|
2007-05-29 21:07:39 +00:00
|
|
|
if ($this->isValidModel($modelToSave)) {
|
|
|
|
// Extract the array of data we are trying to build
|
|
|
|
list($foo, $data) = explode("->save", $command);
|
|
|
|
$badChars = array("(", ")");
|
|
|
|
$data = str_replace($badChars, "", $data);
|
|
|
|
$saveCommand = "\$this->{$modelToSave}->save(array('{$modelToSave}' => array({$data})));";
|
|
|
|
@eval($saveCommand);
|
2007-05-29 21:11:34 +00:00
|
|
|
$this->out('Saved record for ' . $modelToSave);
|
2007-05-29 21:07:39 +00:00
|
|
|
}
|
|
|
|
|
2007-05-30 01:49:00 +00:00
|
|
|
break;
|
|
|
|
case (preg_match("/^(\w+) columns/", $command, $tmp) == true):
|
|
|
|
$modelToCheck = strip_tags(str_replace($this->badCommandChars, "", $tmp[1]));
|
2007-06-21 14:38:46 +00:00
|
|
|
|
2007-05-30 01:49:00 +00:00
|
|
|
if ($this->isValidModel($modelToCheck)) {
|
|
|
|
// Get the column info for this model
|
|
|
|
$fieldsCommand = "\$data = \$this->{$modelToCheck}->getColumnTypes();";
|
|
|
|
@eval($fieldsCommand);
|
|
|
|
|
|
|
|
if (is_array($data)) {
|
|
|
|
foreach ($data as $field => $type) {
|
|
|
|
$this->out("\t{$field}: {$type}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->out("Please verify that you selected a valid model");
|
|
|
|
}
|
|
|
|
break;
|
2007-05-28 16:06:12 +00:00
|
|
|
default:
|
|
|
|
$this->out("Invalid command\n");
|
2007-05-24 03:03:15 +00:00
|
|
|
break;
|
2007-03-27 05:10:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-09 23:15:03 +00:00
|
|
|
|
2007-05-28 18:08:52 +00:00
|
|
|
function isValidModel($modelToCheck)
|
2007-05-28 16:06:12 +00:00
|
|
|
{
|
|
|
|
if (in_array($modelToCheck, $this->models)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2007-03-27 05:10:42 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-28 16:06:12 +00:00
|
|
|
}
|
2007-06-21 14:38:46 +00:00
|
|
|
?>
|