Skip to content

Commit

Permalink
Move Repository creation into own compiler pass (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored Sep 28, 2020
1 parent e168774 commit 9a1a3c0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 32 deletions.
27 changes: 0 additions & 27 deletions DependencyInjection/Compiler/MappingPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
}
61 changes: 61 additions & 0 deletions DependencyInjection/Compiler/RepositoryPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* 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);
}
}
}
}
9 changes: 6 additions & 3 deletions ONGRElasticsearchBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
10 changes: 8 additions & 2 deletions Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -96,6 +97,7 @@ function ($parameter) use ($metadataCollectorMock) {
}
)
);

$containerMock
->expects($this->exactly(2))
->method('setDefinition')
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 9a1a3c0

Please sign in to comment.