Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-31588: As a Developer I want to configure search engine features with tags which follows same convention #51

Merged
merged 2 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +6,7 @@
*/
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler;

use eZ\Publish\Core\Base\Container\Compiler\TaggedServiceIdsIterator\BackwardCompatibleIterator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -31,20 +32,13 @@ public function process(ContainerBuilder $container)

$parameterProviderRegistryDef = $container->getDefinition('ezpublish.fieldType.parameterProviderRegistry');

$deprecatedFieldTypeParameterProviderTags = $container->findTaggedServiceIds(self::DEPRECATED_FIELD_TYPE_PARAMETER_PROVIDER_SERVICE_TAG);
foreach ($deprecatedFieldTypeParameterProviderTags as $deprecatedFieldTypeParameterProviderTag) {
@trigger_error(
sprintf(
'Service tag `%s` is deprecated and will be removed in eZ Platform 4.0. Use `%s` instead.',
self::DEPRECATED_FIELD_TYPE_PARAMETER_PROVIDER_SERVICE_TAG,
self::FIELD_TYPE_PARAMETER_PROVIDER_SERVICE_TAG
),
E_USER_DEPRECATED
);
}
$fieldTypeParameterProviderTags = $container->findTaggedServiceIds(self::FIELD_TYPE_PARAMETER_PROVIDER_SERVICE_TAG);
$parameterProviderFieldTypesTags = array_merge($deprecatedFieldTypeParameterProviderTags, $fieldTypeParameterProviderTags);
foreach ($parameterProviderFieldTypesTags as $id => $attributes) {
$iterator = new BackwardCompatibleIterator(
$container,
self::FIELD_TYPE_PARAMETER_PROVIDER_SERVICE_TAG,
self::DEPRECATED_FIELD_TYPE_PARAMETER_PROVIDER_SERVICE_TAG
);

foreach ($iterator as $id => $attributes) {
foreach ($attributes as $attribute) {
if (!isset($attribute['alias'])) {
throw new \LogicException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler;

use AppendIterator;
use ArrayIterator;
use Iterator;
use eZ\Publish\Core\Base\Container\Compiler\TaggedServiceIdsIterator\BackwardCompatibleIterator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -31,7 +29,12 @@ public function process(ContainerBuilder $container): void

$queryTypes = [];

$iterator = $this->getTaggedServiceIdsIterator($container);
$iterator = new BackwardCompatibleIterator(
$container,
self::QUERY_TYPE_SERVICE_TAG,
self::DEPRECATED_QUERY_TYPE_SERVICE_TAG
);

foreach ($iterator as $taggedServiceId => $tags) {
$queryTypeDefinition = $container->getDefinition($taggedServiceId);
$queryTypeClass = $container->getParameterBag()->resolveValue($queryTypeDefinition->getClass());
Expand All @@ -45,31 +48,4 @@ public function process(ContainerBuilder $container): void
$aggregatorDefinition = $container->getDefinition('ezpublish.query_type.registry');
$aggregatorDefinition->addMethodCall('addQueryTypes', [$queryTypes]);
}

private function getTaggedServiceIdsIterator(ContainerBuilder $container): Iterator
{
$serviceIdsWithDeprecatedTags = $container->findTaggedServiceIds(
self::DEPRECATED_QUERY_TYPE_SERVICE_TAG
);

foreach ($serviceIdsWithDeprecatedTags as $serviceId => $tags) {
@trigger_error(
sprintf(
'Service tag `%s` is deprecated and will be removed in eZ Platform 4.0. Tag %s with `%s` instead.',
self::DEPRECATED_QUERY_TYPE_SERVICE_TAG,
$serviceId,
self::QUERY_TYPE_SERVICE_TAG
),
E_USER_DEPRECATED
);
}

$taggedServiceIds = $container->findTaggedServiceIds(self::QUERY_TYPE_SERVICE_TAG);

$iterator = new AppendIterator();
$iterator->append(new ArrayIterator($serviceIdsWithDeprecatedTags));
$iterator->append(new ArrayIterator($taggedServiceIds));

return $iterator;
}
}
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],
];
}
}
Loading