Adding __()

Refactoring duplicate code, and pulling out separate methods.
This commit is contained in:
mark_story 2009-05-05 00:43:29 -04:00
parent bb2f6b2ef5
commit 37d81cb92d

View file

@ -59,7 +59,7 @@ class ModelTask extends Shell {
* @var array * @var array
* @access public * @access public
*/ */
var $tasks = array('DbConfig', 'Fixture'); var $tasks = array('DbConfig', 'Fixture', 'Test');
/** /**
* Holds tables found on connection. * Holds tables found on connection.
* *
@ -106,7 +106,6 @@ class ModelTask extends Shell {
if (App::import('Model', $modelClass)) { if (App::import('Model', $modelClass)) {
$object = new $modelClass(); $object = new $modelClass();
$modelExists = true;
} else { } else {
App::import('Model'); App::import('Model');
$object = new Model(array('name' => $modelClass, 'ds' => $ds)); $object = new Model(array('name' => $modelClass, 'ds' => $ds));
@ -125,7 +124,6 @@ class ModelTask extends Shell {
$this->hr(); $this->hr();
$this->interactive = true; $this->interactive = true;
$useTable = null;
$primaryKey = 'id'; $primaryKey = 'id';
$validate = $associations = array(); $validate = $associations = array();
@ -137,29 +135,24 @@ class ModelTask extends Shell {
$db =& ConnectionManager::getDataSource($this->connection); $db =& ConnectionManager::getDataSource($this->connection);
$fullTableName = $db->fullTableName($useTable); $fullTableName = $db->fullTableName($useTable);
$wannaDoValidation = $this->in(__('Would you like to supply validation criteria for the fields in your model?', true), array('y','n'), 'y');
if (in_array($useTable, $this->__tables)) { if (in_array($useTable, $this->__tables)) {
App::import('Model'); App::import('Model');
$tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $this->connection)); $tempModel = new Model(array('name' => $currentModelName, 'table' => $useTable, 'ds' => $this->connection));
$fields = $tempModel->schema(); $fields = $tempModel->schema();
if (!array_key_exists('id', $fields)) { if (!array_key_exists('id', $fields)) {
foreach ($fields as $name => $field) { $primaryKey = $this->findPrimaryKey($fields);
if (isset($field['key']) && $field['key'] == 'primary') {
break;
}
}
$primaryKey = $this->in(__('What is the primaryKey?', true), null, $name);
} }
} }
if (array_search($useTable, $this->__tables) !== false && (low($wannaDoValidation) == 'y' || low($wannaDoValidation) == 'yes')) { $prompt = __('Would you like to supply validation criteria for the fields in your model?', true);
$wannaDoValidation = $this->in($prompt, array('y','n'), 'y');
if (array_search($useTable, $this->__tables) !== false && strtolower($wannaDoValidation) == 'y') {
$validate = $this->doValidation($tempModel); $validate = $this->doValidation($tempModel);
} }
$wannaDoAssoc = $this->in(__('Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)?', true), array('y','n'), 'y'); $prompt = __('Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)?', true);
if ((low($wannaDoAssoc) == 'y' || low($wannaDoAssoc) == 'yes')) { $wannaDoAssoc = $this->in($prompt, array('y','n'), 'y');
if (strtolower($wannaDoAssoc) == 'y') {
$associations = $this->doAssociations($tempModel); $associations = $this->doAssociations($tempModel);
} }
@ -170,48 +163,29 @@ class ModelTask extends Shell {
$this->out("Name: " . $currentModelName); $this->out("Name: " . $currentModelName);
if ($this->connection !== 'default') { if ($this->connection !== 'default') {
$this->out("DB Config: " . $useDbConfig); $this->out(sprintf(__("DB Config: %s", true), $useDbConfig));
} }
if ($fullTableName !== Inflector::tableize($currentModelName)) { if ($fullTableName !== Inflector::tableize($currentModelName)) {
$this->out("DB Table: " . $fullTableName); $this->out(sprintf(__("DB Table: %s", true), $fullTableName));
} }
if ($primaryKey != 'id') { if ($primaryKey != 'id') {
$this->out("Primary Key: " . $primaryKey); $this->out(sprintf(__("Primary Key: %s", true), $primaryKey));
} }
if (!empty($validate)) { if (!empty($validate)) {
$this->out("Validation: " . print_r($validate, true)); $this->out(sprintf(__("Validation: %s", true), print_r($validate, true)));
} }
if (!empty($associations)) { if (!empty($associations)) {
$this->out("Associations:"); $this->out(__("Associations:", true));
$assocKeys = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
if (!empty($associations['belongsTo'])) { foreach ($assocKeys as $assocKey) {
for ($i = 0; $i < count($associations['belongsTo']); $i++) { $this->_printAssociation($currentModelName, $assocKey, $associations);
$this->out(" $currentModelName belongsTo {$associations['belongsTo'][$i]['alias']}");
}
}
if (!empty($associations['hasOne'])) {
for ($i = 0; $i < count($associations['hasOne']); $i++) {
$this->out(" $currentModelName hasOne {$associations['hasOne'][$i]['alias']}");
}
}
if (!empty($associations['hasMany'])) {
for ($i = 0; $i < count($associations['hasMany']); $i++) {
$this->out(" $currentModelName hasMany {$associations['hasMany'][$i]['alias']}");
}
}
if (!empty($associations['hasAndBelongsToMany'])) {
for ($i = 0; $i < count($associations['hasAndBelongsToMany']); $i++) {
$this->out(" $currentModelName hasAndBelongsToMany {$associations['hasAndBelongsToMany'][$i]['alias']}");
}
} }
} }
$this->hr(); $this->hr();
$looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y'); $looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y');
if (low($looksGood) == 'y' || low($looksGood) == 'yes') { if (strtolower($looksGood) == 'y') {
if ($this->bake($currentModelName, $associations, $validate, $primaryKey, $useTable, $this->connection)) { if ($this->bake($currentModelName, $associations, $validate, $primaryKey, $useTable, $this->connection)) {
if ($this->_checkUnitTest()) { if ($this->_checkUnitTest()) {
$this->bakeTest($currentModelName, $useTable, $associations); $this->bakeTest($currentModelName, $useTable, $associations);
@ -221,6 +195,37 @@ class ModelTask extends Shell {
return false; return false;
} }
} }
/**
* Print out all the associations of a particular type
*
* @param string $modelName Name of the model relations belong to.
* @param string $type Name of association you want to see. i.e. 'belongsTo'
* @param string $associations Collection of associations.
* @access public
* @return void
**/
function _printAssociation($modelName, $type, $associations) {
if (!empty($associations[$type])) {
for ($i = 0; $i < count($associations[$type]); $i++) {
$out = "\t" . $modelName . ' ' . $type . ' ' . $associations[$type][$i]['alias'];
$this->out($out);
}
}
}
/**
* Finds a primary Key in a list of fields.
*
* @param array $fields Array of fields that might have a primary key.
* @return string Name of field that is a primary key.
**/
function findPrimaryKey($fields) {
foreach ($fields as $name => $field) {
if (isset($field['key']) && $field['key'] == 'primary') {
break;
}
}
return $this->in(__('What is the primaryKey?', true), null, $name);
}
/** /**
* Handles associations * Handles associations
* *
@ -229,7 +234,7 @@ class ModelTask extends Shell {
* @return array $validate * @return array $validate
* @access public * @access public
*/ */
function doValidation(&$model, $interactive = true) { function doValidation(&$model) {
if (!is_object($model)) { if (!is_object($model)) {
return false; return false;
} }
@ -238,9 +243,7 @@ class ModelTask extends Shell {
if (empty($fields)) { if (empty($fields)) {
return false; return false;
} }
$validate = $options = array();
$validate = array();
$options = array();
if (class_exists('Validation')) { if (class_exists('Validation')) {
$parent = get_class_methods(get_parent_class('Validation')); $parent = get_class_methods(get_parent_class('Validation'));
@ -308,11 +311,10 @@ class ModelTask extends Shell {
* Handles associations * Handles associations
* *
* @param object $model * @param object $model
* @param boolean $interactive
* @return array $assocaitons * @return array $assocaitons
* @access public * @access public
*/ */
function doAssociations(&$model, $interactive = true) { function doAssociations(&$model) {
if (!is_object($model)) { if (!is_object($model)) {
return false; return false;
} }
@ -327,7 +329,9 @@ class ModelTask extends Shell {
$primaryKey = $model->primaryKey; $primaryKey = $model->primaryKey;
$foreignKey = $this->_modelKey($model->name); $foreignKey = $this->_modelKey($model->name);
$associations = array('belongsTo' => array(), 'hasMany' => array(), 'hasOne'=> array(), 'hasAndBelongsToMany' => array()); $associations = array(
'belongsTo' => array(), 'hasMany' => array(), 'hasOne'=> array(), 'hasAndBelongsToMany' => array()
);
$possibleKeys = array(); $possibleKeys = array();
//Look for belongsTo //Look for belongsTo
@ -392,11 +396,11 @@ class ModelTask extends Shell {
} }
} }
if ($interactive !== true) { if ($this->interactive !== true) {
unset($associations['hasOne']); unset($associations['hasOne']);
} }
if ($interactive === true) { if ($this->interactive === true) {
$this->hr(); $this->hr();
if (empty($associations)) { if (empty($associations)) {
$this->out(__('None found.', true)); $this->out(__('None found.', true));
@ -526,7 +530,7 @@ class ModelTask extends Shell {
if (is_object($name)) { if (is_object($name)) {
if (!is_array($associations)) { if (!is_array($associations)) {
$associations = $this->doAssociations($name, $associations); $associations = $this->doAssociations($name, $associations);
$validate = $this->doValidation($name, $associations); $validate = $this->doValidation($name);
} }
$primaryKey = $name->primaryKey; $primaryKey = $name->primaryKey;
$useTable = $name->table; $useTable = $name->table;