Repository: symfony/symfony
File: src/Symfony/Bridge/Doctrine/Middleware/IdleConnection/Listener.php (PHP)

Code before:
 * file that was distributed with this source code.
 */

namespace Symfony\Bridge\Doctrine\Middleware\IdleConnection;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

final class Listener implements EventSubscriberInterface
{
    /**
     * @param \ArrayObject<string, int> $connectionExpiries
     */
    public function __construct(
        private readonly \ArrayObject $connectionExpiries,
        private ContainerInterface $container,
    ) {
    }

    public function onKernelRequest(RequestEvent $event): void
    {
        if (HttpKernelInterface::SUB_REQUEST === $event->getRequestType()) {
            // skip subrequests
            return;
        }
        $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', 192], // before session listeners since they could use the DB
        ];
    }

Review comment (suggestion):
```suggestion
        if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {

```

Code after:
 * file that was distributed with this source code.
 */

namespace Symfony\Bridge\Doctrine\Middleware\IdleConnection;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

final class Listener implements EventSubscriberInterface
{
    /**
     * @param \ArrayObject<string, int> $connectionExpiries
     */
    public function __construct(
        private readonly \ArrayObject $connectionExpiries,
        private ContainerInterface $container,
    ) {
    }

    public function onKernelRequest(RequestEvent $event): void
    {
        if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
            return;
        }
        $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', 192], // before session listeners since they could use the DB
        ];
    }
}