From edcc8be2078d4327a467a6448a31725ac0e40d6b Mon Sep 17 00:00:00 2001 From: romanb Date: Sun, 24 Feb 2008 16:54:02 +0000 Subject: [PATCH] some smaller refactorings. started to replace the term 'template' with 'behavior'. --- lib/Doctrine/ClassMetadata.php | 100 +++++++++++++++++++++---------- lib/Doctrine/Collection.php | 5 +- lib/Doctrine/Data/Import.php | 2 +- lib/Doctrine/Mapper.php | 16 ----- lib/Doctrine/Record.php | 19 +++--- lib/Doctrine/Relation/Parser.php | 24 +------- 6 files changed, 85 insertions(+), 81 deletions(-) diff --git a/lib/Doctrine/ClassMetadata.php b/lib/Doctrine/ClassMetadata.php index 913c1b874..564bebcb6 100644 --- a/lib/Doctrine/ClassMetadata.php +++ b/lib/Doctrine/ClassMetadata.php @@ -78,16 +78,16 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab protected $_inheritanceType = Doctrine::INHERITANCETYPE_TABLE_PER_CLASS; /** - * An array containing all templates attached to the class. + * An array containing all behaviors attached to the class. * * @see Doctrine_Template * @var array $_templates * @todo Unify under 'Behaviors'. */ - protected $_templates = array(); + protected $_behaviors = array(); /** - * An array containing all generators attached to this class. + * An array containing all behavior generators attached to the class. * * @see Doctrine_Record_Generator * @var array $_generators @@ -242,7 +242,13 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab 'collate' => null, 'indexes' => array(), 'checks' => array() - ); + ); + + /** + * @var array $_invokedMethods method invoker cache + */ + protected $_invokedMethods = array(); + /** * Constructs a new metadata instance. @@ -373,6 +379,17 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab return $this->_tableOptions[$name]; } + + public function getBehaviorForMethod($method) + { + return (isset($this->_invokedMethods[$method])) ? + $this->_invokedMethods[$method] : false; + } + + public function addBehaviorMethod($method, $behavior) + { + $this->_invokedMethods[$method] = $class; + } /** * getOption @@ -590,6 +607,8 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab /** * Gets the names of all validators that are applied on a field. * + * @param string The field name. + * @return array The names of all validators that are applied on the specified field. */ public function getFieldValidators($fieldName) { @@ -599,10 +618,9 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab } /** - * hasDefaultValues - * returns true if this class has default values, otherwise false + * Checks whether the class mapped class has a default value on any field. * - * @return boolean + * @return boolean TRUE if the entity has a default value on any field, otherwise false. */ public function hasDefaultValues() { @@ -630,6 +648,8 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab } /** + * Gets the identifier (primary key) field(s) of the mapped class. + * * @return mixed */ public function getIdentifier() @@ -643,6 +663,10 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab } /** + * Gets the type of the identifier (primary key) used by the mapped class. The type + * can be either "Doctrine::IDENTIFIER_NATURAL", "Doctrine::IDENTIFIER_AUTOINCREMENT", + * "Doctrine::IDENTIFIER_SEQUENCE" or "Doctrine::IDENTIFIER_COMPOSITE". + * * @return integer */ public function getIdentifierType() @@ -650,6 +674,9 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab return $this->_identifierType; } + /** + * Sets the identifier type used by the mapped class. + */ public function setIdentifierType($type) { $this->_identifierType = $type; @@ -658,12 +685,18 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab /** * hasColumn * @return boolean + * @deprecated */ public function hasColumn($columnName) { return isset($this->_mappedColumns[$columnName]); } + public function hasMappedColumn($columnName) + { + return isset($this->_mappedColumns[$columnName]); + } + /** * hasField * @return boolean @@ -864,6 +897,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab * getDefinitionOf * * @return mixed array on success, false on failure + * @deprecated */ public function getDefinitionOf($fieldName) { @@ -871,17 +905,30 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab return $this->getColumnDefinition($columnName); } + + public function getMappingForField($fieldName) + { + $columnName = $this->getColumnName($fieldName); + + return $this->getColumnDefinition($columnName); + } /** * getTypeOf * * @return mixed string on success, false on failure + * @deprecated */ public function getTypeOf($fieldName) { return $this->getTypeOfColumn($this->getColumnName($fieldName)); } + public function getTypeOfField($fieldName) + { + return $this->getTypeOfColumn($this->getColumnName($fieldName)); + } + /** * getTypeOfColumn * @@ -994,15 +1041,15 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab } /** - * getTemplates - * returns all templates attached to this table + * getBehaviors + * returns all behaviors attached to the class. * * @return array an array containing all templates * @todo Unify under 'Behaviors' */ - public function getTemplates() + public function getBehaviors() { - return $this->_templates; + return $this->_behaviors; } /** @@ -1308,29 +1355,29 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab * @return void * @todo Unify under 'Behaviors'. */ - public function getTemplate($template) + public function getBehavior($behaviorName) { - if ( ! isset($this->_templates[$template])) { - throw new Doctrine_Table_Exception('Template ' . $template . ' not loaded'); + if ( ! isset($this->_behaviors[$behaviorName])) { + throw new Doctrine_Table_Exception('Template ' . $behaviorName . ' not loaded'); } - return $this->_templates[$template]; + return $this->_behaviors[$behaviorName]; } /** * @todo Unify under 'Behaviors'. */ - public function hasTemplate($template) + public function hasBehavior($behaviorName) { - return isset($this->_templates[$template]); + return isset($this->_behaviors[$behaviorName]); } /** * @todo Unify under 'Behaviors'. */ - public function addTemplate($template, Doctrine_Template $impl) + public function addBehavior($behaviorName, Doctrine_Template $impl) { - $this->_templates[$template] = $impl; + $this->_behaviors[$behaviorName] = $impl; return $this; } @@ -1599,7 +1646,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab $className = get_class($tpl); - $this->addTemplate($className, $tpl); + $this->addBehavior($className, $tpl); $tpl->setTable($this); $tpl->setUp(); @@ -1639,17 +1686,6 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab } } - /** - * Mixes a predefined behaviour into the class. - * - * @param string|object The name of the behavior or the behavior object. - * @todo Implementation. - */ - public function addBehavior($behavior) - { - // ... - } - /** * Registers a custom mapper for the entity class. * @@ -1727,6 +1763,8 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab return $this; } + + /** * */ diff --git a/lib/Doctrine/Collection.php b/lib/Doctrine/Collection.php index 99236e642..93b993eaa 100644 --- a/lib/Doctrine/Collection.php +++ b/lib/Doctrine/Collection.php @@ -485,8 +485,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator */ public function add($record, $key = null) { - if( ! $record instanceOf Doctrine_Record) { - throw new Doctrine_Record_Exception('Value variable in set is not an instance of Doctrine_Record'); + if ( ! $record instanceof Doctrine_Record) { + throw new Doctrine_Record_Exception('Value variable in set is not an instance of Doctrine_Record.'); } if (isset($this->referenceField)) { @@ -527,6 +527,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator } else { $this->data[] = $record; } + return true; } diff --git a/lib/Doctrine/Data/Import.php b/lib/Doctrine/Data/Import.php index 219f5b2ca..f49eb00d8 100644 --- a/lib/Doctrine/Data/Import.php +++ b/lib/Doctrine/Data/Import.php @@ -178,7 +178,7 @@ class Doctrine_Data_Import extends Doctrine_Data // This is simple here to get the templates present for this model // better way? $obj = new $className(null, true); - $templates = array_keys($obj->getTable()->getTemplates()); + $templates = array_keys($obj->getTable()->getBehaviors()); if (in_array('Doctrine_Template_NestedSet', $templates)) { $nestedSets[$className][] = $data; diff --git a/lib/Doctrine/Mapper.php b/lib/Doctrine/Mapper.php index 95c2fcabb..d94c4061c 100644 --- a/lib/Doctrine/Mapper.php +++ b/lib/Doctrine/Mapper.php @@ -68,11 +68,6 @@ class Doctrine_Mapper extends Doctrine_Configurable implements Countable */ protected $_repository; - /** - * @var array $_invokedMethods method invoker cache - */ - protected $_invokedMethods = array(); - /** * Constructs a new mapper. @@ -95,17 +90,6 @@ class Doctrine_Mapper extends Doctrine_Configurable implements Countable } } - public function getMethodOwner($method) - { - return (isset($this->_invokedMethods[$method])) ? - $this->_invokedMethods[$method] : false; - } - - public function setMethodOwner($method, $class) - { - $this->_invokedMethods[$method] = $class; - } - /** * export * exports this table to database based on column and option definitions diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index a88b58955..3fa8e78cf 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -1682,7 +1682,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count public function revert($version) { $data = $this->_table - ->getTemplate('Doctrine_Template_Versionable') + ->getBehavior('Doctrine_Template_Versionable') ->getAuditLog() ->getVersion($this, $version); @@ -1841,17 +1841,16 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count */ public function __call($method, $args) { - if (($template = $this->_mapper->getMethodOwner($method)) !== false) { - $template->setInvoker($this); - return call_user_func_array(array($template, $method), $args); + if (($behavior = $this->_table->getBehaviorForMethod($method)) !== false) { + $behavior->setInvoker($this); + return call_user_func_array(array($behavior, $method), $args); } - foreach ($this->_mapper->getTable()->getTemplates() as $template) { - if (method_exists($template, $method)) { - $template->setInvoker($this); - $this->_mapper->setMethodOwner($method, $template); - - return call_user_func_array(array($template, $method), $args); + foreach ($this->_table->getBehaviors() as $behavior) { + if (method_exists($behavior, $method)) { + $behavior->setInvoker($this); + $this->_table->addBehaviorMethod($method, $behavior); + return call_user_func_array(array($behavior, $method), $args); } } diff --git a/lib/Doctrine/Relation/Parser.php b/lib/Doctrine/Relation/Parser.php index ae9714269..3ccbdca95 100644 --- a/lib/Doctrine/Relation/Parser.php +++ b/lib/Doctrine/Relation/Parser.php @@ -136,12 +136,12 @@ class Doctrine_Relation_Parser $this->getRelations(); return $this->getRelation($alias, false); } else { - try { + /*try { throw new Exception(); } catch (Exception $e) { //echo "" . "
"; ///echo $e->getTraceAsString() . "


"; - } + }*/ throw new Doctrine_Relation_Exception("Unknown relation '$alias'."); } } @@ -171,19 +171,7 @@ class Doctrine_Relation_Parser $def = $this->completeAssocDefinition($def); $localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getClassName())); - // if the two many-many related components share the same table, we need - // custom relation names to distinguish the relations. - /*if ($this->_table->getInheritanceType() == Doctrine::INHERITANCETYPE_SINGLE_TABLE && - in_array($def['class'], $this->_table->getOption('subclasses'))) { - if ( ! isset($def['refRelationName']) || ! isset($def['refReverseRelationName'])) { - throw new Doctrine_Relation_Exception("Incomplete relation. Many-to-many relations between " - . "classes that share the same table (single table inheritance) need to specify " - . "a 'refRelationName' and a 'refReverseRelationName' to distinguish relations."); - } - $relationName = $def['refRelationName']; - } else {*/ - $relationName = $def['refClass']; - //} + $relationName = $def['refClass']; if ( ! isset($this->_pending[$relationName]) && ! isset($this->_relations[$relationName])) { $this->_completeManyToManyRelation($def); @@ -222,12 +210,7 @@ class Doctrine_Relation_Parser $identifierColumnNames = $this->_table->getIdentifierColumnNames(); $idColumnName = array_pop($identifierColumnNames); - // if the two many-many related components shared the same table, we need a relation name - // to distinguish the relations. $relationName = $def['refClass']; - /*if (isset($def['refRelationName'])) { - $relationName .= ' as ' . $def['refRelationName']; - }*/ // add a relation pointing from the intermediary table to the table of this parser $parser = $def['refTable']->getRelationParser(); @@ -416,7 +399,6 @@ class Doctrine_Relation_Parser { $conn = $this->_table->getConnection(); $def['table'] = $this->getImpl($def, 'class'); - //$def['class'] = $def['table']->getComponentName(); $def['localTable'] = $this->_table; $foreignClasses = array_merge($def['table']->getOption('parents'), array($def['class']));