Skip to content

[FrameworkBundle] Add framework config for DBAL cache adapter #44065

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

Merged
merged 1 commit into from
Nov 17, 2021
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
1 change: 1 addition & 0 deletions UPGRADE-5.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ FrameworkBundle
* Deprecate the public `profiler` service to private
* Deprecate `get()`, `has()`, `getDoctrine()`, and `dispatchMessage()` in `AbstractController`, use method/constructor injection instead
* Deprecate the `cache.adapter.doctrine` service: The Doctrine Cache library is deprecated. Either switch to Symfony Cache or use the PSR-6 adapters provided by Doctrine Cache.
* In `framework.cache` configuration, using `cache.adapter.pdo` adapter with a Doctrine DBAL connection is deprecated, use `cache.adapter.doctrine_dbal` instead.

HttpKernel
----------
Expand Down
1 change: 1 addition & 0 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ FrameworkBundle
* Remove the `AdapterInterface` autowiring alias, use `CacheItemPoolInterface` instead
* Remove `get()`, `has()`, `getDoctrine()`, and `dispatchMessage()` in `AbstractController`, use method/constructor injection instead
* Deprecate the `cache.adapter.doctrine` service: The Doctrine Cache library is deprecated. Either switch to Symfony Cache or use the PSR-6 adapters provided by Doctrine Cache.
* In `framework.cache` configuration, using the `cache.adapter.pdo` with a Doctrine DBAL connection is no longer supported, use `cache.adapter.doctrine_dbal` instead.

HttpFoundation
--------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
use Doctrine\ORM\Tools\ToolEvents;
use Symfony\Component\Cache\Adapter\DoctrineSchemaConfiguratorInterface;
use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter;

/**
* Automatically adds the cache table needed for the DoctrineDbalAdapter of
Expand All @@ -27,7 +27,7 @@ final class DoctrineDbalCacheAdapterSchemaSubscriber implements EventSubscriber
private $dbalAdapters;

/**
* @param iterable<mixed, DoctrineSchemaConfiguratorInterface> $dbalAdapters
* @param iterable<mixed, DoctrineDbalAdapter> $dbalAdapters
*/
public function __construct(iterable $dbalAdapters)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use Doctrine\ORM\Tools\ToolEvents;
use Symfony\Component\Cache\Adapter\PdoAdapter;

trigger_deprecation('symfony/doctrine-bridge', '5.4', 'The "%s" class is deprecated, use "%s" instead.', PdoCacheAdapterDoctrineSchemaSubscriber::class, DoctrineDbalCacheAdapterSchemaSubscriber::class);

