Skip to content

Commit b49a1e3

Browse files
authored
Merge pull request #1689 from deguif/use-service-locator-for-pager-persisters
Use service locator for pager persisters registry
2 parents e95e05b + 7c74926 commit b49a1e3

File tree

10 files changed

+42
-187
lines changed

10 files changed

+42
-187
lines changed

src/DependencyInjection/Compiler/RegisterPagerPersistersPass.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/FOSElasticaBundle.php

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

1414
use FOS\ElasticaBundle\DependencyInjection\Compiler\ConfigSourcePass;
1515
use FOS\ElasticaBundle\DependencyInjection\Compiler\IndexPass;
16-
use FOS\ElasticaBundle\DependencyInjection\Compiler\RegisterPagerPersistersPass;
1716
use Symfony\Component\DependencyInjection\ContainerBuilder;
1817
use Symfony\Component\HttpKernel\Bundle\Bundle;
1918

@@ -28,6 +27,5 @@ public function build(ContainerBuilder $container)
2827

2928
$container->addCompilerPass(new ConfigSourcePass());
3029
$container->addCompilerPass(new IndexPass());
31-
$container->addCompilerPass(new RegisterPagerPersistersPass());
3230
}
3331
}

src/Finder/PaginatedFinderInterface.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace FOS\ElasticaBundle\Finder;
1313

14-
use Elastica\Query;
1514
use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface;
1615
use Pagerfanta\Pagerfanta;
1716

src/Persister/PagerPersisterRegistry.php

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,27 @@
1111

1212
namespace FOS\ElasticaBundle\Persister;
1313

14-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
14+
use Symfony\Component\DependencyInjection\ServiceLocator;
1615

