Skip to content

Commit

Permalink
EZP-31588: Aligned search engine tags to convention
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Jul 2, 2020
1 parent ef9bfab commit 06b7cc0
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function buildSearchEngine()
) {
throw new InvalidSearchEngine(
"Invalid search engine '{$repositoryConfig['search']['engine']}'. " .
"Could not find any service tagged with 'ezpublish.searchEngine' " .
"Could not find any service tagged with 'ezplatform.search_engine' " .
"with alias '{$repositoryConfig['search']['engine']}'."
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function buildSearchEngineIndexer()
) {
throw new InvalidSearchEngineIndexer(
"Invalid search engine '{$repositoryConfig['search']['engine']}'. " .
"Could not find any service tagged with 'ezpublish.searchEngineIndexer' " .
"Could not find any service tagged with 'ezplatform.search_engine.indexer' " .
"with alias '{$repositoryConfig['search']['engine']}'."
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
*/
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler;

use eZ\Publish\Core\Base\Container\Compiler\TaggedServiceIdsIterator\BackwardCompatibleIterator;
use LogicException;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use LogicException;

/**
* This compiler pass registers eZ Publish search engines indexers.
*/
class RegisterSearchEngineIndexerPass implements CompilerPassInterface
{
public const SEARCH_ENGINE_INDEXER_SERVICE_TAG = 'ezplatform.search_engine.indexer';
public const DEPRECATED_SEARCH_ENGINE_INDEXER_SERVICE_TAG = 'ezpublish.searchEngineIndexer';

/**
* Container service id of the SearchEngineIndexerFactory.
*
Expand All @@ -39,20 +43,31 @@ public function process(ContainerBuilder $container)
}

$searchEngineIndexerFactoryDefinition = $container->getDefinition($this->factoryId);
foreach ($container->findTaggedServiceIds('ezpublish.searchEngineIndexer') as $id => $attributes) {

$iterator = new BackwardCompatibleIterator(
$container,
self::SEARCH_ENGINE_INDEXER_SERVICE_TAG,
self::DEPRECATED_SEARCH_ENGINE_INDEXER_SERVICE_TAG
);

foreach ($iterator as $serviceId => $attributes) {
foreach ($attributes as $attribute) {
if (!isset($attribute['alias'])) {
throw new LogicException(
'ezpublish.searchEngineIndexer service tag needs an "alias" attribute to ' .
'identify the search engine.'
sprintf(
'Service "%s" tagged with "%s" or "%s" needs an "alias" attribute to identify the search engine',
$serviceId,
self::SEARCH_ENGINE_INDEXER_SERVICE_TAG,
self::DEPRECATED_SEARCH_ENGINE_INDEXER_SERVICE_TAG
)
);
}

// Register the search engine with the search engine factory
$searchEngineIndexerFactoryDefinition->addMethodCall(
'registerSearchEngineIndexer',
[
new Reference($id),
new Reference($serviceId),
$attribute['alias'],
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
*/
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler;

use eZ\Publish\Core\Base\Container\Compiler\TaggedServiceIdsIterator\BackwardCompatibleIterator;
use LogicException;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use LogicException;

/**
* This compiler pass will register eZ Publish search engines.
*/
class RegisterSearchEnginePass implements CompilerPassInterface
{
public const SEARCH_ENGINE_SERVICE_TAG = 'ezplatform.search_engine';
public const DEPRECATED_SEATCH_ENGINE_SERVICE_TAG = 'ezpublish.searchEngine';

/**
* Container service id of the SearchEngineFactory.
*
Expand All @@ -40,20 +44,30 @@ public function process(ContainerBuilder $container)

$searchEngineFactoryDefinition = $container->getDefinition($this->factoryId);

foreach ($container->findTaggedServiceIds('ezpublish.searchEngine') as $id => $attributes) {
$iterator = new BackwardCompatibleIterator(
$container,
self::SEARCH_ENGINE_SERVICE_TAG,
self::DEPRECATED_SEATCH_ENGINE_SERVICE_TAG
);

foreach ($iterator as $serviceId => $attributes) {
foreach ($attributes as $attribute) {
if (!isset($attribute['alias'])) {
throw new LogicException(
'ezpublish.searchEngine service tag needs an "alias" attribute to ' .
'identify the search engine.'
sprintf(
'Service "%s" tagged with "%s" or "%s" needs an "alias" attribute to identify the search engine',
$serviceId,
self::SEARCH_ENGINE_SERVICE_TAG,
self::DEPRECATED_SEATCH_ENGINE_SERVICE_TAG
)
);
}

// Register the search engine with the search engine factory
$searchEngineFactoryDefinition->addMethodCall(
'registerSearchEngine',
[
new Reference($id),
new Reference($serviceId),
$attribute['alias'],
]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Compiler;

use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\RegisterSearchEngineIndexerPass;
use LogicException;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

final class RegisterSearchEngineIndexerPassTest extends AbstractCompilerPassTestCase
{
private const EXAMPLE_SERVICE_ID = 'app.search_engine';
private const EXAMPLE_ALIAS = 'foo';

protected function setUp(): void
{
parent::setUp();

$this->setDefinition('ezpublish.api.search_engine.indexer.factory', new Definition());
}

protected function registerCompilerPass(ContainerBuilder $container): void
{
$container->addCompilerPass(new RegisterSearchEngineIndexerPass());
}

/**
* @dataProvider tagsProvider
*/
public function testRegisterSearchEngineIndexer(string $tag): void
{
$definition = new Definition();
$definition->addTag($tag, [
'alias' => self::EXAMPLE_ALIAS,
]);

$this->setDefinition(self::EXAMPLE_SERVICE_ID, $definition);
$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'ezpublish.api.search_engine.indexer.factory',
'registerSearchEngineIndexer',
[
new Reference(self::EXAMPLE_SERVICE_ID),
self::EXAMPLE_ALIAS,
]
);
}

/**
* @dataProvider tagsProvider
*/
public function testRegisterSearchEngineIndexerWithoutAliasThrowsLogicException(string $tag): void
{
$this->expectException(LogicException::class);

$definition = new Definition();
$definition->addTag($tag);

$this->setDefinition(self::EXAMPLE_SERVICE_ID, $definition);
$this->compile();
}

public function tagsProvider(): iterable
{
return [
[RegisterSearchEngineIndexerPass::SEARCH_ENGINE_INDEXER_SERVICE_TAG],
[RegisterSearchEngineIndexerPass::DEPRECATED_SEARCH_ENGINE_INDEXER_SERVICE_TAG],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Compiler;

use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\RegisterSearchEnginePass;
use LogicException;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

final class RegisterSearchEnginePassTest extends AbstractCompilerPassTestCase
{
private const EXAMPLE_SERVICE_ID = 'app.search_engine';
private const EXAMPLE_ALIAS = 'foo';

protected function setUp(): void
{
parent::setUp();

$this->setDefinition('ezpublish.api.search_engine.factory', new Definition());
}

protected function registerCompilerPass(ContainerBuilder $container): void
{
$container->addCompilerPass(new RegisterSearchEnginePass());
}

/**
* @dataProvider tagsProvider
*/
public function testRegisterSearchEngine(string $tag): void
{
$definition = new Definition();
$definition->addTag($tag, [
'alias' => self::EXAMPLE_ALIAS,
]);

$this->setDefinition(self::EXAMPLE_SERVICE_ID, $definition);
$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'ezpublish.api.search_engine.factory',
'registerSearchEngine',
[
new Reference(self::EXAMPLE_SERVICE_ID),
self::EXAMPLE_ALIAS,
]
);
}

/**
* @dataProvider tagsProvider
*/
public function testRegisterSearchEngineWithoutAliasThrowsLogicException(string $tag): void
{
$this->expectException(LogicException::class);

$definition = new Definition();
$definition->addTag($tag);

$this->setDefinition(self::EXAMPLE_SERVICE_ID, $definition);
$this->compile();
}

public function tagsProvider(): iterable
{
return [
[RegisterSearchEnginePass::SEARCH_ENGINE_SERVICE_TAG],
[RegisterSearchEnginePass::DEPRECATED_SEATCH_ENGINE_SERVICE_TAG],
];
}
}
4 changes: 2 additions & 2 deletions eZ/Publish/Core/settings/search_engines/legacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ services:
$languageHandler: '@ezpublish.spi.persistence.language_handler'
$mapper: '@ezpublish.search.legacy.fulltext_mapper'
tags:
- {name: ezpublish.searchEngine, alias: legacy}
- {name: ezplatform.search_engine, alias: legacy}
lazy: true

ezpublish.spi.search.legacy.indexer:
Expand All @@ -79,5 +79,5 @@ services:
$connection: '@ezpublish.persistence.connection'
$searchHandler: "@ezpublish.spi.search.legacy"
tags:
- {name: ezpublish.searchEngineIndexer, alias: legacy}
- {name: ezplatform.search_engine.indexer, alias: legacy}
lazy: true

0 comments on commit 06b7cc0

Please sign in to comment.