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

Deprecate configuring caches through DoctrineCacheBundle #981

Merged
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
37 changes: 29 additions & 8 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use const E_USER_DEPRECATED;
use function array_key_exists;
use function in_array;
use function is_array;
use function sprintf;
use function trigger_error;

/**
* This class contains the configuration information for the bundle
Expand Down Expand Up @@ -706,16 +710,33 @@ private function getOrmCacheDriverNode($name)
})
->end()
->children()
->scalarNode('type')->defaultValue('array')->end()
->scalarNode('type')
->defaultNull()
->beforeNormalization()
->ifNotInArray([null, 'pool', 'service'])
->then(static function ($v) use ($name) {
@trigger_error(
sprintf(
'Using the "%s" type for cache "%s" is deprecated since DoctrineBundle 1.12 and will be dropped in 2.0. Please use the "service" or "pool" types exclusively.',
$v,
$name
),
E_USER_DEPRECATED
);

return $v;
})
->end()
->end()
->scalarNode('id')->end()
->scalarNode('pool')->end()
->scalarNode('host')->end()
->scalarNode('port')->end()
->scalarNode('database')->end()
->scalarNode('instance_class')->end()
->scalarNode('class')->end()
->scalarNode('namespace')->defaultNull()->end()
->scalarNode('cache_provider')->defaultNull()->end()
->scalarNode('host')->setDeprecated()->end()
->scalarNode('port')->setDeprecated()->end()
->scalarNode('database')->setDeprecated()->end()
->scalarNode('instance_class')->setDeprecated()->end()
->scalarNode('class')->setDeprecated()->end()
->scalarNode('namespace')->defaultNull()->setDeprecated()->end()
->scalarNode('cache_provider')->defaultNull()->setDeprecated()->end()
->end();

return $node;
Expand Down
17 changes: 17 additions & 0 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,13 @@ protected function loadCacheDriver($driverName, $entityManagerName, array $drive
$serviceId = null;
$aliasId = $this->getObjectManagerElementName(sprintf('%s_%s', $entityManagerName, $driverName));

if ($driverMap['type'] === null) {
$driverMap = [
'type' => 'pool',
'pool' => $this->getPoolNameForCacheDriver($driverName),
];
}

switch ($driverMap['type']) {
case 'service':
$serviceId = $driverMap['id'];
Expand Down Expand Up @@ -888,4 +895,14 @@ private function createPoolCacheDefinition(ContainerBuilder $container, string $

return $serviceId;
}

private function getPoolNameForCacheDriver(string $driverName) : string
{
switch ($driverName) {
case 'metadata_cache':
return 'cache.system';
default:
return 'cache.app';
}
}
}
2 changes: 1 addition & 1 deletion Tests/Builder/BundleConfigurationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function addBaseEntityManager()
public function addBaseSecondLevelCache()
{
$this->addSecondLevelCache([
'region_cache_driver' => ['type' => 'memcache'],
'region_cache_driver' => ['type' => 'pool', 'pool' => 'my_pool'],
'regions' => [
'hour_region' => ['lifetime' => 3600],
],
Expand Down
8 changes: 4 additions & 4 deletions Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Doctrine\Bundle\DoctrineBundle\Tests;

use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\EventManager;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
Expand All @@ -20,6 +19,7 @@
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
use Symfony\Bridge\Doctrine\Validator\DoctrineLoader;
use Symfony\Component\Cache\DoctrineProvider;
use Symfony\Component\PropertyInfo\PropertyInitializableExtractorInterface;

class ContainerTest extends TestCase
Expand Down Expand Up @@ -47,9 +47,9 @@ public function testContainer()
$this->assertInstanceOf(Reader::class, $container->get('doctrine.orm.metadata.annotation_reader'));
$this->assertInstanceOf(Configuration::class, $container->get('doctrine.orm.default_configuration'));
$this->assertInstanceOf(MappingDriverChain::class, $container->get('doctrine.orm.default_metadata_driver'));
$this->assertInstanceOf(ArrayCache::class, $container->get('doctrine.orm.default_metadata_cache'));
$this->assertInstanceOf(ArrayCache::class, $container->get('doctrine.orm.default_query_cache'));
$this->assertInstanceOf(ArrayCache::class, $container->get('doctrine.orm.default_result_cache'));
$this->assertInstanceOf(DoctrineProvider::class, $container->get('doctrine.orm.default_metadata_cache'));
$this->assertInstanceOf(DoctrineProvider::class, $container->get('doctrine.orm.default_query_cache'));
$this->assertInstanceOf(DoctrineProvider::class, $container->get('doctrine.orm.default_result_cache'));
$this->assertInstanceOf(EntityManager::class, $container->get('doctrine.orm.default_entity_manager'));
$this->assertInstanceOf(Connection::class, $container->get('database_connection'));
$this->assertInstanceOf(EntityManager::class, $container->get('doctrine.orm.entity_manager'));
Expand Down
41 changes: 32 additions & 9 deletions Tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\DoctrineProvider;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -339,13 +341,19 @@ public function testLoadMultipleConnections()
$this->assertEquals('doctrine.orm.em2_configuration', (string) $arguments[1]);

$definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_metadata_cache'));
$this->assertEquals('%doctrine_cache.xcache.class%', $definition->getClass());
$this->assertEquals(DoctrineProvider::class, $definition->getClass());

$definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_query_cache'));
$this->assertEquals('%doctrine_cache.array.class%', $definition->getClass());
$this->assertEquals(DoctrineProvider::class, $definition->getClass());
$arguments = $definition->getArguments();
$this->assertInstanceOf(Reference::class, $arguments[0]);
$this->assertEquals('cache.app', (string) $arguments[0]);

$definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_result_cache'));
$this->assertEquals('%doctrine_cache.array.class%', $definition->getClass());
$this->assertEquals(DoctrineProvider::class, $definition->getClass());
$arguments = $definition->getArguments();
$this->assertInstanceOf(Reference::class, $arguments[0]);
$this->assertEquals('cache.app', (string) $arguments[0]);
}

public function testLoadLogging()
Expand Down Expand Up @@ -373,15 +381,18 @@ public function testEntityManagerMetadataCacheDriverConfiguration()
$container = $this->loadContainer('orm_service_multiple_entity_managers');

$definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_metadata_cache'));
$this->assertDICDefinitionClass($definition, '%doctrine_cache.xcache.class%');
$this->assertDICDefinitionClass($definition, DoctrineProvider::class);

$definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em2_metadata_cache'));
$this->assertDICDefinitionClass($definition, '%doctrine_cache.apc.class%');
$this->assertDICDefinitionClass($definition, DoctrineProvider::class);
}

/**
* @group legacy
*/
public function testEntityManagerMemcacheMetadataCacheDriverConfiguration()
{
$container = $this->loadContainer('orm_service_simple_single_entity_manager');
$container = $this->loadContainer('orm_service_simple_single_entity_manager_memcache');

$definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.default_metadata_cache'));
$this->assertDICDefinitionClass($definition, '%doctrine_cache.memcache.class%');
Expand All @@ -399,6 +410,9 @@ public function testEntityManagerMemcacheMetadataCacheDriverConfiguration()
]);
}

