Ejemplo n.º 1
0
 /**
  * Overrides an already defined type to use a different implementation.
  *
  * @static
  * @param string $name
  * @param string $className
  * @throws MappingException
  */
 public static function overrideType($name, $className)
 {
     if (!isset(self::$typesMap[$name])) {
         throw MappingException::typeNotFound($name);
     }
     self::$typesMap[$name] = $className;
 }
Ejemplo n.º 2
0
 /**
  * Gets the mapping of a field.
  *
  * @param string $fieldName  The field name.
  *
  * @return array  The field mapping.
  *
  * @throws MappingException if the $fieldName is not found in the fieldMappings array
  *
  * @throws MappingException
  */
 public function getFieldMapping($fieldName)
 {
     if (!isset($this->fieldMappings[$fieldName])) {
         throw MappingException::mappingNotFound($this->name, $fieldName);
     }
     return $this->fieldMappings[$fieldName];
 }
Ejemplo n.º 3
0
 private function completeIdGeneratorMapping(ClassMetadataInfo $class)
 {
     $idGenOptions = $class->generatorOptions;
     switch ($class->generatorType) {
         case ClassMetadata::GENERATOR_TYPE_AUTO:
             $class->setIdGenerator(new \CosmoW\ODM\Riak\Id\AutoGenerator($class));
             break;
         case ClassMetadata::GENERATOR_TYPE_INCREMENT:
             $incrementGenerator = new \CosmoW\ODM\Riak\Id\IncrementGenerator($class);
             if (isset($idGenOptions['key'])) {
                 $incrementGenerator->setKey($idGenOptions['key']);
             }
             if (isset($idGenOptions['collection'])) {
                 $incrementGenerator->setCollection($idGenOptions['collection']);
             }
             $class->setIdGenerator($incrementGenerator);
             break;
         case ClassMetadata::GENERATOR_TYPE_UUID:
             $uuidGenerator = new \CosmoW\ODM\Riak\Id\UuidGenerator($class);
             isset($idGenOptions['salt']) && $uuidGenerator->setSalt($idGenOptions['salt']);
             $class->setIdGenerator($uuidGenerator);
             break;
         case ClassMetadata::GENERATOR_TYPE_ALNUM:
             $alnumGenerator = new \CosmoW\ODM\Riak\Id\AlnumGenerator($class);
             if (isset($idGenOptions['pad'])) {
                 $alnumGenerator->setPad($idGenOptions['pad']);
             }
             if (isset($idGenOptions['chars'])) {
                 $alnumGenerator->setChars($idGenOptions['chars']);
             } elseif (isset($idGenOptions['awkwardSafe'])) {
                 $alnumGenerator->setAwkwardSafeMode($idGenOptions['awkwardSafe']);
             }
             $class->setIdGenerator($alnumGenerator);
             break;
         case ClassMetadata::GENERATOR_TYPE_CUSTOM:
             if (empty($idGenOptions['class'])) {
                 throw MappingException::missingIdGeneratorClass($class->name);
             }
             $customGenerator = new $idGenOptions['class']();
             unset($idGenOptions['class']);
             if (!$customGenerator instanceof \CosmoW\ODM\Riak\Id\AbstractIdGenerator) {
                 throw MappingException::classIsNotAValidGenerator(get_class($customGenerator));
             }
             $methods = get_class_methods($customGenerator);
             foreach ($idGenOptions as $name => $value) {
                 $method = 'set' . ucfirst($name);
                 if (!in_array($method, $methods)) {
                     throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name);
                 }
                 $customGenerator->{$method}($value);
             }
             $class->setIdGenerator($customGenerator);
             break;
         case ClassMetadata::GENERATOR_TYPE_NONE:
             break;
         default:
             throw new MappingException("Unknown generator type: " . $class->generatorType);
     }
 }
