Skip to content

Commit

Permalink
Deprecate configuring caches through doctrine-cache-bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jun 11, 2019
1 parent 5f3fa02 commit 4615b7f
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 19 deletions.
16 changes: 8 additions & 8 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,16 +706,16 @@ private function getOrmCacheDriverNode($name)
})
->end()
->children()
->scalarNode('type')->defaultValue('array')->end()
->scalarNode('type')->defaultNull()->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
38 changes: 36 additions & 2 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
use const E_USER_DEPRECATED;
use function class_exists;
use function sprintf;
use function trigger_error;

/**
* DoctrineExtension is an extension for the Doctrine DBAL and ORM library.
Expand Down Expand Up @@ -739,20 +741,42 @@ 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),
];
}

$usesDeprecatedCacheType = true;

switch ($driverMap['type']) {
case 'service':
$serviceId = $driverMap['id'];
$usesDeprecatedCacheType = false;
$serviceId = $driverMap['id'];
break;

case 'pool':
$serviceId = $this->createPoolCacheDefinition($container, $driverMap['pool']);
$usesDeprecatedCacheType = false;
$serviceId = $this->createPoolCacheDefinition($container, $driverMap['pool']);
break;

case 'provider':
$serviceId = sprintf('doctrine_cache.providers.%s', $driverMap['cache_provider']);
break;
}

if ($usesDeprecatedCacheType) {
@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 "id" or "pool" types exclusively.',
$driverMap['type'],
$driverName
),
E_USER_DEPRECATED
);
}

if ($serviceId !== null) {
$container->setAlias($aliasId, new Alias($serviceId, false));

Expand Down Expand Up @@ -888,4 +912,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';
}
}
}
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
45 changes: 43 additions & 2 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\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
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 @@ -188,6 +190,9 @@ public function testDbalLoadSavepointsForNestedTransactions()
$this->assertCount(0, $calls);
}

/**
* @group legacy
*/
public function testLoadSimpleSingleConnection()
{
$container = $this->loadContainer('orm_service_simple_single_entity_manager');
Expand Down Expand Up @@ -222,6 +227,8 @@ public function testLoadSimpleSingleConnection()

/**
* The PDO driver doesn't require a database name to be to set when connecting to a database server
*
* @group legacy
*/
public function testLoadSimpleSingleConnectionWithoutDbName()
{
Expand Down Expand Up @@ -258,6 +265,9 @@ public function testLoadSimpleSingleConnectionWithoutDbName()
]);
}

/**
* @group legacy
*/
public function testLoadSingleConnection()
{
$container = $this->loadContainer('orm_service_single_entity_manager');
Expand Down Expand Up @@ -294,6 +304,9 @@ public function testLoadSingleConnection()
$this->assertDICDefinitionMethodCallOnce($configDef, 'setDefaultRepositoryClassName', ['Acme\Doctrine\Repository']);
}

/**
* @group legacy
*/
public function testLoadMultipleConnections()
{
$container = $this->loadContainer('orm_service_multiple_entity_managers');
Expand Down Expand Up @@ -342,10 +355,16 @@ public function testLoadMultipleConnections()
$this->assertEquals('%doctrine_cache.xcache.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 All @@ -368,6 +387,9 @@ public function testLoadLogging()
$this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', [new Reference('doctrine.dbal.logger.chain.both')]);
}

/**
* @group legacy
*/
public function testEntityManagerMetadataCacheDriverConfiguration()
{
$container = $this->loadContainer('orm_service_multiple_entity_managers');
Expand All @@ -379,6 +401,9 @@ public function testEntityManagerMetadataCacheDriverConfiguration()
$this->assertDICDefinitionClass($definition, '%doctrine_cache.apc.class%');
}

/**
* @group legacy
*/
public function testEntityManagerMemcacheMetadataCacheDriverConfiguration()
{
$container = $this->loadContainer('orm_service_simple_single_entity_manager');
Expand All @@ -399,6 +424,9 @@ public function testEntityManagerMemcacheMetadataCacheDriverConfiguration()
]);
}

/**
* @group legacy
*/
public function testEntityManagerRedisMetadataCacheDriverConfigurationWithDatabaseKey()
{
$container = $this->loadContainer('orm_service_simple_single_entity_manager_redis');
Expand All @@ -417,6 +445,9 @@ public function testEntityManagerRedisMetadataCacheDriverConfigurationWithDataba
$this->assertDICDefinitionMethodCallOnce($definition, 'select', [1]);
}

