|
| 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 | +namespaceSymfony\Bridge\Doctrine\Listener; |
| 13 | + |
| 14 | +useDoctrine\DBAL\Connection; |
| 15 | +useDoctrine\DBAL\ExceptionasDBALException; |
| 16 | +useDoctrine\ORM\EntityManagerInterface; |
| 17 | +useDoctrine\Persistence\ManagerRegistry; |
| 18 | +useProxyManager\Proxy\LazyLoadingInterface; |
| 19 | +useSymfony\Bridge\Doctrine\Event\ForceKernelRebootEvent; |
| 20 | +useSymfony\Component\DependencyInjection\ContainerInterface; |
| 21 | +useSymfony\Component\EventDispatcher\EventDispatcherInterface; |
| 22 | +useSymfony\Component\EventDispatcher\EventSubscriberInterface; |
| 23 | +useSymfony\Component\HttpKernel\Event\RequestEvent; |
| 24 | +useSymfony\Component\HttpKernel\KernelEvents; |
| 25 | +useSymfony\Component\VarExporter\LazyObjectInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * Based on https://github.com/Baldinof/roadrunner-bundle/blob/3.x/src/Integration/Doctrine/DoctrineORMMiddleware.php. |
| 29 | + */ |
| 30 | +class DoctrineConnectionListenerimplements EventSubscriberInterface |
| 31 | +{ |
| 32 | +publicfunction__construct(privateManagerRegistry$managerRegistry,privateContainerInterface$container,privateEventDispatcherInterface$eventDispatcher) |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | +publicfunctiononKernelRequest(RequestEvent$event) |
| 37 | + { |
| 38 | +$connectionServices =$this->managerRegistry->getConnectionNames(); |
| 39 | + |
| 40 | +foreach ($connectionServicesas$connectionServiceName) { |
| 41 | +if (!$this->container->initialized($connectionServiceName)) { |
| 42 | +continue; |
| 43 | + } |
| 44 | + |
| 45 | +$connection =$this->container->get($connectionServiceName); |
| 46 | + |
| 47 | +if (!$connectioninstanceof Connection) { |
| 48 | +thrownew \RuntimeException(sprintf('Is not an instance of "%s".', Connection::class)); |
| 49 | + } |
| 50 | + |
| 51 | +if ($connection->isConnected() &&false ===$this->ping($connection)) { |
| 52 | +$connection->close(); |
| 53 | + } |
| 54 | + |
| 55 | +$managerNames =$this->managerRegistry->getManagerNames(); |
| 56 | + |
| 57 | +foreach ($managerNamesas$managerName) { |
| 58 | +if (!$this->container->initialized($managerName)) { |
| 59 | +continue; |
| 60 | + } |
| 61 | + |
| 62 | +$manager =$this->container->get($managerName); |
| 63 | + |
| 64 | +if (!$managerinstanceof EntityManagerInterface) { |
| 65 | +thrownew \RuntimeException(sprintf('Is not an instance of "%s".', EntityManagerInterface::class)); |
| 66 | + } |
| 67 | + |
| 68 | +if ($managerinstanceof LazyLoadingInterface) { |
| 69 | +continue;// Doctrine bundle will handle manager reset on next request |
| 70 | + } |
| 71 | + |
| 72 | +if ($managerinstanceof LazyObjectInterface) { |
| 73 | +continue;// Doctrine bundle will handle manager reset on next request |
| 74 | + } |
| 75 | + |
| 76 | +if (!$manager->isOpen()) { |
| 77 | +$this->eventDispatcher->dispatch(newForceKernelRebootEvent( |
| 78 | +"entity manager '$managerName' is closed and the package `symfony/proxy-manager-bridge` is not installed so kernel reset will not re-open it" |
| 79 | + )); |
| 80 | + |
| 81 | +return; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +privatefunctionping(Connection$con):bool |
| 88 | + { |
| 89 | +try { |
| 90 | +$con->executeQuery($con->getDatabasePlatform()->getDummySelectSQL()); |
| 91 | + |
| 92 | +returntrue; |
| 93 | + }catch (DBALException$e) { |
| 94 | +returnfalse; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +publicstaticfunctiongetSubscribedEvents():array |
| 99 | + { |
| 100 | +return [ |
| 101 | + KernelEvents::REQUEST =>'onKernelRequest', |
| 102 | + ]; |
| 103 | + } |
| 104 | +} |