Exemplo n.º 1
0
 public function initialise($container)
 {
     $this->manager = $container->get('doctrine.odm.mongodb.document_manager');
     $this->odmMetadata = $this->manager->getClassMetadata($this->documentName);
     $this->class = $this->odmMetadata->getReflectionClass()->name;
     $mapping = $container->get('grid.mapping.manager');
     $mapping->addDriver($this, -1);
     $this->metadata = $mapping->getMetadata($this->class);
 }
 /**
  * {@inheritdoc}
  */
 public function __construct(DocumentManager $dm, UnitOfWork $uow, ClassMetadata $class)
 {
     if ($class->getReflectionClass()->isSubclassOf('Gedmo\\Translatable\\Document\\MappedSuperclass\\AbstractPersonalTranslation')) {
         throw new \Gedmo\Exception\UnexpectedValueException('This repository is useless for personal translations');
     }
     parent::__construct($dm, $uow, $class);
 }
 protected function buildMetadata(ClassMetadata $source, ClassMetadata $target, $eventManager)
 {
     $sourceReflClass = $source->getReflectionClass();
     $targetReflClass = $target->getReflectionClass();
     //Document annotations
     foreach ($this->annotationReader->getClassAnnotations($sourceReflClass) as $annotation) {
         if (defined(get_class($annotation) . '::event')) {
             // Raise annotation event
             if ($eventManager->hasListeners($annotation::event)) {
                 $eventManager->dispatchEvent($annotation::event, new AnnotationEventArgs($target, EventType::document, $annotation, $targetReflClass, $eventManager));
             }
         }
     }
     //Field annotations
     foreach ($sourceReflClass->getProperties() as $reflField) {
         foreach ($this->annotationReader->getPropertyAnnotations($reflField) as $annotation) {
             if (defined(get_class($annotation) . '::event')) {
                 // Raise annotation event
                 if ($eventManager->hasListeners($annotation::event)) {
                     $eventManager->dispatchEvent($annotation::event, new AnnotationEventArgs($target, EventType::field, $annotation, $reflField, $eventManager));
                 }
             }
         }
     }
     //Method annotations
     foreach ($sourceReflClass->getMethods() as $reflMethod) {
         foreach ($this->annotationReader->getMethodAnnotations($reflMethod) as $annotation) {
             if (defined(get_class($annotation) . '::event')) {
                 // Raise annotation event
                 if ($eventManager->hasListeners($annotation::event)) {
                     $eventManager->dispatchEvent($annotation::event, new AnnotationEventArgs($target, EventType::method, $annotation, $reflMethod, $eventManager));
                 }
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass($className, ClassMetadata $class)
 {
     $reflClass = $class->getReflectionClass();
     $classAnnotations = $this->reader->getClassAnnotations($reflClass);
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\Document'])) {
         $documentAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\Document'];
     } elseif (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\MappedSuperclass'])) {
         $documentAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\MappedSuperclass'];
         $class->isMappedSuperclass = true;
     } elseif (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\EmbeddedDocument'])) {
         $documentAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\EmbeddedDocument'];
         $class->isEmbeddedDocument = true;
     } else {
         throw MongoDBException::classIsNotAValidDocument($className);
     }
     if (isset($documentAnnot->db)) {
         $class->setDatabase($documentAnnot->db);
     }
     if (isset($documentAnnot->collection)) {
         $class->setCollection($documentAnnot->collection);
     }
     if (isset($documentAnnot->repositoryClass)) {
         $class->setCustomRepositoryClass($documentAnnot->repositoryClass);
     }
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\Indexes'])) {
         $indexes = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\Indexes']->value;
         $indexes = is_array($indexes) ? $indexes : array($indexes);
         foreach ($indexes as $index) {
             $this->addIndex($class, $index);
         }
     }
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\Index'])) {
         $index = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\Index'];
         $this->addIndex($class, $index);
     }
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\UniqueIndex'])) {
         $index = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\UniqueIndex'];
         $this->addIndex($class, $index);
     }
     if (isset($documentAnnot->indexes)) {
         foreach ($documentAnnot->indexes as $index) {
             $this->addIndex($class, $index);
         }
     }
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\InheritanceType'])) {
         $inheritanceTypeAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\InheritanceType'];
         $class->setInheritanceType(constant('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value));
     }
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorField'])) {
         $discrFieldAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorField'];
         $class->setDiscriminatorField(array('fieldName' => $discrFieldAnnot->fieldName));
     }
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorMap'])) {
         $discrMapAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorMap'];
         $class->setDiscriminatorMap($discrMapAnnot->value);
     }
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorValue'])) {
         $discrValueAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorValue'];
         $class->setDiscriminatorValue($discrValueAnnot->value);
     }
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\ChangeTrackingPolicy'])) {
         $changeTrackingAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\ChangeTrackingPolicy'];
         $class->setChangeTrackingPolicy(constant('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata::CHANGETRACKING_' . $changeTrackingAnnot->value));
     }
     $methods = $reflClass->getMethods();
     foreach ($reflClass->getProperties() as $property) {
         if ($class->isMappedSuperclass && !$property->isPrivate() || $class->isInheritedField($property->name)) {
             continue;
         }
         $mapping = array();
         $mapping['fieldName'] = $property->getName();
         if ($alsoLoad = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\MongoDB\\Mapping\\AlsoLoad')) {
             $mapping['alsoLoadFields'] = (array) $alsoLoad->value;
         }
         if ($notSaved = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\MongoDB\\Mapping\\NotSaved')) {
             $mapping['notSaved'] = true;
         }
         if ($versionAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\MongoDB\\Mapping\\Version')) {
             $mapping['version'] = true;
         }
         if ($versionAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\MongoDB\\Mapping\\Lock')) {
             $mapping['lock'] = true;
         }
         $indexes = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\MongoDB\\Mapping\\Indexes');
         $indexes = $indexes ? $indexes : array();
         if ($index = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\MongoDB\\Mapping\\Index')) {
             $indexes[] = $index;
         }
         if ($index = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\MongoDB\\Mapping\\UniqueIndex')) {
             $indexes[] = $index;
         }
         foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) {
             if ($fieldAnnot instanceof \Doctrine\ODM\MongoDB\Mapping\Field) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $class->mapField($mapping);
             }
         }
         if ($indexes) {
             foreach ($indexes as $index) {
                 $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName'];
                 $keys = array();
                 $keys[$name] = 'asc';
                 if (isset($index->order)) {
                     $keys[$name] = $index->order;
                 }
                 $this->addIndex($class, $index, $keys);
             }
         }
     }
     foreach ($methods as $method) {
         if ($method->isPublic()) {
             if ($alsoLoad = $this->reader->getMethodAnnotation($method, 'Doctrine\\ODM\\MongoDB\\Mapping\\AlsoLoad')) {
                 $fields = (array) $alsoLoad->value;
                 foreach ($fields as $value) {
                     $class->alsoLoadMethods[$value] = $method->getName();
                 }
             }
         }
     }
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\HasLifecycleCallbacks'])) {
         foreach ($methods as $method) {
             if ($method->isPublic()) {
                 $annotations = $this->reader->getMethodAnnotations($method);
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PrePersist'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::prePersist);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostPersist'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::postPersist);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PreUpdate'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::preUpdate);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostUpdate'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::postUpdate);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PreRemove'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::preRemove);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostRemove'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::postRemove);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PreLoad'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::preLoad);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostLoad'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::postLoad);
                 }
             }
         }
     }
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass($className, ClassMetadata $class)
 {
     $reflClass = $class->getReflectionClass();
     $classAnnotations = $this->_reader->getClassAnnotations($reflClass);
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\Document'])) {
         $documentAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\Document'];
         if ($documentAnnot->db) {
             $class->setDB($documentAnnot->db);
         }
         if ($documentAnnot->collection) {
             $class->setCollection($documentAnnot->collection);
         }
         if ($documentAnnot->repositoryClass) {
             $class->setCustomRepositoryClass($documentAnnot->repositoryClass);
         }
         if ($documentAnnot->indexes) {
             foreach ($documentAnnot->indexes as $index) {
                 $class->addIndex($index->keys, $index->options);
             }
         }
         if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\InheritanceType'])) {
             $inheritanceTypeAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\InheritanceType'];
             $class->setInheritanceType(constant('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value));
         }
         if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorField'])) {
             $discrFieldAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorField'];
             $class->setDiscriminatorField(array('fieldName' => $discrFieldAnnot->fieldName));
         }
         if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorMap'])) {
             $discrMapAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorMap'];
             $class->setDiscriminatorMap($discrMapAnnot->value);
         }
     } else {
         if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\MappedSuperclass'])) {
             $class->isMappedSuperclass = true;
         } else {
             if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\EmbeddedDocument'])) {
                 $class->isEmbeddedDocument = true;
             }
         }
     }
     foreach ($reflClass->getProperties() as $property) {
         $mapping = array();
         $mapping['fieldName'] = $property->getName();
         $types = array('Id', 'Increment', 'File', 'Field', 'String', 'Boolean', 'Int', 'Float', 'Date', 'Key', 'Bin', 'BinFunc', 'BinUUID', 'BinMD5', 'BinCustom', 'EmbedOne', 'EmbedMany', 'ReferenceOne', 'ReferenceMany', 'Timestamp', 'Hash', 'Collection');
         foreach ($types as $type) {
             if ($fieldAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\MongoDB\\Mapping\\' . $type)) {
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $class->mapField($mapping);
                 break;
             }
         }
     }
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\HasLifecycleCallbacks'])) {
         foreach ($reflClass->getMethods() as $method) {
             if ($method->isPublic()) {
                 $annotations = $this->_reader->getMethodAnnotations($method);
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PrePersist'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::prePersist);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostPersist'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::postPersist);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PreUpdate'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::preUpdate);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostUpdate'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::postUpdate);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PreRemove'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::preRemove);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostRemove'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::postRemove);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostLoad'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\Events::postLoad);
                 }
             }
         }
     }
 }
Exemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass($className, ClassMetadata $class)
 {
     $reflClass = $class->getReflectionClass();
     $classAnnotations = $this->_reader->getClassAnnotations($reflClass);
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\Document'])) {
         $documentAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\Document'];
         if ($documentAnnot->db) {
             $class->setDB($documentAnnot->db);
         }
         if ($documentAnnot->collection) {
             $class->setCollection($documentAnnot->collection);
         }
         if ($documentAnnot->repositoryClass) {
             $class->setCustomRepositoryClass($documentAnnot->repositoryClass);
         }
         if ($documentAnnot->indexes) {
             foreach ($documentAnnot->indexes as $index) {
                 $class->addIndex($index->keys, $index->options);
             }
         }
         if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\InheritanceType'])) {
             $inheritanceTypeAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\InheritanceType'];
             $class->setInheritanceType(constant('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value));
         }
         if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorField'])) {
             $discrFieldAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorField'];
             $class->setDiscriminatorField(array('fieldName' => $discrFieldAnnot->fieldName));
         }
         if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorMap'])) {
             $discrMapAnnot = $classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\DiscriminatorMap'];
             $class->setDiscriminatorMap($discrMapAnnot->value);
         }
     } else {
         if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\MappedSuperclass'])) {
             $class->isMappedSuperclass = true;
         } else {
             if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\EmbeddedDocument'])) {
                 $class->isEmbeddedDocument = true;
             }
         }
     }
     $methods = $reflClass->getMethods();
     foreach ($reflClass->getProperties() as $property) {
         $mapping = array();
         $mapping['fieldName'] = $property->getName();
         if ($alsoLoad = $this->_reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\MongoDB\\Mapping\\AlsoLoad')) {
             $mapping['alsoLoadFields'] = (array) $alsoLoad->value;
         }
         if ($notSaved = $this->_reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\MongoDB\\Mapping\\NotSaved')) {
             $mapping['notSaved'] = true;
         }
         foreach ($this->_reader->getPropertyAnnotations($property) as $fieldAnnot) {
             if ($fieldAnnot instanceof \Doctrine\ODM\MongoDB\Mapping\Field) {
                 if ($fieldAnnot instanceof \Doctrine\ODM\MongoDB\Mapping\Id && $fieldAnnot->custom) {
                     $fieldAnnot->type = 'custom_id';
                     $class->setAllowCustomId(true);
                 }
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $class->mapField($mapping);
             }
         }
         $types = array('Embed', 'Reference');
         foreach ($types as $type) {
             if ($fieldAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\\ODM\\MongoDB\\Mapping\\' . $type)) {
                 // This is a blatant hack to see if the defined default
                 // value is an array so we can make the embed/reference many
                 // instead of one. This won't be necessary once the ReflectionProperty
                 // class has a getDefaultValue() method: http://bugs.php.net/bug.php?id=41670
                 $property->setAccessible(true);
                 $default = $property->getValue(new $class->name());
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 if (is_array($default)) {
                     $mapping['type'] = 'many';
                 } else {
                     $mapping['type'] = 'one';
                 }
                 $class->mapField($mapping);
             }
         }
     }
     foreach ($methods as $method) {
         if ($method->isPublic()) {
             if ($alsoLoad = $this->_reader->getMethodAnnotation($method, 'Doctrine\\ODM\\MongoDB\\Mapping\\AlsoLoad')) {
                 $class->fieldMappings[$mapping['fieldName']]['alsoLoadMethods'][] = array('name' => (array) $alsoLoad->value, 'method' => $method->getName());
             }
         }
     }
     if (isset($classAnnotations['Doctrine\\ODM\\MongoDB\\Mapping\\HasLifecycleCallbacks'])) {
         foreach ($methods as $method) {
             if ($method->isPublic()) {
                 $annotations = $this->_reader->getMethodAnnotations($method);
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PrePersist'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\ODMEvents::prePersist);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostPersist'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\ODMEvents::postPersist);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PreUpdate'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\ODMEvents::preUpdate);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostUpdate'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\ODMEvents::postUpdate);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PreRemove'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\ODMEvents::preRemove);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostRemove'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\ODMEvents::postRemove);
                 }
                 if (isset($annotations['Doctrine\\ODM\\MongoDB\\Mapping\\PostLoad'])) {
                     $class->addLifecycleCallback($method->getName(), \Doctrine\ODM\MongoDB\ODMEvents::postLoad);
                 }
             }
         }
     }
 }
Exemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function getReflectionClass()
 {
     return $this->classMetadata->getReflectionClass();
 }