Permalink
Browse files

Added configuring repository by annotation

Conflicts:

	Manager/RepositoryManager.php
	Resources/config/mongodb.xml
	Resources/config/orm.xml
	Tests/Manager/RepositoryManagerTest.php
  • Loading branch information...
1 parent adf8597 commit e678ce8cc654322d0d87cbf51647712d5ee9b7a3 @richardmiller richardmiller committed Dec 13, 2011
View
@@ -0,0 +1,18 @@
+<?php
+
+namespace FOQ\ElasticaBundle\Configuration;
+
+/**
+ * Annotation class for setting search repository.
+ *
+ * @author Richard Miller <info@limethinking.co.uk>
+ * @Annotation
+ * @Target("CLASS")
+ */
+class Search
+{
+
+ /** @var string */
+ public $repositoryClass;
+
+}
@@ -2,10 +2,10 @@
namespace FOQ\ElasticaBundle\Manager;
+use Doctrine\Common\Annotations\Reader;
use FOQ\ElasticaBundle\Finder\FinderInterface;
use FOQ\ElasticaBundle\Repository;
use RuntimeException;
-
/**
* @author Richard Miller <info@limethinking.co.uk>
*
@@ -16,6 +16,12 @@ class RepositoryManager implements RepositoryManagerInterface
{
protected $entities = array();
protected $repositories = array();
+ protected $reader;
+
+ public function __construct(Reader $reader)
+ {
+ $this->reader = $reader;
+ }
public function addEntity($entityName, FinderInterface $finder, $repositoryName = null)
{
@@ -40,6 +46,8 @@ public function getRepository($entityName)
throw new RuntimeException(sprintf('No search finder configured for %s', $entityName));
}
+ $this->setRepositoryFromAnnotation($entityName);
+
if (isset($this->entities[$entityName]['repositoryName'])) {
$repositoryName = $this->entities[$entityName]['repositoryName'];
@@ -56,4 +64,18 @@ public function getRepository($entityName)
return $repository;
}
+ private function setRepositoryFromAnnotation($realEntityName)
+ {
+ if (isset($this->entities[$realEntityName]['repositoryName'])) {
+ return;
+ }
+
+ $refClass = new \ReflectionClass($realEntityName);
+ $annotation = $this->reader->getClassAnnotation($refClass, 'FOQ\\ElasticaBundle\\Configuration\\Search');
+ if ($annotation) {
+ $this->entities[$realEntityName]['repositoryName']
+ = $annotation->repositoryClass;
+ }
+ }
+
}
@@ -61,7 +61,9 @@
<argument /> <!-- options -->
</service>
- <service id="foq_elastica.manager" class="%foq_elastica.manager.class%" />
+ <service id="foq_elastica.manager" class="%foq_elastica.manager.class%">
+ <argument type="service" id="annotation_reader"/>
+ </service>
<service id="foq_elastica.elastica_to_model_transformer.collection.prototype" class="%foq_elastica.elastica_to_model_transformer.collection.class%" public="true" abstract="true">
<argument type="collection" /> <!-- transformers -->
@@ -26,6 +26,7 @@
<argument type="collection" /> <!-- options -->
</service>
+
</services>
</container>
@@ -6,6 +6,8 @@
class CustomRepository{}
+class Entity{}
+
/**
* @author Richard Miller <info@limethinking.co.uk>
*/
@@ -18,9 +20,13 @@ public function testThatGetRepositoryReturnsDefaultRepository()
->disableOriginalConstructor()
->getMock();
- $entityName = 'Test Entity';
+ $readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $entityName = 'FOQ\ElasticaBundle\Tests\Manager\Entity';
- $manager = new RepositoryManager($finderMock);
+ $manager = new RepositoryManager($readerMock);
$manager->addEntity($entityName, $finderMock);
$repository = $manager->getRepository($entityName);
$this->assertInstanceOf('FOQ\ElasticaBundle\Repository', $repository);
@@ -32,9 +38,13 @@ public function testThatGetRepositoryReturnsCustomRepository()
->disableOriginalConstructor()
->getMock();
- $entityName = 'Test Entity';
+ $readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $entityName = 'FOQ\ElasticaBundle\Tests\Manager\Entity';
- $manager = new RepositoryManager($finderMock);
+ $manager = new RepositoryManager($readerMock);
$manager->addEntity($entityName, $finderMock, 'FOQ\ElasticaBundle\Tests\Manager\CustomRepository');
$repository = $manager->getRepository($entityName);
$this->assertInstanceOf('FOQ\ElasticaBundle\Tests\Manager\CustomRepository', $repository);
@@ -49,9 +59,13 @@ public function testThatGetRepositoryThrowsExceptionIfEntityNotConfigured()
->disableOriginalConstructor()
->getMock();
- $entityName = 'Test Entity';
+ $readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
+ ->disableOriginalConstructor()
+ ->getMock();
- $manager = new RepositoryManager($finderMock);
+ $entityName = 'FOQ\ElasticaBundle\Tests\Manager\Entity';
+
+ $manager = new RepositoryManager($readerMock);
$manager->addEntity($entityName, $finderMock);
$manager->getRepository('Missing Entity');
}
@@ -65,10 +79,15 @@ public function testThatGetRepositoryThrowsExceptionIfCustomRepositoryNotFound()
->disableOriginalConstructor()
->getMock();
- $entityName = 'Test Entity';
+ $readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $entityName = 'FOQ\ElasticaBundle\Tests\Manager\Entity';
- $manager = new RepositoryManager($finderMock);
+ $manager = new RepositoryManager($readerMock);
$manager->addEntity($entityName, $finderMock, 'FOQ\ElasticaBundle\Tests\MissingRepository');
$manager->getRepository('Missing Entity');
}
+
}

0 comments on commit e678ce8

Please sign in to comment.