From 0f081d7c4525b51f577a753173f7ba9dd00350b6 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sun, 29 Jul 2012 20:47:31 -0300 Subject: [PATCH] support short class name --- .../ORM/Mapping/ClassMetadataInfo.php | 42 +++++++++---------- .../ORM/Mapping/Driver/AnnotationDriver.php | 11 ++--- .../Tests/Models/Company/CompanyContract.php | 2 +- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index db4acbb8b..642da6701 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1326,10 +1326,7 @@ class ClassMetadataInfo implements ClassMetadata $mapping['sourceEntity'] = $this->name; if (isset($mapping['targetEntity'])) { - if (strlen($this->namespace) > 0 && strpos($mapping['targetEntity'], '\\') === false) { - $mapping['targetEntity'] = $this->namespace . '\\' . $mapping['targetEntity']; - } - + $mapping['targetEntity'] = $this->fullyQualifiedClassName($mapping['targetEntity']); $mapping['targetEntity'] = ltrim($mapping['targetEntity'], '\\'); } @@ -1917,11 +1914,7 @@ class ClassMetadataInfo implements ClassMetadata public function setSubclasses(array $subclasses) { foreach ($subclasses as $subclass) { - if (strpos($subclass, '\\') === false && strlen($this->namespace)) { - $this->subClasses[] = $this->namespace . '\\' . $subclass; - } else { - $this->subClasses[] = $subclass; - } + $this->subClasses[] = $this->fullyQualifiedClassName($subclass); } } @@ -2274,11 +2267,9 @@ class ClassMetadataInfo implements ClassMetadata $queryMapping['isSelfClass'] = true; $queryMapping['resultClass'] = $this->name; - - } else if (strlen($this->namespace) > 0 && strpos($queryMapping['resultClass'], '\\') === false) { - $queryMapping['resultClass'] = $this->namespace . '\\' . $queryMapping['resultClass']; } + $queryMapping['resultClass'] = $this->fullyQualifiedClassName($queryMapping['resultClass']); $queryMapping['resultClass'] = ltrim($queryMapping['resultClass'], '\\'); } @@ -2317,10 +2308,9 @@ class ClassMetadataInfo implements ClassMetadata $entityResult['isSelfClass'] = true; $entityResult['entityClass'] = $this->name; - } else if (strlen($this->namespace) > 0 && strpos($entityResult['entityClass'], '\\') === false) { - $entityResult['entityClass'] = $this->namespace . '\\' . $entityResult['entityClass']; } + $entityResult['entityClass'] = $this->fullyQualifiedClassName($entityResult['entityClass']); $resultMapping['entities'][$key]['entityClass'] = ltrim($entityResult['entityClass'], '\\'); $resultMapping['entities'][$key]['isSelfClass'] = $entityResult['isSelfClass']; @@ -2432,11 +2422,7 @@ class ClassMetadataInfo implements ClassMetadata */ public function setCustomRepositoryClass($repositoryClassName) { - if ($repositoryClassName !== null && strpos($repositoryClassName, '\\') === false - && strlen($this->namespace) > 0) { - $repositoryClassName = $this->namespace . '\\' . $repositoryClassName; - } - $this->customRepositoryClassName = $repositoryClassName; + $this->customRepositoryClassName = $this->fullyQualifiedClassName($repositoryClassName); } /** @@ -2605,10 +2591,7 @@ class ClassMetadataInfo implements ClassMetadata */ public function addDiscriminatorMapClass($name, $className) { - if (strlen($this->namespace) > 0 && strpos($className, '\\') === false) { - $className = $this->namespace . '\\' . $className; - } - + $className = $this->fullyQualifiedClassName($className); $className = ltrim($className, '\\'); $this->discriminatorMap[$name] = $className; @@ -3030,4 +3013,17 @@ class ClassMetadataInfo implements ClassMetadata } return $relations; } + + /** + * @param string $className + * @return string + */ + public function fullyQualifiedClassName($className) + { + if ($className !== null && strpos($className, '\\') === false && strlen($this->namespace) > 0) { + return $this->namespace . '\\' . $className; + } + + return $className; + } } diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index f8abdd149..139795752 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -385,9 +385,8 @@ class AnnotationDriver extends AbstractAnnotationDriver // Check for JoinTable annotations if ($associationOverride->joinTable) { - $joinTable = null; $joinTableAnnot = $associationOverride->joinTable; - $joinTable = array( + $joinTable = array( 'name' => $joinTableAnnot->name, 'schema' => $joinTableAnnot->schema ); @@ -421,17 +420,16 @@ class AnnotationDriver extends AbstractAnnotationDriver $entityListenersAnnot = $classAnnotations['Doctrine\ORM\Mapping\EntityListeners']; foreach ($entityListenersAnnot->value as $listener) { + $listener = $metadata->fullyQualifiedClassName($listener); if ( ! class_exists($listener)) { throw new \InvalidArgumentException("Indefined class \"$listener\""); } $listener = new \ReflectionClass($listener); - foreach ($listener->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { foreach ($this->getMethodCallbacks($method) as $value) { - list($callback, $event) = $value; - $metadata->addEntityListener($event, $listener->name, $callback); + $metadata->addEntityListener($value[1], $listener->name, $value[0]); } } } @@ -447,8 +445,7 @@ class AnnotationDriver extends AbstractAnnotationDriver } foreach ($this->getMethodCallbacks($method) as $value) { - list($callback, $event) = $value; - $metadata->addLifecycleCallback($callback, $event); + $metadata->addLifecycleCallback($value[0], $value[1]); } } } diff --git a/tests/Doctrine/Tests/Models/Company/CompanyContract.php b/tests/Doctrine/Tests/Models/Company/CompanyContract.php index 71aa6f060..b218d8f86 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyContract.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyContract.php @@ -7,7 +7,7 @@ namespace Doctrine\Tests\Models\Company; * @Table(name="company_contracts") * @InheritanceType("SINGLE_TABLE") * @DiscriminatorColumn(name="discr", type="string") - * @EntityListeners({"Doctrine\Tests\Models\Company\ContractSubscriber"}) + * @EntityListeners({"ContractSubscriber"}) * @DiscriminatorMap({ * "fix" = "CompanyFixContract", * "flexible" = "CompanyFlexContract",