Skip to content

Commit ec9d4be

Browse files
committed
Replace PdoCacheAdapterDoctrineSchemaSubscriber with DoctrineDbalCacheAdapterSchemaSubscriber
1 parent e79b188 commit ec9d4be

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `DoctrineOpenTransactionLoggerMiddleware` to log when a transaction has been left open
8+
* Deprecate `PdoCacheAdapterDoctrineSchemaSubscriber` and add `DoctrineDbalCacheAdapterSchemaSubscriber` instead
89

910
5.3
1011
---
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\SchemaListener;
13+
14+
use Doctrine\Common\EventSubscriber;
15+
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
16+
use Doctrine\ORM\Tools\ToolEvents;
17+
use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter;
18+
19+
/**
20+
* Automatically adds the cache table needed for the DoctrineDbalAdapter.
21+
*
22+
* @author Ryan Weaver <ryan@symfonycasts.com>
23+
*/
24+
final class DoctrineDbalCacheAdapterSchemaSubscriber implements EventSubscriber
25+
{
26+
private $dbalAdapters;
27+
28+
/**
29+
* @param iterable|DoctrineDbalAdapter[] $dbalAdapters
30+
*/
31+
public function __construct(iterable $dbalAdapters)
32+
{
33+
$this->dbalAdapters = $dbalAdapters;
34+
}
35+
36+
public function postGenerateSchema(GenerateSchemaEventArgs $event): void
37+
{
38+
$dbalConnection = $event->getEntityManager()->getConnection();
39+
foreach ($this->dbalAdapters as $dbalAdapter) {
40+
$dbalAdapter->configureSchema($event->getSchema(), $dbalConnection);
41+
}
42+
}
43+
44+
public function getSubscribedEvents(): array
45+
{
46+
if (!class_exists(ToolEvents::class)) {
47+
return [];
48+
}
49+
50+
return [
51+
ToolEvents::postGenerateSchema,
52+
];
53+
}
54+
}

src/Symfony/Bridge/Doctrine/SchemaListener/PdoCacheAdapterDoctrineSchemaSubscriber.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
use Doctrine\ORM\Tools\ToolEvents;
1717
use Symfony\Component\Cache\Adapter\PdoAdapter;
1818

19+
trigger_deprecation('symfony/doctrine-bridge', '5.4', 'The "%s" class is deprecated, use "%s" instead.', PdoCacheAdapterDoctrineSchemaSubscriber::class, DoctrineDbalCacheAdapterSchemaSubscriber::class);
20+
1921
/**
2022
* Automatically adds the cache table needed for the PdoAdapter.
2123
*
2224
* @author Ryan Weaver <ryan@symfonycasts.com>
25+
* @deprecated since symfony 5.4 use DoctrineDbalCacheAdapterSchemaSubscriber
2326
*/
2427
final class PdoCacheAdapterDoctrineSchemaSubscriber implements EventSubscriber
2528
{
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\SchemaListener;
13+
14+
use Doctrine\DBAL\Connection;
15+
use Doctrine\DBAL\Schema\Schema;
16+
use Doctrine\ORM\EntityManagerInterface;
17+
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Bridge\Doctrine\SchemaListener\DoctrineDbalCacheAdapterSchemaSubscriber;
20+
use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter;
21+
22+
class DoctrineDbalCacheAdapterSchemaSubscriberTest extends TestCase
23+
{
24+
public function testPostGenerateSchema()
25+
{
26+
$schema = new Schema();
27+
$dbalConnection = $this->createMock(Connection::class);
28+
$entityManager = $this->createMock(EntityManagerInterface::class);
29+
$entityManager->expects($this->once())
30+
->method('getConnection')
31+
->willReturn($dbalConnection);
32+
$event = new GenerateSchemaEventArgs($entityManager, $schema);
33+
34+
$pdoAdapter = $this->createMock(DoctrineDbalAdapter::class);
35+
$pdoAdapter->expects($this->once())
36+
->method('configureSchema')
37+
->with($schema, $dbalConnection);
38+
39+
$subscriber = new DoctrineDbalCacheAdapterSchemaSubscriber([$pdoAdapter]);
40+
$subscriber->postGenerateSchema($event);
41+
}
42+
}

src/Symfony/Bridge/Doctrine/Tests/SchemaListener/PdoCacheAdapterDoctrineSchemaSubscriberTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use Symfony\Bridge\Doctrine\SchemaListener\PdoCacheAdapterDoctrineSchemaSubscriber;
2020
use Symfony\Component\Cache\Adapter\PdoAdapter;
2121

22+
/**
23+
* @group legacy
24+
*/
2225
class PdoCacheAdapterDoctrineSchemaSubscriberTest extends TestCase
2326
{
2427
public function testPostGenerateSchema()

0 commit comments

Comments
 (0)