Ejemplo n.º 4
0
 /**
  * Returns a DBRef array for the supplied document.
  *
  * @param mixed $document A document object
  * @param array $referenceMapping Mapping for the field that references the document
  *
  * @throws \InvalidArgumentException
  * @return array A DBRef array
  */
 public function createDBRef($document, array $referenceMapping = null)
 {
     if (!is_object($document)) {
         throw new \InvalidArgumentException('Cannot create a DBRef, the document is not an object');
     }
     $class = $this->getClassMetadata(get_class($document));
     $id = $this->unitOfWork->getDocumentIdentifier($document);
     if (!$id) {
         throw new \RuntimeException(sprintf('Cannot create a DBRef for class %s without an identifier. Have you forgotten to persist/merge the document first?', $class->name));
     }
     if (!empty($referenceMapping['simple'])) {
         if ($class->inheritanceType === ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_COLLECTION) {
             throw MappingException::simpleReferenceMustNotTargetDiscriminatedDocument($referenceMapping['targetDocument']);
         }
         return $class->getDatabaseIdentifierValue($id);
     }
     $dbRef = array('$ref' => $class->getCollection(), '$id' => $class->getDatabaseIdentifierValue($id), '$db' => $this->getDocumentDatabase($class->name)->getName());
     /* If the class has a discriminator (field and value), use it. A child
      * class that is not defined in the discriminator map may only have a
      * discriminator field and no value, so default to the full class name.
      */
     if (isset($class->discriminatorField)) {
         $dbRef[$class->discriminatorField] = isset($class->discriminatorValue) ? $class->discriminatorValue : $class->name;
     }
     /* Add a discriminator value if the referenced document is not mapped
      * explicitly to a targetDocument class.
      */
     if ($referenceMapping !== null && !isset($referenceMapping['targetDocument'])) {
         $discriminatorField = $referenceMapping['discriminatorField'];
         $discriminatorValue = isset($referenceMapping['discriminatorMap']) ? array_search($class->name, $referenceMapping['discriminatorMap']) : $class->name;
         /* If the discriminator value was not found in the map, use the full
          * class name. In the future, it may be preferable to throw an
          * exception here (perhaps based on some strictness option).
          *
          * @see PersistenceBuilder::prepareEmbeddedDocumentValue()
          */
         if ($discriminatorValue === false) {
             $discriminatorValue = $class->name;
         }
         $dbRef[$discriminatorField] = $discriminatorValue;
     }
     return $dbRef;
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass($className, ClassMetadata $class)
 {
     /** @var $class ClassMetadataInfo */
     $reflClass = $class->getReflectionClass();
     $classAnnotations = $this->reader->getClassAnnotations($reflClass);
     $documentAnnots = array();
     foreach ($classAnnotations as $annot) {
         $classAnnotations[get_class($annot)] = $annot;
         foreach ($this->entityAnnotationClasses as $annotClass => $i) {
             if ($annot instanceof $annotClass) {
                 $documentAnnots[$i] = $annot;
                 continue 2;
             }
         }
         // non-document class annotations
         if ($annot instanceof ODM\AbstractIndex) {
             $this->addIndex($class, $annot);
         }
         if ($annot instanceof ODM\Indexes) {
             foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) {
                 $this->addIndex($class, $index);
             }
         } elseif ($annot instanceof ODM\InheritanceType) {
             $class->setInheritanceType(constant('Doctrine\\ODM\\Riak\\Mapping\\ClassMetadata::INHERITANCE_TYPE_' . $annot->value));
         } elseif ($annot instanceof ODM\DiscriminatorField) {
             // $fieldName property is deprecated, but fall back for BC
             if (isset($annot->value)) {
                 $class->setDiscriminatorField($annot->value);
             } elseif (isset($annot->name)) {
                 $class->setDiscriminatorField($annot->name);
             } elseif (isset($annot->fieldName)) {
                 $class->setDiscriminatorField($annot->fieldName);
             }
         } elseif ($annot instanceof ODM\DiscriminatorMap) {
             $class->setDiscriminatorMap($annot->value);
         } elseif ($annot instanceof ODM\DiscriminatorValue) {
             $class->setDiscriminatorValue($annot->value);
         } elseif ($annot instanceof ODM\ChangeTrackingPolicy) {
             $class->setChangeTrackingPolicy(constant('Doctrine\\ODM\\Riak\\Mapping\\ClassMetadata::CHANGETRACKING_' . $annot->value));
         } elseif ($annot instanceof ODM\DefaultDiscriminatorValue) {
             $class->setDefaultDiscriminatorValue($annot->value);
         }
     }
     if (!$documentAnnots) {
         throw MappingException::classIsNotAValidDocument($className);
     }
     // find the winning document annotation
     ksort($documentAnnots);
     $documentAnnot = reset($documentAnnots);
     if ($documentAnnot instanceof ODM\MappedSuperclass) {
         $class->isMappedSuperclass = true;
     } elseif ($documentAnnot instanceof ODM\EmbeddedDocument) {
         $class->isEmbeddedDocument = true;
     }
     if (isset($documentAnnot->db)) {
         $class->setDatabase($documentAnnot->db);
     }
     if (isset($documentAnnot->collection)) {
         $class->setCollection($documentAnnot->collection);
     }
     if (isset($documentAnnot->repositoryClass) && !$class->isEmbeddedDocument) {
         $class->setCustomRepositoryClass($documentAnnot->repositoryClass);
     }
     if (isset($documentAnnot->indexes)) {
         foreach ($documentAnnot->indexes as $index) {
             $this->addIndex($class, $index);
         }
     }
     if (isset($documentAnnot->requireIndexes)) {
         $class->setRequireIndexes($documentAnnot->requireIndexes);
     }
     if (isset($documentAnnot->slaveOkay)) {
         $class->setSlaveOkay($documentAnnot->slaveOkay);
     }
     foreach ($reflClass->getProperties() as $property) {
         if ($class->isMappedSuperclass && !$property->isPrivate() || $class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name) {
             continue;
         }
         $indexes = array();
         $mapping = array('fieldName' => $property->getName());
         $fieldAnnot = null;
         foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
             if ($annot instanceof ODM\AbstractField) {
                 $fieldAnnot = $annot;
             }
             if ($annot instanceof ODM\AbstractIndex) {
                 $indexes[] = $annot;
             }
             if ($annot instanceof ODM\Indexes) {
                 foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) {
                     $indexes[] = $index;
                 }
             } elseif ($annot instanceof ODM\AlsoLoad) {
                 $mapping['alsoLoadFields'] = (array) $annot->value;
             } elseif ($annot instanceof ODM\Version) {
                 $mapping['version'] = true;
             } elseif ($annot instanceof ODM\Lock) {
                 $mapping['lock'] = true;
             }
         }
         if ($fieldAnnot) {
             $mapping = array_replace($mapping, (array) $fieldAnnot);
             $class->mapField($mapping);
         }
         if ($indexes) {
             foreach ($indexes as $index) {
                 $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName'];
                 $keys = array($name => $index->order ?: 'asc');
                 $this->addIndex($class, $index, $keys);
             }
         }
     }
     /** @var $method \ReflectionMethod */
     foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
         /* Filter for the declaring class only. Callbacks from parent
          * classes will already be registered.
          */
         if ($method->getDeclaringClass()->name !== $reflClass->name) {
             continue;
         }
         foreach ($this->reader->getMethodAnnotations($method) as $annot) {
             if ($annot instanceof ODM\AlsoLoad) {
                 $class->registerAlsoLoadMethod($method->getName(), $annot->value);
             }
             if (!isset($classAnnotations['CosmoW\\ODM\\Riak\\Mapping\\Annotations\\HasLifecycleCallbacks'])) {
                 continue;
             }
             if ($annot instanceof ODM\PrePersist) {
                 $class->addLifecycleCallback($method->getName(), Events::prePersist);
             } elseif ($annot instanceof ODM\PostPersist) {
                 $class->addLifecycleCallback($method->getName(), Events::postPersist);
             } elseif ($annot instanceof ODM\PreUpdate) {
                 $class->addLifecycleCallback($method->getName(), Events::preUpdate);
             } elseif ($annot instanceof ODM\PostUpdate) {
                 $class->addLifecycleCallback($method->getName(), Events::postUpdate);
             } elseif ($annot instanceof ODM\PreRemove) {
                 $class->addLifecycleCallback($method->getName(), Events::preRemove);
             } elseif ($annot instanceof ODM\PostRemove) {
                 $class->addLifecycleCallback($method->getName(), Events::postRemove);
             } elseif ($annot instanceof ODM\PreLoad) {
                 $class->addLifecycleCallback($method->getName(), Events::preLoad);
             } elseif ($annot instanceof ODM\PostLoad) {
                 $class->addLifecycleCallback($method->getName(), Events::postLoad);
             } elseif ($annot instanceof ODM\PreFlush) {
                 $class->addLifecycleCallback($method->getName(), Events::preFlush);
             }
         }
     }
 }