Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5c96c4

Browse files
committed
bug #42011 [Cache] Support decorated Dbal drivers in PdoAdapter (Jeroeny)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Cache] Support decorated Dbal drivers in PdoAdapter | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Explanation in this PR | License | MIT Doctrine v3 supports middleware for Drivers. Upon creating the Connection, `middleware->wrap(Driver): Driver` [is called](https://github.com/doctrine/dbal/blob/3.1.x/src/DriverManager.php#L210), in which the middleware can wrap/decorate the Driver class. So that it can perform tracing [for example](https://github.com/getsentry/sentry-symfony/blob/master/src/Tracing/Doctrine/DBAL/TracingDriverMiddleware.php#L37). When this happens, the Driver class inside the Connection is no longer one of Doctrine's well known Driver classes. The `PdoAdapter ` uses this class to determine the database platform. Which breaks once the Driver is decorated and no longer one of the classes [listed](https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/Cache/Adapter/PdoAdapter.php#L452) in the `PdoAdapter`. Since Dbal exposes this middleware as a feature, I think it would be nice for the `PdoAdapter` to support this. To solve this, the `getDatabasePlatform` can be used. This returns a `Doctrine\DBAL\Platforms\AbstractPlatform` which defines the abstract method `getName`. This returns a value very similar to the list in the `PdoAdapter`. The names don't match exactly, so therefor a small mapping is done to get right the name used in the adapter. As far as a I know, there'd be no other implications with this change. Related: getsentry/sentry-symfony#530 Commits ------- 58d74e3 [Cache] Support decorated Dbal drivers in PdoAdapter
2 parents 9327031 + 58d74e3 commit e5c96c4

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

.github/patch-types.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
case false !== strpos($file = realpath($file), '/vendor/'):
2626
case false !== strpos($file, '/src/Symfony/Bridge/PhpUnit/'):
2727
case false !== strpos($file, '/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Article.php'):
28+
case false !== strpos($file, '/src/Symfony/Component/Cache/Tests/Fixtures/DriverWrapper.php'):
2829
case false !== strpos($file, '/src/Symfony/Component/Config/Tests/Fixtures/BadFileName.php'):
2930
case false !== strpos($file, '/src/Symfony/Component/Config/Tests/Fixtures/BadParent.php'):
3031
case false !== strpos($file, '/src/Symfony/Component/Config/Tests/Fixtures/ParseError.php'):

src/Symfony/Component/Cache/Tests/Adapter/PdoDbalAdapterTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

14+
use Doctrine\DBAL\Configuration;
15+
use Doctrine\DBAL\Driver\Middleware;
1416
use Doctrine\DBAL\DriverManager;
1517
use PHPUnit\Framework\SkippedTestSuiteError;
1618
use Psr\Cache\CacheItemPoolInterface;
1719
use Symfony\Component\Cache\Adapter\PdoAdapter;
20+
use Symfony\Component\Cache\Tests\Fixtures\DriverWrapper;
1821

1922
/**
2023
* @group time-sensitive
@@ -43,4 +46,29 @@ public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterfac
4346
{
4447
return new PdoAdapter(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]), '', $defaultLifetime);
4548
}
49+
50+
public function testConfigureSchemaDecoratedDbalDriver()
51+
{
52+
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]);
53+
if (!interface_exists(Middleware::class)) {
54+
$this->markTestSkipped('doctrine/dbal v2 does not support custom drivers using middleware');
55+
}
56+
57+
$middleware = $this->createMock(Middleware::class);
58+
$middleware
59+
->method('wrap')
60+
->willReturn(new DriverWrapper($connection->getDriver()));
61+
62+
$config = new Configuration();
63+
$config->setMiddlewares([$middleware]);
64+
65+
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $config);
66+
67+
$adapter = new PdoAdapter($connection);
68+
$adapter->createTable();
69+
70+
$item = $adapter->getItem('key');
71+
$item->set('value');
72+
$this->assertTrue($adapter->save($item));
73+
}
4674
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Component\Cache\Tests\Fixtures;
13+
14+
use Doctrine\DBAL\Connection;
15+
use Doctrine\DBAL\Driver;
16+
use Doctrine\DBAL\Platforms\AbstractPlatform;
17+
use Doctrine\DBAL\Schema\AbstractSchemaManager;
18+
19+
class DriverWrapper implements Driver
20+
{
21+
/** @var Driver */
22+
private $driver;
23+
24+
public function __construct(Driver $driver)
25+
{
26+
$this->driver = $driver;
27+
}
28+
29+
public function connect(array $params, $username = null, $password = null, array $driverOptions = []): Driver\Connection
30+
{
31+
return $this->driver->connect($params, $username, $password, $driverOptions);
32+
}
33+
34+
public function getDatabasePlatform(): AbstractPlatform
35+
{
36+
return $this->driver->getDatabasePlatform();
37+
}
38+
39+
public function getSchemaManager(Connection $conn, AbstractPlatform $platform): AbstractSchemaManager
40+
{
41+
return $this->driver->getSchemaManager($conn, $platform);
42+
}
43+
44+
public function getExceptionConverter(): Driver\API\ExceptionConverter
45+
{
46+
return $this->driver->getExceptionConverter();
47+
}
48+
}

src/Symfony/Component/Cache/Traits/PdoTrait.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,15 @@ private function getConnection()
448448
case $driver instanceof \Doctrine\DBAL\Driver\PDO\SQLSrv\Driver:
449449
$this->driver = 'sqlsrv';
450450
break;
451+
case $driver instanceof \Doctrine\DBAL\Driver:
452+
$this->driver = [
453+
'mssql' => 'sqlsrv',
454+
'oracle' => 'oci',
455+
'postgresql' => 'pgsql',
456+
'sqlite' => 'sqlite',
457+
'mysql' => 'mysql',
458+
][$driver->getDatabasePlatform()->getName()] ?? \get_class($driver);
459+
break;
451460
default:
452461
$this->driver = \get_class($driver);
453462
break;

0 commit comments

Comments
 (0)