/**
* @group legacy
*/
public function testEntityManagerRedisMetadataCacheDriverConfigurationWithDatabaseKey()
{
$container = $this->loadContainer('orm_service_simple_single_entity_manager_redis');
Expand All @@ -422,7 +436,7 @@ public function testDependencyInjectionImportsOverrideDefaults()
$container = $this->loadContainer('orm_imports');

$cacheDefinition = $container->getDefinition((string) $container->getAlias('doctrine.orm.default_metadata_cache'));
$this->assertEquals('%doctrine_cache.apc.class%', $cacheDefinition->getClass());
$this->assertEquals(DoctrineProvider::class, $cacheDefinition->getClass());

$configDefinition = $container->getDefinition('doctrine.orm.default_configuration');
$this->assertDICDefinitionMethodCallOnce($configDefinition, 'setAutoGenerateProxyClasses', ['%doctrine.orm.auto_generate_proxy_classes%']);
Expand Down Expand Up @@ -605,7 +619,7 @@ public function testSecondLevelCache()
$this->assertDICDefinitionClass($myEntityRegionDef, '%doctrine.orm.second_level_cache.default_region.class%');
$this->assertDICDefinitionClass($loggerChainDef, '%doctrine.orm.second_level_cache.logger_chain.class%');
$this->assertDICDefinitionClass($loggerStatisticsDef, '%doctrine.orm.second_level_cache.logger_statistics.class%');
$this->assertDICDefinitionClass($cacheDriverDef, '%doctrine_cache.array.class%');
$this->assertDICDefinitionClass($cacheDriverDef, DoctrineProvider::class);
$this->assertDICDefinitionMethodCallOnce($configDef, 'setSecondLevelCacheConfiguration');
$this->assertDICDefinitionMethodCallCount($slcFactoryDef, 'setRegion', [], 3);
$this->assertDICDefinitionMethodCallCount($loggerChainDef, 'setLogger', [], 3);
Expand Down Expand Up @@ -1018,14 +1032,23 @@ private function getContainer(array $bundles)
$map[$bundle] = 'Fixtures\\Bundles\\' . $bundle . '\\' . $bundle;
}

return new ContainerBuilder(new ParameterBag([
$container = new ContainerBuilder(new ParameterBag([
'kernel.name' => 'app',
'kernel.debug' => false,
'kernel.bundles' => $map,
'kernel.cache_dir' => sys_get_temp_dir(),
'kernel.environment' => 'test',
'kernel.root_dir' => __DIR__ . '/../../', // src dir
'kernel.project_dir' => __DIR__ . '/../../', // src dir
'kernel.bundles_metadata' => [],
'container.build_id' => uniqid(),
]));

// Register dummy cache services so we don't have to load the FrameworkExtension
$container->setDefinition('cache.system', (new Definition(ArrayAdapter::class))->setPublic(true));
$container->setDefinition('cache.app', (new Definition(ArrayAdapter::class))->setPublic(true));

return $container;
}

/**
Expand Down
Loading