/**
* @group legacy
*/
public function testDependencyInjectionImportsOverrideDefaults()
{
$container = $this->loadContainer('orm_imports');
Expand Down Expand Up @@ -570,6 +601,9 @@ public function testSetQuoteStrategy()
$this->assertDICDefinitionMethodCallOnce($def2, 'setQuoteStrategy', [0 => new Reference('doctrine.orm.quote_strategy.ansi')]);
}

/**
* @group legacy
*/
public function testSecondLevelCache()
{
$container = $this->loadContainer('orm_second_level_cache');
Expand Down Expand Up @@ -990,6 +1024,10 @@ public function testRepositoryFactory()
private function loadContainer($fixture, array $bundles = ['YamlBundle'], CompilerPassInterface $compilerPass = null)
{
$container = $this->getContainer($bundles);
$extension = new FrameworkExtension();
$container->registerExtension($extension);
$extension->load(['framework' => []], $container);

$container->registerExtension(new DoctrineExtension());

$this->loadFromFile($container, $fixture);
Expand Down Expand Up @@ -1019,6 +1057,9 @@ private function getContainer(array $bundles)
'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(),
]));
}

Expand Down
23 changes: 20 additions & 3 deletions Tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\DoctrineProvider;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -320,13 +321,22 @@ public function testDependencyInjectionConfigurationDefaults()
$this->assertEquals('doctrine.orm.default_entity_listener_resolver', (string) $calls[12][1][0]);

$definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.default_metadata_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.system', (string) $arguments[0]);

$definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.default_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.default_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 testUseSavePointsAddMethodCallToAddSavepointsToTheConnection()
Expand Down Expand Up @@ -393,6 +403,9 @@ public function testSingleEntityManagerWithDefaultConfiguration()
]);
}

/**
* @group legacy
*/
public function testSingleEntityManagerWithDefaultSecondLevelCacheConfiguration()
{
$container = $this->getContainer();
Expand All @@ -418,6 +431,9 @@ public function testSingleEntityManagerWithDefaultSecondLevelCacheConfiguration(
$this->assertEquals('%doctrine.orm.second_level_cache.default_cache_factory.class%', $slcDefinition->getClass());
}

/**
* @group legacy
*/
public function testSingleEntityManagerWithCustomSecondLevelCacheConfiguration()
{
$container = $this->getContainer();
Expand Down Expand Up @@ -674,6 +690,7 @@ public function testMessengerIntegration()
/**
* @param array|string $cacheConfig
*
* @group legacy
* @dataProvider cacheConfigurationProvider
*/
public function testCacheConfiguration(string $expectedAliasName, string $expectedAliasTarget, string $cacheName, $cacheConfig) : void
Expand Down
12 changes: 12 additions & 0 deletions Tests/ServiceRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomClassRepoRepository;
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomServiceRepoRepository;
use Fixtures\Bundles\RepositoryServiceBundle\RepositoryServiceBundle;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand Down Expand Up @@ -44,8 +45,19 @@ public function testRepositoryServiceWiring()
'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' => [],
'kernel.charset' => 'UTF-8',
'kernel.container_class' => ContainerBuilder::class,
'kernel.secret' => 'test',
'container.build_id' => uniqid(),
]));
$container->set('annotation_reader', new AnnotationReader());

$extension = new FrameworkExtension();
$container->registerExtension($extension);
$extension->load(['framework' => []], $container);

$extension = new DoctrineExtension();
$container->registerExtension($extension);
$extension->load([[
Expand Down
9 changes: 9 additions & 0 deletions Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\Version;
use PHPUnit\Framework\TestCase as BaseTestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -33,8 +34,16 @@ public function createYamlBundleTestContainer()
'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(),
]));
$container->set('annotation_reader', new AnnotationReader());

$extension = new FrameworkExtension();
$container->registerExtension($extension);
$extension->load(['framework' => []], $container);

$extension = new DoctrineExtension();
$container->registerExtension($extension);
$extension->load([[
Expand Down
11 changes: 11 additions & 0 deletions UPGRADE-1.12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
UPGRADE FROM 1.11 to 1.12
=========================

Deprecation of DoctrineCacheBundle
----------------------------------

With DoctrineCacheBundle [being deprecated](https://github.com/doctrine/DoctrineCacheBundle/issues/156),
configuring caches through it has been deprecated. If you are using anything
other than the `pool` or `id` cache types, please update your configuration to
either use symfony/cache through the `pool` type or configure your cache
services manually and use the `service` type.
Loading

0 comments on commit 4615b7f

Please sign in to comment.