Example #1
0
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass($className, ClassMetadata $class)
 {
     foreach ($this->drivers as $namespace => $driver) {
         if (strpos($className, $namespace) === 0) {
             $driver->loadMetadataForClass($className, $class);
             return;
         }
     }
     throw MappingException::classIsNotAValidDocument($className);
 }
Example #2
0
 /**
  * Gets the mapping of a (regular) field that holds some data but not a
  * reference to another object.
  *
  * @param string $fieldName The field name.
  *
  * @return array The field mapping.
  *
  * @throws MappingException
  */
 public function getFieldMapping($fieldName)
 {
     if (!$this->hasField($fieldName)) {
         throw MappingException::fieldNotFound($this->name, $fieldName);
     }
     return $this->mappings[$fieldName];
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass($className, ClassMetadata $metadata)
 {
     /** @var $metadata \Doctrine\ODM\PHPCR\Mapping\ClassMetadata */
     $reflClass = $metadata->getReflectionClass();
     $documentAnnots = array();
     foreach ($this->reader->getClassAnnotations($reflClass) as $annot) {
         foreach ($this->entityAnnotationClasses as $annotClass => $i) {
             if ($annot instanceof $annotClass) {
                 $documentAnnots[$i] = $annot;
             }
         }
     }
     if (!$documentAnnots) {
         throw MappingException::classIsNotAValidDocument($className);
     }
     // find the winning document annotation
     ksort($documentAnnots);
     $documentAnnot = reset($documentAnnots);
     if ($documentAnnot instanceof ODM\MappedSuperclass) {
         $metadata->isMappedSuperclass = true;
     }
     if (null !== $documentAnnot->referenceable) {
         $metadata->setReferenceable($documentAnnot->referenceable);
     }
     if (null !== $documentAnnot->versionable) {
         $metadata->setVersioned($documentAnnot->versionable);
     }
     if (null !== $documentAnnot->mixins) {
         $metadata->setMixins($documentAnnot->mixins);
     }
     if (null !== $documentAnnot->inheritMixins) {
         $metadata->setInheritMixins($documentAnnot->inheritMixins);
     }
     if (null !== $documentAnnot->nodeType) {
         $metadata->setNodeType($documentAnnot->nodeType);
     }
     if (null !== $documentAnnot->repositoryClass) {
         $metadata->setCustomRepositoryClassName($documentAnnot->repositoryClass);
     }
     if (null !== $documentAnnot->translator) {
         $metadata->setTranslator($documentAnnot->translator);
     }
     foreach ($reflClass->getProperties() as $property) {
         if ($metadata->isInheritedField($property->name) && $metadata->name !== $property->getDeclaringClass()->getName()) {
             continue;
         }
         $mapping = array();
         $mapping['fieldName'] = $property->getName();
         foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) {
             if ($fieldAnnot instanceof ODM\Property) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapField($mapping);
             } elseif ($fieldAnnot instanceof ODM\Id) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapId($mapping);
             } elseif ($fieldAnnot instanceof ODM\Node) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapNode($mapping);
             } elseif ($fieldAnnot instanceof ODM\Nodename) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapNodename($mapping);
             } elseif ($fieldAnnot instanceof ODM\ParentDocument) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
                 $metadata->mapParentDocument($mapping);
             } elseif ($fieldAnnot instanceof ODM\Child) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
                 $metadata->mapChild($mapping);
             } elseif ($fieldAnnot instanceof ODM\Children) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
                 $metadata->mapChildren($mapping);
             } elseif ($fieldAnnot instanceof ODM\ReferenceOne) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
                 $metadata->mapManyToOne($mapping);
             } elseif ($fieldAnnot instanceof ODM\ReferenceMany) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
                 $metadata->mapManyToMany($mapping);
             } elseif ($fieldAnnot instanceof ODM\Referrers) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
                 $metadata->mapReferrers($mapping);
             } elseif ($fieldAnnot instanceof ODM\MixedReferrers) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapMixedReferrers($mapping);
             } elseif ($fieldAnnot instanceof ODM\Locale) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapLocale($mapping);
             } elseif ($fieldAnnot instanceof ODM\Depth) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapDepth($mapping);
             } elseif ($fieldAnnot instanceof ODM\VersionName) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapVersionName($mapping);
             } elseif ($fieldAnnot instanceof ODM\VersionCreated) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapVersionCreated($mapping);
             }
         }
     }
     foreach ($reflClass->getMethods() as $method) {
         if ($method->isPublic() && $method->getDeclaringClass()->getName() == $metadata->name) {
             foreach ($this->reader->getMethodAnnotations($method) as $annot) {
                 if ($annot instanceof ODM\PrePersist) {
                     $metadata->addLifecycleCallback($method->getName(), Event::prePersist);
                 } elseif ($annot instanceof ODM\PostPersist) {
                     $metadata->addLifecycleCallback($method->getName(), Event::postPersist);
                 } elseif ($annot instanceof ODM\PreUpdate) {
                     $metadata->addLifecycleCallback($method->getName(), Event::preUpdate);
                 } elseif ($annot instanceof ODM\PostUpdate) {
                     $metadata->addLifecycleCallback($method->getName(), Event::postUpdate);
                 } elseif ($annot instanceof ODM\PreRemove) {
                     $metadata->addLifecycleCallback($method->getName(), Event::preRemove);
                 } elseif ($annot instanceof ODM\PostRemove) {
                     $metadata->addLifecycleCallback($method->getName(), Event::postRemove);
                 } elseif ($annot instanceof ODM\PostLoad) {
                     $metadata->addLifecycleCallback($method->getName(), Event::postLoad);
                 }
             }
         }
     }
     $metadata->validateClassMapping();
 }
 /**
  * {@inheritdoc}
  *
  * @throws MappingException
  */
 public function loadMetadata($className)
 {
     if (class_exists($className)) {
         return parent::loadMetadata($className);
     }
     throw MappingException::classNotFound($className);
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass($className, ClassMetadata $metadata)
 {
     $reflClass = $metadata->getReflectionClass();
     $documentAnnots = array();
     foreach ($this->reader->getClassAnnotations($reflClass) as $annot) {
         foreach ($this->entityAnnotationClasses as $annotClass => $i) {
             if ($annot instanceof $annotClass) {
                 $documentAnnots[$i] = $annot;
             }
         }
     }
     if (!$documentAnnots) {
         throw MappingException::classIsNotAValidDocument($className);
     }
     // find the winning document annotation
     ksort($documentAnnots);
     $documentAnnot = reset($documentAnnots);
     if (isset($documentAnnot->versionable) && $documentAnnot->versionable) {
         $metadata->setVersioned($documentAnnot->versionable);
     }
     $metadata->setNodeType($documentAnnot->nodeType);
     if (isset($documentAnnot->referenceable) && $documentAnnot->referenceable) {
         $metadata->setReferenceable(true);
     }
     if ($documentAnnot->repositoryClass) {
         $metadata->setCustomRepositoryClassName($documentAnnot->repositoryClass);
     }
     if ($documentAnnot->translator) {
         $metadata->setTranslator($documentAnnot->translator);
     }
     foreach ($reflClass->getProperties() as $property) {
         if ($metadata->isMappedSuperclass && !$property->isPrivate() || $metadata->isInheritedField($property->name) && $metadata->name !== $property->getDeclaringClass()->getName()) {
             continue;
         }
         $mapping = array();
         $mapping['fieldName'] = $property->getName();
         foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) {
             if ($fieldAnnot instanceof ODM\Property) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapField($mapping);
             } elseif ($fieldAnnot instanceof ODM\Id) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapId($mapping);
             } elseif ($fieldAnnot instanceof ODM\Node) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapNode($mapping);
             } elseif ($fieldAnnot instanceof ODM\Nodename) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapNodename($mapping);
             } elseif ($fieldAnnot instanceof ODM\ParentDocument) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapParentDocument($mapping);
             } elseif ($fieldAnnot instanceof ODM\Child) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapChild($mapping);
             } elseif ($fieldAnnot instanceof ODM\Children) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapChildren($mapping);
             } elseif ($fieldAnnot instanceof ODM\ReferenceOne) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapManyToOne($mapping);
             } elseif ($fieldAnnot instanceof ODM\ReferenceMany) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapManyToMany($mapping);
             } elseif ($fieldAnnot instanceof ODM\Referrers) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapReferrers($mapping);
             } elseif ($fieldAnnot instanceof ODM\Locale) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapLocale($mapping);
             } elseif ($fieldAnnot instanceof ODM\VersionName) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapVersionName($mapping);
             } elseif ($fieldAnnot instanceof ODM\VersionCreated) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapVersionCreated($mapping);
             }
             if (!isset($mapping['name'])) {
                 $mapping['name'] = $property->getName();
             }
         }
     }
     foreach ($reflClass->getMethods() as $method) {
         if ($method->isPublic() && $method->getDeclaringClass()->getName() == $metadata->name) {
             foreach ($this->reader->getMethodAnnotations($method) as $annot) {
                 if ($annot instanceof ODM\PrePersist) {
                     $metadata->addLifecycleCallback($method->getName(), Event::prePersist);
                 } elseif ($annot instanceof ODM\PostPersist) {
                     $metadata->addLifecycleCallback($method->getName(), Event::postPersist);
                 } elseif ($annot instanceof ODM\PreUpdate) {
                     $metadata->addLifecycleCallback($method->getName(), Event::preUpdate);
                 } elseif ($annot instanceof ODM\PostUpdate) {
                     $metadata->addLifecycleCallback($method->getName(), Event::postUpdate);
                 } elseif ($annot instanceof ODM\PreRemove) {
                     $metadata->addLifecycleCallback($method->getName(), Event::preRemove);
                 } elseif ($annot instanceof ODM\PostRemove) {
                     $metadata->addLifecycleCallback($method->getName(), Event::postRemove);
                 } elseif ($annot instanceof ODM\PreLoad) {
                     $metadata->addLifecycleCallback($method->getName(), Event::preLoad);
                 } elseif ($annot instanceof ODM\PostLoad) {
                     $metadata->addLifecycleCallback($method->getName(), Event::postLoad);
                 }
             }
         }
     }
     // Check there is a @Locale annotation for translatable documents
     if (count($metadata->translatableFields)) {
         if (!isset($metadata->localeMapping)) {
             throw new MappingException("You must define a @Locale field for translatable document '{$className}'");
         }
     }
 }
Example #6
0
 /**
  * Gets the mapping of a field.
  *
  * @param string $fieldName  The field name.
  * @return array  The field mapping.
  */
 public function getFieldMapping($fieldName)
 {
     if (!isset($this->fieldMappings[$fieldName])) {
         throw MappingException::mappingNotFound($this->name, $fieldName);
     }
     return $this->fieldMappings[$fieldName];
 }
Example #7
0
 /**
  * @return array the association mapping with the field of this name
  *
  * @throws MappingException if the class has no mapping field with this name
  */
 public function getAssociation($fieldName)
 {
     if (!$this->hasAssociation($fieldName)) {
         throw MappingException::associationNotFound($this->name, $fieldName);
     }
     return $this->mappings[$fieldName];
 }