Added in dynamic unbinding, verification of existing models, verification of existing associations, added in help text and reorganized code in swich-case block now that I figured out you could put preg_matches in a case statement :)

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5215 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
chartjes 2007-05-28 16:06:12 +00:00
parent 700368710f
commit 2680afb990

View file

@ -32,6 +32,7 @@
*/ */
class ConsoleShell extends Shell { class ConsoleShell extends Shell {
var $associations = array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany'); var $associations = array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany');
var $badCommandChars = array('$', ';');
function initialize() { function initialize() {
$this->models = @loadModels(); $this->models = @loadModels();
@ -63,8 +64,12 @@ class ConsoleShell extends Shell {
$this->out('e.g. Foo->findAll()'); $this->out('e.g. Foo->findAll()');
$this->out(''); $this->out('');
$this->out('To dynamically set associations, you can do the following:'); $this->out('To dynamically set associations, you can do the following:');
$this->out("\tModelA <association> ModelB"); $this->out("\tModelA bind <association> ModelB");
$this->out("where the supported assocations are hasOne, hasMany, belongsTo, hasAndBelongsToMany"); $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");
break; break;
case 'quit': case 'quit':
case 'exit': case 'exit':
@ -77,54 +82,72 @@ class ConsoleShell extends Shell {
$this->out(" - {$model}"); $this->out(" - {$model}");
} }
break; break;
default: case (preg_match("/^(\w+) bind (\w+) (\w+)/", $command, $tmp) == true):
// Look to see if we're dynamically binding something foreach ($tmp as $data) {
$dynamicAssociation = false; $data = strip_tags($data);
$data = str_replace($this->badCommandChars, "", $data);
}
$modelA = $tmp[1];
$association = $tmp[2];
$modelB = $tmp[3];
foreach ($this->associations as $association) { if ($this->isValidModel($modelA) && $this->isValidModel($modelB) && in_array($association, $this->associations)) {
if (preg_match("/^(\w+) $association (\w+)/", $command, $this->models) == TRUE) { $this->{$modelA}->bindModel(array($association => array($modelB => array('className' => $modelB))), false);
$modelA = $this->models[1]; $this->out("Created $association association between $modelA and $modelB");
$modelB = $this->models[2]; } else {
$dynamicAssociation = true; $this->out("Please verify you are using valid models and association types");
$this->{$modelA}->bindModel( }
array("$association" => array( break;
"$modelB" => array( case (preg_match("/^(\w+) unbind (\w+) (\w+)/", $command, $tmp) == true):
'className' => $modelB))), false); foreach ($tmp as $data) {
print "Added association $command\n"; $data = strip_tags($data);
break; $data = str_replace($this->badCommandChars, "", $data);
}
$modelA = $tmp[1];
$association = $tmp[2];
$modelB = $tmp[3];
// 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;
} }
} }
if ($dynamicAssociation == false) { if ($this->isValidModel($modelA) && $this->isValidModel($modelB) && in_array($association, $this->associations) && $validCurrentAssociation) {
// let's look for a find statment $this->{$modelA}->unbindModel(array($association => array($modelB)));
if (strpos($command, "->find") > 0) { $this->out("Removed $association association between $modelA and $modelB");
$consoleCommand = '$data = $this->' . $command . ";"; } else {
eval($consoleCommand); $this->out("Please verify you are using valid models, valid current association, and valid association types");
}
if (is_array($data)) { break;
foreach ($data as $idx => $results) { case (strpos($command, "->find") > 0):
if (is_numeric($idx)) { // findAll() output // Remove any bad info
foreach ($results as $modelName => $result) { $command = strip_tags($command);
$this->out("$modelName"); $command = str_replace($this->badCommandChars, "", $command);
$command = str_replace(";", "", $command);
foreach ($result as $field => $value) { // Do we have a valid model?
if (is_array($value)) { list($modelToCheck, $tmp) = explode('->', $command);
foreach($value as $field2 => $value2) {
$this->out("\t$field2: $value2"); if ($this->isValidModel($modelToCheck)) {
} $findCommand = "\$data = \$this->$command;";
@eval($findCommand);
$this->out(""); if (is_array($data)) {
} else { foreach ($data as $idx => $results) {
$this->out("\t$field: $value"); if (is_numeric($idx)) { // findAll() output
} foreach ($results as $modelName => $result) {
} $this->out("$modelName");
}
} else { // find() output
$this->out($idx);
foreach($results as $field => $value) { foreach ($result as $field => $value) {
if (is_array($value)) { if (is_array($value)) {
foreach ($value as $field2 => $value2) { foreach($value as $field2 => $value2) {
$this->out("\t$field2: $value2"); $this->out("\t$field2: $value2");
} }
@ -134,32 +157,47 @@ class ConsoleShell extends Shell {
} }
} }
} }
} else { // find() output
$this->out($idx);
foreach($results as $field => $value) {
if (is_array($value)) {
foreach ($value as $field2 => $value2) {
$this->out("\t$field2: $value2");
}
$this->out("");
} else {
$this->out("\t$field: $value");
}
}
} }
} else {
$this->out("\nNo result set found");
} }
} else {
$this->out("\nNo result set found");
} }
} else {
$this->out("$modelToCheck is not a valid model");
} }
break;
default:
$this->out("Invalid command\n");
break; break;
} }
} }
} }
}
function fatal_error_handler($buffer) {
if(ereg("(error</b>:)(.+)(<br)", $buffer, $regs) ) {
$err = preg_replace("/<.*?>/", "", $regs[2]);
error_log($err);
return "ERROR CAUGHT check log file";
}
return $buffer;
}
function handle_error ($errno, $errstr, $errfile, $errline) { protected function isValidModel($modelToCheck)
error_log("$errstr in $errfile on line $errline"); {
if($errno == FATAL || $errno == ERROR){ if (in_array($modelToCheck, $this->models)) {
ob_end_flush(); return true;
echo "ERROR CAUGHT check log file"; } else {
exit(0); return false;
} }
} }
}
?> ?>