/**
* Automatically adds the cache table needed for the PdoAdapter.
*
Expand All @@ -41,6 +39,10 @@ public function postGenerateSchema(GenerateSchemaEventArgs $event): void
{
$dbalConnection = $event->getEntityManager()->getConnection();
foreach ($this->pdoAdapters as $pdoAdapter) {
if (PdoAdapter::class !== \get_class($pdoAdapter)) {
trigger_deprecation('symfony/doctrine-bridge', '5.4', 'The "%s" class is deprecated, use "%s" instead.', self::class, DoctrineDbalCacheAdapterSchemaSubscriber::class);
}

$pdoAdapter->configureSchema($event->getSchema(), $dbalConnection);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\SchemaListener\DoctrineDbalCacheAdapterSchemaSubscriber;
use Symfony\Component\Cache\Adapter\DoctrineSchemaConfiguratorInterface;
use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter;

class DoctrineDbalCacheAdapterSchemaSubscriberTest extends TestCase
{
Expand All @@ -29,14 +29,15 @@ public function testPostGenerateSchema()
$entityManager->expects($this->once())
->method('getConnection')
->willReturn($dbalConnection);

$event = new GenerateSchemaEventArgs($entityManager, $schema);

$pdoAdapter = $this->createMock(DoctrineSchemaConfiguratorInterface::class);
$pdoAdapter->expects($this->once())
$dbalAdapter = $this->createMock(DoctrineDbalAdapter::class);
$dbalAdapter->expects($this->once())
->method('configureSchema')
->with($schema, $dbalConnection);

$subscriber = new DoctrineDbalCacheAdapterSchemaSubscriber([$pdoAdapter]);
$subscriber = new DoctrineDbalCacheAdapterSchemaSubscriber([$dbalAdapter]);
$subscriber->postGenerateSchema($event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,33 @@
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\SchemaListener\PdoCacheAdapterDoctrineSchemaSubscriber;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Cache\Adapter\PdoAdapter;

/**
* @group legacy
*/
class PdoCacheAdapterDoctrineSchemaSubscriberTest extends TestCase
{
use ExpectDeprecationTrait;

public function testPostGenerateSchema()
{
$schema = new Schema();
$dbalConnection = $this->createMock(Connection::class);
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->expects($this->once())
$entityManager->expects($this->any())
->method('getConnection')
->willReturn($dbalConnection);

$event = new GenerateSchemaEventArgs($entityManager, $schema);

$pdoAdapter = $this->createMock(PdoAdapter::class);
$pdoAdapter->expects($this->once())
->method('configureSchema')
->with($schema, $dbalConnection);
->with($event->getSchema(), $event->getEntityManager()->getConnection());

$this->expectDeprecation('Since symfony/doctrine-bridge 5.4: The "Symfony\Bridge\Doctrine\SchemaListener\PdoCacheAdapterDoctrineSchemaSubscriber" class is deprecated, use "Symfony\Bridge\Doctrine\SchemaListener\DoctrineDbalCacheAdapterSchemaSubscriber" instead.');

$subscriber = new PdoCacheAdapterDoctrineSchemaSubscriber([$pdoAdapter]);
$subscriber->postGenerateSchema($event);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ CHANGELOG
* Add support for `statusCode` default parameter when loading a template directly from route using the `Symfony\Bundle\FrameworkBundle\Controller\TemplateController` controller
* Deprecate `translation:update` command, use `translation:extract` instead
* Add `PhpStanExtractor` support for the PropertyInfo component
* Add `cache.adapter.doctrine_dbal` service to replace `cache.adapter.pdo` when a Doctrine DBAL connection is used.

5.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,7 @@ private function addCacheSection(ArrayNodeDefinition $rootNode, callable $willBe
->scalarNode('default_psr6_provider')->end()
->scalarNode('default_redis_provider')->defaultValue('redis://localhost')->end()
->scalarNode('default_memcached_provider')->defaultValue('memcached://localhost')->end()
->scalarNode('default_doctrine_dbal_provider')->defaultValue('database_connection')->end()
->scalarNode('default_pdo_provider')->defaultValue($willBeAvailable('doctrine/dbal', Connection::class) ? 'database_connection' : null)->end()
->arrayNode('pools')
->useAttributeAsKey('name')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\ChainAdapter;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
Expand Down Expand Up @@ -2159,6 +2161,14 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
$container->removeDefinition('cache.default_marshaller');
}

if (!class_exists(DoctrineAdapter::class)) {
$container->removeDefinition('cache.adapter.doctrine');
}

if (!class_exists(DoctrineDbalAdapter::class)) {
$container->removeDefinition('cache.adapter.doctrine_dbal');
}

$version = new Parameter('container.build_id');
$container->getDefinition('cache.adapter.apcu')->replaceArgument(2, $version);
$container->getDefinition('cache.adapter.system')->replaceArgument(2, $version);
Expand All @@ -2171,7 +2181,7 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
// Inline any env vars referenced in the parameter
$container->setParameter('cache.prefix.seed', $container->resolveEnvPlaceholders($container->getParameter('cache.prefix.seed'), true));
}
foreach (['doctrine', 'psr6', 'redis', 'memcached', 'pdo'] as $name) {
foreach (['doctrine', 'psr6', 'redis', 'memcached', 'doctrine_dbal', 'pdo'] as $name) {
if (isset($config[$name = 'default_'.$name.'_provider'])) {
$container->setAlias('cache.'.$name, new Alias(CachePoolPass::getServiceProvider($container, $config[$name]), false));
}
Expand Down
27 changes: 20 additions & 7 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use Symfony\Component\Cache\Adapter\PdoAdapter;
Expand Down Expand Up @@ -93,10 +94,8 @@
->call('setLogger', [service('logger')->ignoreOnInvalid()])
->tag('cache.pool', ['clearer' => 'cache.default_clearer', 'reset' => 'reset'])
->tag('monolog.logger', ['channel' => 'cache'])
;

if (class_exists(DoctrineAdapter::class)) {
$container->services()->set('cache.adapter.doctrine', DoctrineAdapter::class)
->set('cache.adapter.doctrine', DoctrineAdapter::class)
->abstract()
->args([
abstract_arg('Doctrine provider service'),
Expand All @@ -110,11 +109,8 @@
'reset' => 'reset',
])
->tag('monolog.logger', ['channel' => 'cache'])
->deprecate('symfony/framework-bundle', '5.4', 'The abstract service "%service_id%" is deprecated.')
;
}
->deprecate('symfony/framework-bundle', '5.4', 'The "%service_id%" service inherits from "cache.adapter.doctrine" which is deprecated.')

$container->services()
->set('cache.adapter.filesystem', FilesystemAdapter::class)
->abstract()
->args([
Expand Down Expand Up @@ -188,6 +184,23 @@
])
->tag('monolog.logger', ['channel' => 'cache'])

->set('cache.adapter.doctrine_dbal', DoctrineDbalAdapter::class)
->abstract()
->args([
abstract_arg('DBAL connection service'),
'', // namespace
0, // default lifetime
[], // table options
service('cache.default_marshaller')->ignoreOnInvalid(),
])
->call('setLogger', [service('logger')->ignoreOnInvalid()])
->tag('cache.pool', [
'provider' => 'cache.default_doctrine_dbal_provider',
'clearer' => 'cache.default_clearer',
'reset' => 'reset',
])
->tag('monolog.logger', ['channel' => 'cache'])

->set('cache.adapter.pdo', PdoAdapter::class)
->abstract()
->args([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ protected static function getBundleDefaultConfig()
'directory' => '%kernel.cache_dir%/pools/app',
'default_redis_provider' => 'redis://localhost',
'default_memcached_provider' => 'memcached://localhost',
'default_doctrine_dbal_provider' => 'database_connection',
'default_pdo_provider' => ContainerBuilder::willBeAvailable('doctrine/dbal', Connection::class, ['symfony/framework-bundle']) ? 'database_connection' : null,
'prefix_seed' => '_%kernel.project_dir%.%kernel.container_class%',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
use Symfony\Component\Cache\PruneableInterface;

final class DoctrineDbalAdapter extends AbstractAdapter implements PruneableInterface, DoctrineSchemaConfiguratorInterface
class DoctrineDbalAdapter extends AbstractAdapter implements PruneableInterface
{
protected $maxIdLength = 255;

Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion src/Symfony/Component/Cache/LockRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ final class LockRegistry
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'CouchbaseCollectionAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'DoctrineAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'DoctrineDbalAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'DoctrineSchemaConfiguratorInterface.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'FilesystemAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'FilesystemTagAwareAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'MemcachedAdapter.php',
Expand Down