src/InstallBundle/Listener/UpgradeListener.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of SolidInvoice project.
  5.  *
  6.  * (c) Pierre du Plessis <[email protected]>
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace SolidInvoice\InstallBundle\Listener;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use SolidInvoice\CoreBundle\Entity\Version;
  14. use SolidInvoice\CoreBundle\Repository\VersionRepository;
  15. use SolidInvoice\CoreBundle\SolidInvoiceCoreBundle;
  16. use SolidInvoice\InstallBundle\Installer\Database\Migration;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpKernel\Event\RequestEvent;
  19. use Symfony\Component\HttpKernel\HttpKernelInterface;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. /**
  22.  * Listener class to intercept requests and upgrade the database if necessary.
  23.  */
  24. class UpgradeListener implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @return array<string, list<int|string>>
  28.      */
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             KernelEvents::REQUEST => ['onKernelRequest'10],
  33.         ];
  34.     }
  35.     public function __construct(
  36.         private readonly ?string $installed,
  37.         private readonly ManagerRegistry $registry,
  38.         private readonly Migration $migration
  39.     ) {
  40.     }
  41.     public function onKernelRequest(RequestEvent $event): void
  42.     {
  43.         if (null === $this->installed || '' === $this->installed) {
  44.             return;
  45.         }
  46.         if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
  47.             return;
  48.         }
  49.         /** @var VersionRepository $versionRepository */
  50.         $versionRepository $this->registry->getRepository(Version::class);
  51.         if (version_compare($versionRepository->getCurrentVersion(), SolidInvoiceCoreBundle::VERSION'<')) {
  52.             $this->migration->migrate();
  53.         }
  54.     }
  55. }