diff --git a/DependencyInjection/Compiler/MappingPass.php b/DependencyInjection/Compiler/MappingPass.php index 1bd89a9b..4f3032d8 100644 --- a/DependencyInjection/Compiler/MappingPass.php +++ b/DependencyInjection/Compiler/MappingPass.php @@ -30,8 +30,6 @@ public function process(ContainerBuilder $container) $analysis = $container->getParameter('es.analysis'); $managers = $container->getParameter('es.managers'); - $collector = $container->get('es.metadata_collector'); - foreach ($managers as $managerName => $manager) { $connection = $manager['index']; $managerName = strtolower($managerName); @@ -65,31 +63,6 @@ public function process(ContainerBuilder $container) ->setPublic(true); } } - - $mappings = $collector->getMappings($manager['mappings']); - - // Building repository services. - foreach ($mappings as $repositoryType => $repositoryDetails) { - $repositoryDefinition = new Definition( - 'ONGR\ElasticsearchBundle\Service\Repository', - [$repositoryDetails['namespace']] - ); - $repositoryDefinition->setPublic(true); - - if (isset($repositoryDetails['directory_name']) && $managerName == 'default') { - $container->get('es.document_finder')->setDocumentDir($repositoryDetails['directory_name']); - } - - $repositoryDefinition->setFactory( - [ - new Reference(sprintf('es.manager.%s', $managerName)), - 'getRepository', - ] - ); - - $repositoryId = sprintf('es.manager.%s.%s', $managerName, $repositoryType); - $container->setDefinition($repositoryId, $repositoryDefinition); - } } } } diff --git a/DependencyInjection/Compiler/RepositoryPass.php b/DependencyInjection/Compiler/RepositoryPass.php new file mode 100644 index 00000000..4c1c998d --- /dev/null +++ b/DependencyInjection/Compiler/RepositoryPass.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\HttpKernel\Kernel; + +/** + * Compiles elastic search data. + */ +class RepositoryPass implements CompilerPassInterface +{ + /** + * {@inheritdoc} + */ + public function process(ContainerBuilder $container) + { + $managers = $container->getParameter('es.managers'); + + $collector = $container->get('es.metadata_collector'); + + foreach ($managers as $managerName => $manager) { + $mappings = $collector->getMappings($manager['mappings']); + + // Building repository services. + foreach ($mappings as $repositoryType => $repositoryDetails) { + $repositoryDefinition = new Definition( + 'ONGR\ElasticsearchBundle\Service\Repository', + [$repositoryDetails['namespace']] + ); + $repositoryDefinition->setPublic(true); + + if (isset($repositoryDetails['directory_name']) && $managerName == 'default') { + $container->get('es.document_finder')->setDocumentDir($repositoryDetails['directory_name']); + } + + $repositoryDefinition->setFactory( + [ + new Reference(sprintf('es.manager.%s', $managerName)), + 'getRepository', + ] + ); + + $repositoryId = sprintf('es.manager.%s.%s', $managerName, $repositoryType); + $container->setDefinition($repositoryId, $repositoryDefinition); + } + } + } +} diff --git a/ONGRElasticsearchBundle.php b/ONGRElasticsearchBundle.php index 054104bc..0cfa4162 100644 --- a/ONGRElasticsearchBundle.php +++ b/ONGRElasticsearchBundle.php @@ -13,6 +13,7 @@ use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\ManagerPass; use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\MappingPass; +use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\RepositoryPass; use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -29,8 +30,10 @@ public function build(ContainerBuilder $container) { parent::build($container); - // MappingPass need to be behind the Symfony `DecoratorServicePass` to allow decorating the annotation reader - $container->addCompilerPass(new MappingPass(), PassConfig::TYPE_OPTIMIZE, -10); - $container->addCompilerPass(new ManagerPass(), PassConfig::TYPE_OPTIMIZE, -11); + $container->addCompilerPass(new MappingPass()); + $container->addCompilerPass(new ManagerPass()); + // The `RepositoryPass` need to be behind the Symfony `DecoratorServicePass` + // to allow decorating the annotation reader + $container->addCompilerPass(new RepositoryPass(), PassConfig::TYPE_OPTIMIZE, -10); } } diff --git a/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php b/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php index e196e2aa..89444b6a 100644 --- a/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php +++ b/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchBundle\Tests\Unit\DependencyInjection\Compiler; use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\MappingPass; +use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\RepositoryPass; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\Alias; @@ -69,7 +70,7 @@ public function getContainerMock(array $managers) ->disableOriginalConstructor() ->getMock(); - $containerMock->expects($this->exactly(2))->method('getParameter')->with($this->anything()) + $containerMock->expects($this->exactly(3))->method('getParameter')->with($this->anything()) ->will( $this->returnCallback( function ($parameter) use ($managers) { @@ -96,6 +97,7 @@ function ($parameter) use ($metadataCollectorMock) { } ) ); + $containerMock ->expects($this->exactly(2)) ->method('setDefinition') @@ -132,7 +134,11 @@ function ($parameter) use ($metadataCollectorMock) { */ public function testProcessWithSeveralManagers(array $managers) { + $container = $this->getContainerMock($managers); + $compilerPass = new MappingPass(); - $compilerPass->process($this->getContainerMock($managers)); + $compilerPass->process($container); + $compilerPass = new RepositoryPass(); + $compilerPass->process($container); } }