Added configuring repository by annotation
This commit is contained in:
parent
564f1c843b
commit
048b1ff625
18
Configuration/Search.php
Normal file
18
Configuration/Search.php
Normal file
|
@ -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,11 +2,11 @@
|
|||
|
||||
namespace FOQ\ElasticaBundle\Manager;
|
||||
|
||||
use Doctrine\Common\Annotations\Reader;
|
||||
use Doctrine\Common\Persistence\ManagerRegistry;
|
||||
use FOQ\ElasticaBundle\Finder\FinderInterface;
|
||||
use FOQ\ElasticaBundle\Repository;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* @author Richard Miller <info@limethinking.co.uk>
|
||||
*
|
||||
|
@ -18,10 +18,12 @@ class RepositoryManager implements RepositoryManagerInterface
|
|||
protected $entities = array();
|
||||
protected $repositories = array();
|
||||
protected $managerRegistry;
|
||||
protected $reader;
|
||||
|
||||
public function __construct(ManagerRegistry $managerRegistry)
|
||||
public function __construct(ManagerRegistry $managerRegistry, Reader $reader)
|
||||
{
|
||||
$this->managerRegistry = $managerRegistry;
|
||||
$this->reader = $reader;
|
||||
}
|
||||
|
||||
public function addEntity($entityName, FinderInterface $finder, $repositoryName = null)
|
||||
|
@ -53,6 +55,8 @@ class RepositoryManager implements RepositoryManagerInterface
|
|||
throw new RuntimeException(sprintf('No search finder configured for %s', $realEntityName));
|
||||
}
|
||||
|
||||
$this->setRepositoryFromAnnotation($realEntityName);
|
||||
|
||||
if (isset($this->entities[$realEntityName]['repositoryName'])) {
|
||||
|
||||
$repositoryName = $this->entities[$realEntityName]['repositoryName'];
|
||||
|
@ -69,4 +73,18 @@ class RepositoryManager implements RepositoryManagerInterface
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
|
||||
<service id="foq_elastica.manager.mongodb" class="FOQ\ElasticaBundle\Manager\RepositoryManager">
|
||||
<argument type="service" id="doctrine.odm.mongodb"/>
|
||||
<argument type="service" id="annotation_reader"/>
|
||||
</service>
|
||||
|
||||
</services>
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
|
||||
<service id="foq_elastica.manager.orm" class="FOQ\ElasticaBundle\Manager\RepositoryManager">
|
||||
<argument type="service" id="doctrine"/>
|
||||
<argument type="service" id="annotation_reader"/>
|
||||
</service>
|
||||
|
||||
</services>
|
||||
|
|
|
@ -6,6 +6,8 @@ use FOQ\ElasticaBundle\Manager\RepositoryManager;
|
|||
|
||||
class CustomRepository{}
|
||||
|
||||
class Entity{}
|
||||
|
||||
/**
|
||||
* @author Richard Miller <info@limethinking.co.uk>
|
||||
*/
|
||||
|
@ -22,9 +24,13 @@ class RepositoryManagerTest extends \PHPUnit_Framework_TestCase
|
|||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$entityName = 'Test Entity';
|
||||
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$manager = new RepositoryManager($registryMock);
|
||||
$entityName = 'FOQ\ElasticaBundle\Tests\Manager\Entity';
|
||||
|
||||
$manager = new RepositoryManager($registryMock, $readerMock);
|
||||
$manager->addEntity($entityName, $finderMock);
|
||||
$repository = $manager->getRepository($entityName);
|
||||
$this->assertInstanceOf('FOQ\ElasticaBundle\Repository', $repository);
|
||||
|
@ -40,9 +46,13 @@ class RepositoryManagerTest extends \PHPUnit_Framework_TestCase
|
|||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$entityName = 'Test Entity';
|
||||
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$manager = new RepositoryManager($registryMock);
|
||||
$entityName = 'FOQ\ElasticaBundle\Tests\Manager\Entity';
|
||||
|
||||
$manager = new RepositoryManager($registryMock, $readerMock);
|
||||
$manager->addEntity($entityName, $finderMock, 'FOQ\ElasticaBundle\Tests\Manager\CustomRepository');
|
||||
$repository = $manager->getRepository($entityName);
|
||||
$this->assertInstanceOf('FOQ\ElasticaBundle\Tests\Manager\CustomRepository', $repository);
|
||||
|
@ -61,9 +71,13 @@ class RepositoryManagerTest extends \PHPUnit_Framework_TestCase
|
|||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$entityName = 'Test Entity';
|
||||
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$manager = new RepositoryManager($registryMock);
|
||||
$entityName = 'FOQ\ElasticaBundle\Tests\Manager\Entity';
|
||||
|
||||
$manager = new RepositoryManager($registryMock, $readerMock);
|
||||
$manager->addEntity($entityName, $finderMock);
|
||||
$manager->getRepository('Missing Entity');
|
||||
}
|
||||
|
@ -81,9 +95,13 @@ class RepositoryManagerTest extends \PHPUnit_Framework_TestCase
|
|||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$entityName = 'Test Entity';
|
||||
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$manager = new RepositoryManager($registryMock);
|
||||
$entityName = 'FOQ\ElasticaBundle\Tests\Manager\Entity';
|
||||
|
||||
$manager = new RepositoryManager($registryMock, $readerMock);
|
||||
$manager->addEntity($entityName, $finderMock, 'FOQ\ElasticaBundle\Tests\MissingRepository');
|
||||
$manager->getRepository('Missing Entity');
|
||||
}
|
||||
|
@ -98,17 +116,21 @@ class RepositoryManagerTest extends \PHPUnit_Framework_TestCase
|
|||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$shortEntityName = 'TestBundle:TestEntity';
|
||||
$entityName = 'TestBundle\Full\Path\To\TestEntity';
|
||||
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$shortEntityName = 'TestBundle:Entity';
|
||||
$entityName = 'FOQ\ElasticaBundle\Tests\Manager\Entity';
|
||||
$shortPath = 'TestBundle';
|
||||
$fullPath = 'TestBundle\Full\Path\To';
|
||||
$fullPath = 'FOQ\ElasticaBundle\Tests\Manager';
|
||||
|
||||
$registryMock->expects($this->once())
|
||||
->method('getAliasNamespace')
|
||||
->with($this->equalTo($shortPath))
|
||||
->will($this->returnValue($fullPath));
|
||||
|
||||
$manager = new RepositoryManager($registryMock);
|
||||
$manager = new RepositoryManager($registryMock, $readerMock);
|
||||
$manager->addEntity($entityName, $finderMock);
|
||||
$repository = $manager->getRepository($shortEntityName);
|
||||
$this->assertInstanceOf('FOQ\ElasticaBundle\Repository', $repository);
|
||||
|
|
Loading…
Reference in a new issue