diff --git a/src/Doctrine/ODM/OrientDB/Configuration.php b/src/Doctrine/ODM/OrientDB/Configuration.php new file mode 100644 index 0000000..722a40f --- /dev/null +++ b/src/Doctrine/ODM/OrientDB/Configuration.php @@ -0,0 +1,155 @@ +metadataDriver = $mappingDriver; + } + + /** + * Get the metadata driver + * + * @return MappingDriver + */ + public function getMetadataDriver() + { + return $this->metadataDriver; + } + + /** + * Set binding + * + * @param BindingInterface $binding + */ + public function setBinding(BindingInterface $binding) + { + $this->binding = $binding; + } + + /** + * Get bindung + * + * @return BindingInterface + */ + public function getBinding() + { + return $this->binding; + } + + /** + * Adds a new default annotation driver with a correctly configured annotation reader. If $useSimpleAnnotationReader + * is true, the notation `@Entity` will work, otherwise, the notation `@ODM\Entity` will be supported. + * + * @param array $paths + * @param bool $useSimpleAnnotationReader + * + * @return AnnotationDriver + */ + public function newDefaultAnnotationDriver($paths = array(), $useSimpleAnnotationReader = true) + { + /** @todo update this */ + AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php'); + + if ($useSimpleAnnotationReader) { + // Register the ORM Annotations in the AnnotationRegistry + $reader = new SimpleAnnotationReader(); + $reader->addNamespace('Doctrine\ODM\OrientDB\Mapper'); /* @todo rename directory to Mapping */ + $cachedReader = new CachedReader($reader, new ArrayCache()); + + return new AnnotationDriver($cachedReader, (array) $paths); /* @todo write class */ + } + + return new AnnotationDriver( + new CachedReader(new AnnotationReader(), new ArrayCache()), + (array) $paths + ); + } + + /** + * Sets a class metadata factory. + * + * @param string $cmfName + * @return void + */ + public function setClassMetadataFactoryName($classMetadataFactoryName) + { + $this->classMetadataFactoryName = $classMetadataFactoryName; + } + + /** + * Get class metadata factory name + * + * @return string + */ + public function getClassMetadataFactoryName() + { + return $this->classMetadataFactoryName; + } + + /** + * Set default repository class + * + * @param string $className + * @return void + * @throws ODMException If not is a \Doctrine\Common\Persistence\ObjectRepository + */ + public function setDefaultRepositoryClassName($className) + { + $reflectionClass = new \ReflectionClass($className); + + if ( ! $reflectionClass->implementsInterface('Doctrine\Common\Persistence\ObjectRepository')) { + throw ODMException::invalidDocumentRepository($className); + } + $this->defaultRepositoryClassName = $className; + } + + /** + * Get default repository class. + * + * @return string + */ + public function getDefaultRepositoryClassName() + { + return $this->defaultRepositoryClassName; + } +} diff --git a/src/Doctrine/ODM/OrientDB/Events.php b/src/Doctrine/ODM/OrientDB/Events.php new file mode 100644 index 0000000..20b252a --- /dev/null +++ b/src/Doctrine/ODM/OrientDB/Events.php @@ -0,0 +1,145 @@ +mapper = $mapper; $this->binding = $binding; $this->metadataFactory = $metadataFactory ?: new ClassMetadataFactory($mapper); + $this->eventManager = $eventManager ?: new EventManager(); + } + + /** + * Gets the EventManager used by the DocumentManager. + * + * @return EventManager + */ + public function getEventManager() + { + return $this->eventManager; } /** @@ -138,7 +157,7 @@ public function find($rid, $fetchPlan = '*:1', $lazy = true) * @param string $rid * @param mixed $fetchPlan * @return Proxy\Collection|array - * @throws Doctrine\OrientDB\Binding\InvalidQueryException + * @throws \Doctrine\OrientDB\Binding\InvalidQueryException */ public function findRecords(Array $rids, $fetchPlan = null, $lazy = true) { @@ -163,9 +182,10 @@ public function findRecords(Array $rids, $fetchPlan = null, $lazy = true) /** * @todo to implement/test * - * @param \stdClass $object + * @param null|object|array $entity + * @return void */ - public function flush() + public function flush($entity = null) { throw new \Exception; } @@ -174,7 +194,7 @@ public function flush() * Gets the $class Metadata. * * @param string $class - * @return Doctrine\Common\Persistence\Mapping\ClassMetadata + * @return \Doctrine\ODM\OrientDb\Mapper\ClassMetadata */ public function getClassMetadata($class) { @@ -199,13 +219,20 @@ public function getMetadataFactory() */ public function getRepository($className) { - $repositoryClass = $className . "Repository"; + if (isset($this->repositories[$className])) { + return $this->repositories[$className]; + } + $metadata = $this->getClassMetadata($className); + $customRepositoryClassName = $metadata->getRepositoryClassname(); - if (class_exists($repositoryClass)) { - return new $repositoryClass($className, $this, $this->getMapper()); + if (null !== $customRepositoryClassName) { + $repository = new $customRepositoryClassName($className, $this, $this->getMapper(), $this->getEventManager()); + } else { + $repository = new Repository($className, $this, $this->getMapper(), $this->getEventManager()); } - return new Repository($className, $this, $this->getMapper()); + $this->repositories[$className] = $repository; + return $repository; } /** diff --git a/src/Doctrine/ODM/OrientDB/Mapper/Annotations/Document.php b/src/Doctrine/ODM/OrientDB/Mapper/Annotations/Document.php index 8fd51a8..25c4084 100644 --- a/src/Doctrine/ODM/OrientDB/Mapper/Annotations/Document.php +++ b/src/Doctrine/ODM/OrientDB/Mapper/Annotations/Document.php @@ -25,6 +25,7 @@ class Document extends \Doctrine\Common\Annotations\Annotation { public $class; + public $repositoryClass; /** * Given a $Doctrine\OrientDBClass, checks wheter this annotation matches it. diff --git a/src/Doctrine/ODM/OrientDB/Mapper/Annotations/Reader.php b/src/Doctrine/ODM/OrientDB/Mapper/Annotations/Reader.php index f67a1d4..cfd9b6c 100644 --- a/src/Doctrine/ODM/OrientDB/Mapper/Annotations/Reader.php +++ b/src/Doctrine/ODM/OrientDB/Mapper/Annotations/Reader.php @@ -143,7 +143,7 @@ protected function getReader() /** * Creates a new instance of a cache provider. * - * @return Doctrine\Common\Cache\CacheProvider + * @return \Doctrine\Common\Cache\CacheProvider */ protected function createCacheProvider() { diff --git a/src/Doctrine/ODM/OrientDB/Mapper/ClassMetadata.php b/src/Doctrine/ODM/OrientDB/Mapper/ClassMetadata.php index 1b0a919..3568c37 100644 --- a/src/Doctrine/ODM/OrientDB/Mapper/ClassMetadata.php +++ b/src/Doctrine/ODM/OrientDB/Mapper/ClassMetadata.php @@ -31,6 +31,14 @@ class ClassMetadata implements DoctrineMetadata private $singleAssociations = array('link'); private $multipleAssociations = array('linklist', 'linkset', 'linkmap'); + /** + * The name of the custom repository class used for the document class. + * (Optional). + * + * @var string|null + */ + protected $repositoryClassName; + /** * Instantiates a new Metadata for the given $className. * @@ -65,10 +73,31 @@ public function getIdentifier() return array('@rid'); } + /** + * Registers a custom repository class for the document class. + * + * @param string $mapperClassName The class name of the custom mapper. + */ + public function setRepositoryClassName($repositoryClassName) + { + $this->repositoryClassName = $repositoryClassName; + } + + /** + * Get repository class + * + * @return string|null + */ + public function getRepositoryClassName() + { + $document = $this->getMapper()->getClassAnnotation($this->getName()); + return $document->repositoryClass; + } + /** * Gets the ReflectionClass instance for this mapped class. * - * @return ReflectionClass + * @return \ReflectionClass */ public function getReflectionClass() { @@ -346,7 +375,7 @@ protected function isValuedAssociation($field, Array $associationTypes) /** * Returns the mapper associated with this Metadata. * - * @return Mapper + * @return DataMapper */ protected function getMapper() { diff --git a/src/Doctrine/ODM/OrientDB/ODMException.php b/src/Doctrine/ODM/OrientDB/ODMException.php new file mode 100644 index 0000000..c2e2d6f --- /dev/null +++ b/src/Doctrine/ODM/OrientDB/ODMException.php @@ -0,0 +1,23 @@ +