Skip to content
This repository has been archived by the owner on Oct 29, 2020. It is now read-only.

Allow providers to be loaded lazy #113

Closed
Closed
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
4 changes: 4 additions & 0 deletions DependencyInjection/CacheProviderLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public function loadCacheProvider($name, array $config, ContainerBuilder $contai
$container->setAlias($alias, $serviceId);
}

if ($config['lazy']) {
$service->setLazy(true);
}

if ($this->definitionClassExists($type, $container)) {
$this->getCacheDefinition($type, $container)->configure($name, $config, $service, $container);
}
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public function getConfigTreeBuilder()
->then($normalization)
->end()
->children()
->scalarNode('lazy')->defaultFalse()->end()
->scalarNode('namespace')->defaultNull()->end()
->scalarNode('type')->defaultNull()->end()
->append($this->addBasicProviderNode('apc'))
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/SymfonyBridgeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function loadCacheDriver($cacheName, $objectManagerName, array $cacheDriv
$type => array(),
'type' => $type,
'namespace' => null,
'lazy' => false
);

if ( ! isset($cacheDriver['namespace'])) {
Expand Down
19 changes: 18 additions & 1 deletion Tests/DependencyInjection/AbstractDoctrineCacheExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,24 @@ public function testUnrecognizedCacheDriverException()
$this->compileContainer('unrecognized');
}

public function testLazy()
{
$container = $this->compileContainer('lazy');

$providers = array(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know about the phpunit data provider ?
It would allow you to get rid of the foreach.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if the first test fail you still see that the other pass.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other tests in the same class do it the exact same way.

'doctrine_cache.providers.lazy_provider' => true,
'doctrine_cache.providers.lazy_false_provider' => false,
'doctrine_cache.providers.lazy_no_provider' => false,
);

foreach ($providers as $key => $lazy) {
$this->assertTrue($container->hasDefinition($key));
$definition = $container->getDefinition($key);
$this->assertTrue($definition->isLazy() == $lazy);
}

}

public function testAcl()
{
$container = $this->compileContainer('acl');
Expand All @@ -315,7 +333,6 @@ public function assertCacheProvider(ContainerBuilder $container, $name, $class,

$this->assertTrue($definition->isPublic());
$this->assertEquals($class, $definition->getClass());

foreach (array_unique($expectedCalls) as $methodName => $params) {
$this->assertMethodCall($definition, $methodName, $params);
}
Expand Down
20 changes: 20 additions & 0 deletions Tests/DependencyInjection/Fixtures/config/xml/lazy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" ?>
<srv:container xmlns="http://doctrine-project.org/schemas/symfony-dic/cache"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://doctrine-project.org/schemas/symfony-dic/cache http://doctrine-project.org/schemas/symfony-dic/cache/doctrine_cache-1.0.xsd">

<doctrine-cache>

<provider name="lazy_provider" type="apc">
<lazy>true</lazy>
</provider>
<provider name="lazy_false_provider" type="array">
<lazy>false</lazy>
</provider>
<provider name="lazy_no_provider" type="array">
</provider>

</doctrine-cache>
</srv:container>
10 changes: 10 additions & 0 deletions Tests/DependencyInjection/Fixtures/config/yml/lazy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
doctrine_cache:
providers:
lazy_provider:
type: apc
lazy: true
lazy_false_provider:
type: array
lazy: false
lazy_no_provider:
type: redis