diff --git a/DependencyInjection/Compiler/MappingPass.php b/DependencyInjection/Compiler/MappingPass.php index 88e47468..0fbf1f76 100644 --- a/DependencyInjection/Compiler/MappingPass.php +++ b/DependencyInjection/Compiler/MappingPass.php @@ -71,10 +71,18 @@ public function process(ContainerBuilder $container) [$repository], ] ); - $container->setDefinition( - sprintf('es.manager.%s.%s', $managerName, substr($repository, strpos($repository, ':') + 1)), - $repositoryDefinition - ); + + $repository = substr($repository, strpos($repository, ':') + 1); + $repositoryId = sprintf('es.manager.%s.%s', $managerName, $repository); + + if (strtolower(substr($repository, -8)) === 'document') { + $container->setAlias( + sprintf('es.manager.%s.%s', $managerName, substr($repository, 0, strlen($repository) - 8)), + $repositoryId + ); + } + + $container->setDefinition($repositoryId, $repositoryDefinition); } } } diff --git a/ORM/Manager.php b/ORM/Manager.php index e6db259c..34e5e561 100644 --- a/ORM/Manager.php +++ b/ORM/Manager.php @@ -69,7 +69,7 @@ public function getRepository($type) { $type = is_array($type) ? $type : [$type]; - foreach ($type as $selectedType) { + foreach ($type as &$selectedType) { $this->checkRepositoryType($selectedType); } @@ -174,13 +174,23 @@ public function getTypesMapping() * * @throws \InvalidArgumentException */ - private function checkRepositoryType($type) + private function checkRepositoryType(&$type) { - if (!array_key_exists($type, $this->getBundlesMapping())) { - $exceptionMessage = "Undefined repository {$type}, valid repositories are: " . - join(', ', array_keys($this->getBundlesMapping())) . '.'; - throw new \InvalidArgumentException($exceptionMessage); + $mapping = $this->getBundlesMapping(); + + if (array_key_exists($type, $mapping)) { + return; } + + if (array_key_exists($type . 'Document', $mapping)) { + $type .= 'Document'; + + return; + } + + $exceptionMessage = "Undefined repository `{$type}`, valid repositories are: `" . + join('`, `', array_keys($this->getBundlesMapping())) . '`.'; + throw new \InvalidArgumentException($exceptionMessage); } /** diff --git a/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php b/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php index 73454e4d..3e4bf22d 100644 --- a/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php +++ b/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php @@ -41,6 +41,18 @@ public function getTestContainerData() 'es.manager.default.bar', 'ONGR\ElasticsearchBundle\ORM\Repository', ], + [ + 'es.manager.default.color', + 'ONGR\ElasticsearchBundle\ORM\Repository', + ], + [ + 'es.manager.default.colordocument', + 'ONGR\ElasticsearchBundle\ORM\Repository', + ], + [ + 'es.metadata_collector', + 'ONGR\ElasticsearchBundle\Mapping\MetadataCollector', + ], ]; } diff --git a/Tests/Functional/ORM/RepositoryTest.php b/Tests/Functional/ORM/RepositoryTest.php index 97c66448..4a647ba1 100644 --- a/Tests/Functional/ORM/RepositoryTest.php +++ b/Tests/Functional/ORM/RepositoryTest.php @@ -534,6 +534,20 @@ public function testSuggest($suggesters, $expectedResults) } } + /** + * Tests if repository is fetched without suffix. + */ + public function testGetRepositoryWithDoucmentSuffix() + { + $manager = $this->getManager(); + $repository = $manager->getRepository('AcmeTestBundle:Color'); + + $this->assertInstanceOf( + 'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\ColorDocument', + $repository->createDocument() + ); + } + /** * Assert suggestion score is set. * diff --git a/Tests/Unit/ORM/ManagerTest.php b/Tests/Unit/ORM/ManagerTest.php index f7c82736..78fe2bd2 100644 --- a/Tests/Unit/ORM/ManagerTest.php +++ b/Tests/Unit/ORM/ManagerTest.php @@ -64,7 +64,7 @@ public function testGetRepositories() * Check if an exception is thrown when an undefined repository is specified. * * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Undefined repository rep1, valid repositories are: rep2, rep3. + * @expectedExceptionMessage Undefined repository `rep1`, valid repositories are: `rep2`, `rep3`. */ public function testGetRepositoriesException() { @@ -83,7 +83,7 @@ public function testGetRepositoriesException() * Check if an exception is thrown when an undefined repository is specified and only a single rep is specified. * * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Undefined repository rep1, valid repositories are: rep2, rep3. + * @expectedExceptionMessage Undefined repository `rep1`, valid repositories are: `rep2`, `rep3`. */ public function testGetRepositoriesExceptionSingle() { diff --git a/Tests/app/fixture/Acme/TestBundle/Document/ColorDocument.php b/Tests/app/fixture/Acme/TestBundle/Document/ColorDocument.php new file mode 100644 index 00000000..06e0af86 --- /dev/null +++ b/Tests/app/fixture/Acme/TestBundle/Document/ColorDocument.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document; + +use ONGR\ElasticsearchBundle\Annotation as ES; +use ONGR\ElasticsearchBundle\Document\DocumentInterface; +use ONGR\ElasticsearchBundle\Document\DocumentTrait; + +/** + * Class ColorDocument. + * + * @ES\Document(type="color") + */ +class ColorDocument implements DocumentInterface +{ + use DocumentTrait; +}