17-
final class PagerPersisterRegistry implements ContainerAwareInterface
16+
final class PagerPersisterRegistry
1817
{
19-
use ContainerAwareTrait;
18+
/** @var ServiceLocator */
19+
private $persisters;
2020

21-
/**
22-
* @var string[]
23-
*/
24-
private $nameToServiceIdMap = [];
25-
26-
/**
27-
* @param string[] $nameToServiceIdMap
28-
*/
29-
public function __construct(array $nameToServiceIdMap)
21+
public function __construct(ServiceLocator $persisters)
3022
{
31-
$this->nameToServiceIdMap = $nameToServiceIdMap;
23+
$this->persisters = $persisters;
3224
}
3325

3426
/**
35-
* @param string $name
36-
*
3727
* @throws \InvalidArgumentException if no pager persister was registered for the given name
38-
*
39-
* @return PagerPersisterInterface
4028
*/
41-
public function getPagerPersister($name)
29+
public function getPagerPersister(string $name): PagerPersisterInterface
4230
{
43-
if (!isset($this->nameToServiceIdMap[$name])) {
31+
if (!$this->persisters->has($name)) {
4432
throw new \InvalidArgumentException(sprintf('No pager persister was registered for the give name "%s".', $name));
4533
}
4634

47-
$serviceId = $this->nameToServiceIdMap[$name];
48-
49-
$pagerPersister = $this->container->get($serviceId);
50-
51-
if (!$pagerPersister instanceof PagerPersisterInterface) {
52-
throw new \LogicException(sprintf('The pager provider service "%s" must implement "%s" interface but it is an instance of "%s" class.', $serviceId, PagerPersisterInterface::class, get_class($pagerPersister)));
53-
}
54-
55-
return $pagerPersister;
35+
return $this->persisters->get($name);
5636
}
5737
}

src/Persister/PersisterRegistry.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515

1616
class PersisterRegistry
1717
{
18-
/**
19-
* @var ServiceLocator
20-
*/
21-
private $persisters = [];
18+
/** @var ServiceLocator */
19+
private $persisters;
2220

2321
public function __construct(ServiceLocator $persisters)
2422
{

src/Provider/PagerProviderRegistry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class PagerProviderRegistry
2020
{
2121
/** @var ServiceLocator */
22-
private $providers = [];
22+
private $providers;
2323

2424
public function __construct(ServiceLocator $providers)
2525
{

src/Resources/config/persister.xml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@
2121
</service>
2222

2323
<service id="fos_elastica.pager_persister_registry" class="FOS\ElasticaBundle\Persister\PagerPersisterRegistry">
24-
<argument type="collection" /> <!-- nameToServiceIdMap -->
25-
26-
<call method="setContainer">
27-
<argument type="service" id="service_container" />
28-
</call>
24+
<argument type="tagged_locator" tag="fos_elastica.pager_persister" index-by="persisterName" />
2925
</service>
3026

3127
<service id="fos_elastica.persister_registry" class="FOS\ElasticaBundle\Persister\PersisterRegistry">

tests/Unit/DependencyInjection/FOSElasticaExtensionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,9 @@ public function testShouldRegisterPagerPersisterRegisterService()
390390

391391
$this->assertTrue($container->hasDefinition('fos_elastica.pager_persister_registry'));
392392

393-
$listener = $container->getDefinition('fos_elastica.pager_persister_registry');
394-
$this->assertSame(PagerPersisterRegistry::class, $listener->getClass());
395-
$this->assertSame([], $listener->getArgument(0));
393+
$registry = $container->getDefinition('fos_elastica.pager_persister_registry');
394+
$this->assertSame(PagerPersisterRegistry::class, $registry->getClass());
395+
$this->assertCount(0, $registry->getArgument(0)->getTaggedIteratorArgument()->getValues());
396396
}
397397

398398
public function testShouldRegisterDoctrineORMListener()

tests/Unit/Persister/AsyncPagerPersisterTest.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use FOS\ElasticaBundle\Provider\PagerProviderInterface;
2323
use FOS\ElasticaBundle\Provider\PagerProviderRegistry;
2424
use PHPUnit\Framework\TestCase;
25-
use Symfony\Component\DependencyInjection\ContainerInterface;
2625
use Symfony\Component\DependencyInjection\ServiceLocator;
2726
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2827
use Symfony\Component\Messenger\Envelope;
@@ -38,7 +37,7 @@ public function testShouldImplementPagerPersisterInterface()
3837

3938
public function testInsertDispatchAsyncPersistPageObject()
4039
{
41-
$pagerPersisterRegistry = new PagerPersisterRegistry([]);
40+
$pagerPersisterRegistry = new PagerPersisterRegistry($this->createMock(ServiceLocator::class));
4241
$pagerProviderRegistry = $this->createMock(PagerProviderRegistry::class);
4342
$messageBus = $this->createMock(MessageBusInterface::class);
4443
$sut = new AsyncPagerPersister($pagerPersisterRegistry, $pagerProviderRegistry, $messageBus);
@@ -57,24 +56,20 @@ function ($message) {
5756

5857
public function testInsertPageReturnObjectCount()
5958
{
60-
$pagerPersisterRegistry = new PagerPersisterRegistry(
61-
[
62-
InPlacePagerPersister::NAME => 'my_service',
63-
]
64-
);
65-
66-
$serviceLocator = $this->createMock(ServiceLocator::class);
67-
$serviceLocator->expects($this->once())->method('has')->with('foo')->willReturn(true);
68-
$serviceLocator->expects($this->once())->method('get')->with('foo')->willReturn($this->createMock(ObjectPersisterInterface::class));
59+
$persistersLocator = $this->createMock(ServiceLocator::class);
60+
$persistersLocator->expects($this->once())->method('has')->with('foo')->willReturn(true);
61+
$persistersLocator->expects($this->once())->method('get')->with('foo')->willReturn($this->createMock(ObjectPersisterInterface::class));
6962

70-
$container = $this->createMock(ContainerInterface::class);
71-
$container->expects($this->once())->method('get')->with('my_service')->willReturn(
63+
$pagerPersistersLocator = $this->createMock(ServiceLocator::class);
64+
$pagerPersistersLocator->expects($this->once())->method('has')->with('in_place')->willReturn(true);
65+
$pagerPersistersLocator->expects($this->once())->method('get')->with('in_place')->willReturn(
7266
new InPlacePagerPersister(
73-
new PersisterRegistry($serviceLocator),
67+
new PersisterRegistry($persistersLocator),
7468
$this->createMock(EventDispatcherInterface::class)
7569
)
7670
);
77-
$pagerPersisterRegistry->setContainer($container);
71+
72+
$pagerPersisterRegistry = new PagerPersisterRegistry($pagerPersistersLocator);
7873

7974
$pagerMock = $this->createMock(PagerInterface::class);
8075
$pagerMock->expects($this->exactly(2))->method('setMaxPerPage')->with(10);

tests/Unit/Persister/PagerPersisterRegistryTest.php

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,84 +12,49 @@
1212
namespace FOS\ElasticaBundle\Persister;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\DependencyInjection\Container;
16-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15+
use Symfony\Component\DependencyInjection\ServiceLocator;
1716

1817
class PagerPersisterRegistryTest extends TestCase
1918
{
20-
public function testShouldImplementContainerAwareInterface()
21-
{
22-
$rc = new \ReflectionClass(PagerPersisterRegistry::class);
23-
24-
$this->assertTrue($rc->implementsInterface(ContainerAwareInterface::class));
25-
}
26-
2719
public function testShouldBeFinal()
2820
{
2921
$rc = new \ReflectionClass(PagerPersisterRegistry::class);
3022

3123
$this->assertTrue($rc->isFinal());
3224
}
3325

34-
public function testCouldBeConstructedWithNameToServiceIdMap()
35-
{
36-
new PagerPersisterRegistry([]);
37-
}
38-
3926
public function testThrowsIfThereIsNoSuchEntryInNameToServiceIdMap()
4027
{
41-
$container = new Container();
42-
43-
$registry = new PagerPersisterRegistry([
44-
'the_name' => 'the_service_id',
45-
]);
46-
$registry->setContainer($container);
28+
$serviceLocator = $this->createMock(ServiceLocator::class);
29+
$serviceLocator->expects($this->once())->method('has')->with('the_name')->willReturn(false);
4730

4831
$this->expectException(\InvalidArgumentException::class);
49-
$this->expectExceptionMessage('No pager persister was registered for the give name "the_other_name".');
50-
$registry->getPagerPersister('the_other_name');
51-
}
32+
$this->expectExceptionMessage('No pager persister was registered for the give name "the_name".');
5233

53-
public function testThrowsIfRelatedServiceDoesNotImplementPagerPersisterInterface()
54-
{
55-
$container = new Container();
56-
$container->set('the_service_id', new \stdClass());
57-
58-
$registry = new PagerPersisterRegistry([
59-
'the_name' => 'the_service_id',
60-
]);
61-
$registry->setContainer($container);
62-
63-
$this->expectException(\LogicException::class);
64-
$this->expectExceptionMessage('The pager provider service "the_service_id" must implement "FOS\ElasticaBundle\Persister\PagerPersisterInterface" interface but it is an instance of "stdClass" class.');
65-
$registry->getPagerPersister('the_name');
34+
(new PagerPersisterRegistry($serviceLocator))->getPagerPersister('the_name');
6635
}
6736

68-
public function testThrowsIfThereIsServiceWithSuchId()
37+
public function testThrowsIfRelatedServiceDoesNotImplementPagerPersisterInterface()
6938
{
70-
$container = new Container();
39+
$serviceLocator = $this->createMock(ServiceLocator::class);
40+
$serviceLocator->expects($this->once())->method('has')->with('the_name')->willReturn(true);
41+
$serviceLocator->expects($this->once())->method('get')->with('the_name')->willReturn(new \stdClass());
7142

72-
$registry = new PagerPersisterRegistry([
73-
'the_name' => 'the_service_id',
74-
]);
75-
$registry->setContainer($container);
43+
$this->expectException(\TypeError::class);
44+
$this->expectExceptionMessage('Return value of FOS\ElasticaBundle\Persister\PagerPersisterRegistry::getPagerPersister() must implement interface FOS\ElasticaBundle\Persister\PagerPersisterInterface, instance of stdClass returned');
7645

77-
$this->expectException(\LogicException::class);
78-
$this->expectExceptionMessage('You have requested a non-existent service "the_service_id".');
79-
$registry->getPagerPersister('the_name');
46+
(new PagerPersisterRegistry($serviceLocator))->getPagerPersister('the_name');
8047
}
8148

8249
public function testShouldReturnPagerPersisterByGivenName()
8350
{
8451
$pagerPersisterMock = $this->createPagerPersisterMock();
8552

86-
$container = new Container();
87-
$container->set('the_service_id', $pagerPersisterMock);
53+
$serviceLocator = $this->createMock(ServiceLocator::class);
54+
$serviceLocator->expects($this->once())->method('has')->with('the_name')->willReturn(true);
55+
$serviceLocator->expects($this->once())->method('get')->with('the_name')->willReturn($pagerPersisterMock);
8856

89-
$registry = new PagerPersisterRegistry([
90-
'the_name' => 'the_service_id',
91-
]);
92-
$registry->setContainer($container);
57+
$registry = new PagerPersisterRegistry($serviceLocator);
9358

9459
$actualPagerPersister = $registry->getPagerPersister('the_name');
9560

0 commit comments

Comments
 (0)