Skip to content

Commit 81c3bc3

Browse files
alexander-schranzniklasnatter
authored andcommitted
Move Repository creation into own compiler pass (#14)
1 parent e168774 commit 81c3bc3

File tree

4 files changed

+75
-32
lines changed

4 files changed

+75
-32
lines changed

DependencyInjection/Compiler/MappingPass.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ public function process(ContainerBuilder $container)
3030
$analysis = $container->getParameter('es.analysis');
3131
$managers = $container->getParameter('es.managers');
3232

33-
$collector = $container->get('es.metadata_collector');
34-
3533
foreach ($managers as $managerName => $manager) {
3634
$connection = $manager['index'];
3735
$managerName = strtolower($managerName);
@@ -65,31 +63,6 @@ public function process(ContainerBuilder $container)
6563
->setPublic(true);
6664
}
6765
}
68-
69-
$mappings = $collector->getMappings($manager['mappings']);
70-
71-
// Building repository services.
72-
foreach ($mappings as $repositoryType => $repositoryDetails) {
73-
$repositoryDefinition = new Definition(
74-
'ONGR\ElasticsearchBundle\Service\Repository',
75-
[$repositoryDetails['namespace']]
76-
);
77-
$repositoryDefinition->setPublic(true);
78-
79-
if (isset($repositoryDetails['directory_name']) && $managerName == 'default') {
80-
$container->get('es.document_finder')->setDocumentDir($repositoryDetails['directory_name']);
81-
}
82-
83-
$repositoryDefinition->setFactory(
84-
[
85-
new Reference(sprintf('es.manager.%s', $managerName)),
86-
'getRepository',
87-
]
88-
);
89-
90-
$repositoryId = sprintf('es.manager.%s.%s', $managerName, $repositoryType);
91-
$container->setDefinition($repositoryId, $repositoryDefinition);
92-
}
9366
}
9467
}
9568
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\HttpKernel\Kernel;
19+
20+
/**
21+
* Compiles elastic search data.
22+
*/
23+
class RepositoryPass implements CompilerPassInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function process(ContainerBuilder $container)
29+
{
30+
$managers = $container->getParameter('es.managers');
31+
32+
$collector = $container->get('es.metadata_collector');
33+
34+
foreach ($managers as $managerName => $manager) {
35+
$mappings = $collector->getMappings($manager['mappings']);
36+
37+
// Building repository services.
38+
foreach ($mappings as $repositoryType => $repositoryDetails) {
39+
$repositoryDefinition = new Definition(
40+
'ONGR\ElasticsearchBundle\Service\Repository',
41+
[$repositoryDetails['namespace']]
42+
);
43+
$repositoryDefinition->setPublic(true);
44+
45+
if (isset($repositoryDetails['directory_name']) && $managerName == 'default') {
46+
$container->get('es.document_finder')->setDocumentDir($repositoryDetails['directory_name']);
47+
}
48+
49+
$repositoryDefinition->setFactory(
50+
[
51+
new Reference(sprintf('es.manager.%s', $managerName)),
52+
'getRepository',
53+
]
54+
);
55+
56+
$repositoryId = sprintf('es.manager.%s.%s', $managerName, $repositoryType);
57+
$container->setDefinition($repositoryId, $repositoryDefinition);
58+
}
59+
}
60+
}
61+
}

ONGRElasticsearchBundle.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\ManagerPass;
1515
use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\MappingPass;
16+
use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\RepositoryPass;
1617
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
1819
use Symfony\Component\HttpKernel\Bundle\Bundle;
@@ -29,8 +30,10 @@ public function build(ContainerBuilder $container)
2930
{
3031
parent::build($container);
3132

32-
// MappingPass need to be behind the Symfony `DecoratorServicePass` to allow decorating the annotation reader
33-
$container->addCompilerPass(new MappingPass(), PassConfig::TYPE_OPTIMIZE, -10);
34-
$container->addCompilerPass(new ManagerPass(), PassConfig::TYPE_OPTIMIZE, -11);
33+
$container->addCompilerPass(new MappingPass());
34+
$container->addCompilerPass(new ManagerPass());
35+
// The `RepositoryPass` need to be behind the Symfony `DecoratorServicePass`
36+
// to allow decorating the annotation reader
37+
$container->addCompilerPass(new RepositoryPass(), PassConfig::TYPE_OPTIMIZE, -10);
3538
}
3639
}

Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace ONGR\ElasticsearchBundle\Tests\Unit\DependencyInjection\Compiler;
1313

1414
use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\MappingPass;
15+
use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\RepositoryPass;
1516
use PHPUnit\Framework\TestCase;
1617
use Symfony\Component\DependencyInjection\Alias;
1718

@@ -69,7 +70,7 @@ public function getContainerMock(array $managers)
6970
->disableOriginalConstructor()
7071
->getMock();
7172

72-
$containerMock->expects($this->exactly(2))->method('getParameter')->with($this->anything())
73+
$containerMock->expects($this->exactly(3))->method('getParameter')->with($this->anything())
7374
->will(
7475
$this->returnCallback(
7576
function ($parameter) use ($managers) {
@@ -96,6 +97,7 @@ function ($parameter) use ($metadataCollectorMock) {
9697
}
9798
)
9899
);
100+
99101
$containerMock
100102
->expects($this->exactly(2))
101103
->method('setDefinition')
@@ -132,7 +134,11 @@ function ($parameter) use ($metadataCollectorMock) {
132134
*/
133135
public function testProcessWithSeveralManagers(array $managers)
134136
{
137+
$container = $this->getContainerMock($managers);
138+
135139
$compilerPass = new MappingPass();
136-
$compilerPass->process($this->getContainerMock($managers));
140+
$compilerPass->process($container);
141+
$compilerPass = new RepositoryPass();
142+
$compilerPass->process($container);
137143
}
138144
}

0 commit comments

Comments
 (0)