$connectionExpiries
+ */
+ public function __construct(
+ private readonly \ArrayObject $connectionExpiries,
+ private ContainerInterface $container,
+ ) {
+ }
+
+ public function onKernelRequest(RequestEvent $event): void
+ {
+ $timestamp = time();
+
+ foreach ($this->connectionExpiries as $name => $expiry) {
+ if ($timestamp >= $expiry) {
+ // unset before so that we won't retry in case of any failure
+ $this->connectionExpiries->offsetUnset($name);
+
+ try {
+ $connection = $this->container->get("doctrine.dbal.{$name}_connection");
+ $connection->close();
+ } catch (\Exception) {
+ // ignore exceptions to remain fail-safe
+ }
+ }
+ }
+ }
+
+ public static function getSubscribedEvents(): array
+ {
+ return [
+ KernelEvents::REQUEST => 'onKernelRequest',
+ ];
+ }
+}
diff --git a/src/Symfony/Bridge/Doctrine/Tests/Middleware/IdleConnection/DriverTest.php b/src/Symfony/Bridge/Doctrine/Tests/Middleware/IdleConnection/DriverTest.php
new file mode 100644
index 0000000000000..010e1879a8ab4
--- /dev/null
+++ b/src/Symfony/Bridge/Doctrine/Tests/Middleware/IdleConnection/DriverTest.php
@@ -0,0 +1,42 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bridge\Doctrine\Tests\Middleware\IdleConnection;
+
+use Doctrine\DBAL\Driver as DriverInterface;
+use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
+use PHPUnit\Framework\TestCase;
+use Symfony\Bridge\Doctrine\Middleware\IdleConnection\Driver;
+
+class DriverTest extends TestCase
+{
+ /**
+ * @group time-sensitive
+ */
+ public function testConnect()
+ {
+ $driverMock = $this->createMock(DriverInterface::class);
+ $connectionMock = $this->createMock(ConnectionInterface::class);
+
+ $driverMock->expects($this->once())
+ ->method('connect')
+ ->willReturn($connectionMock);
+
+ $connectionExpiries = new \ArrayObject();
+
+ $driver = new Driver($driverMock, $connectionExpiries, 60, 'default');
+ $connection = $driver->connect([]);
+
+ $this->assertSame($connectionMock, $connection);
+ $this->assertArrayHasKey('default', $connectionExpiries);
+ $this->assertSame(time() + 60, $connectionExpiries['default']);
+ }
+}
diff --git a/src/Symfony/Bridge/Doctrine/Tests/Middleware/IdleConnection/ListenerTest.php b/src/Symfony/Bridge/Doctrine/Tests/Middleware/IdleConnection/ListenerTest.php
new file mode 100644
index 0000000000000..099ab48777133
--- /dev/null
+++ b/src/Symfony/Bridge/Doctrine/Tests/Middleware/IdleConnection/ListenerTest.php
@@ -0,0 +1,43 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Middleware\IdleConnection;
+
+use Doctrine\DBAL\Connection as ConnectionInterface;
+use PHPUnit\Framework\TestCase;
+use Symfony\Bridge\Doctrine\Middleware\IdleConnection\Listener;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\HttpKernel\Event\RequestEvent;
+
+class ListenerTest extends TestCase
+{
+ public function testOnKernelRequest()
+ {
+ $containerMock = $this->createMock(ContainerInterface::class);
+ $connectionExpiries = new \ArrayObject(['connectionone' => time() - 30, 'connectiontwo' => time() + 40]);
+
+ $connectionOneMock = $this->getMockBuilder(ConnectionInterface::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $containerMock->expects($this->exactly(1))
+ ->method('get')
+ ->with('doctrine.dbal.connectionone_connection')
+ ->willReturn($connectionOneMock);
+
+ $listener = new Listener($connectionExpiries, $containerMock);
+
+ $listener->onKernelRequest($this->createMock(RequestEvent::class));
+
+ $this->assertArrayNotHasKey('connectionone', (array) $connectionExpiries);
+ $this->assertArrayHasKey('connectiontwo', (array) $connectionExpiries);
+ }
+}
diff --git a/src/Symfony/Bridge/Doctrine/phpunit.xml.dist b/src/Symfony/Bridge/Doctrine/phpunit.xml.dist
index f99086654ecab..0328649ec4f2f 100644
--- a/src/Symfony/Bridge/Doctrine/phpunit.xml.dist
+++ b/src/Symfony/Bridge/Doctrine/phpunit.xml.dist
@@ -33,7 +33,12 @@
- Symfony\Bridge\Doctrine\Middleware\Debug
+
+
+ Symfony\Bridge\Doctrine\Middleware\Debug
+ Symfony\Bridge\Doctrine\Middleware\Debug
+
+
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy