vendor/symfony/doctrine-bridge/SchemaListener/MessengerTransportDoctrineSchemaSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Doctrine\SchemaListener;
  11. use Doctrine\Common\EventSubscriber;
  12. use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
  13. use Doctrine\DBAL\Events;
  14. use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
  15. use Doctrine\ORM\Tools\ToolEvents;
  16. use Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineTransport;
  17. use Symfony\Component\Messenger\Transport\TransportInterface;
  18. /**
  19.  * Automatically adds any required database tables to the Doctrine Schema.
  20.  *
  21.  * @author Ryan Weaver <ryan@symfonycasts.com>
  22.  */
  23. final class MessengerTransportDoctrineSchemaSubscriber implements EventSubscriber
  24. {
  25.     private const PROCESSING_TABLE_FLAG self::class.':processing';
  26.     private $transports;
  27.     /**
  28.      * @param iterable<mixed, TransportInterface> $transports
  29.      */
  30.     public function __construct(iterable $transports)
  31.     {
  32.         $this->transports $transports;
  33.     }
  34.     public function postGenerateSchema(GenerateSchemaEventArgs $event): void
  35.     {
  36.         $dbalConnection $event->getEntityManager()->getConnection();
  37.         foreach ($this->transports as $transport) {
  38.             if (!$transport instanceof DoctrineTransport) {
  39.                 continue;
  40.             }
  41.             $transport->configureSchema($event->getSchema(), $dbalConnection);
  42.         }
  43.     }
  44.     public function onSchemaCreateTable(SchemaCreateTableEventArgs $event): void
  45.     {
  46.         $table $event->getTable();
  47.         // if this method triggers a nested create table below, allow Doctrine to work like normal
  48.         if ($table->hasOption(self::PROCESSING_TABLE_FLAG)) {
  49.             return;
  50.         }
  51.         foreach ($this->transports as $transport) {
  52.             if (!$transport instanceof DoctrineTransport) {
  53.                 continue;
  54.             }
  55.             if (!$extraSql $transport->getExtraSetupSqlForTable($table)) {
  56.                 continue;
  57.             }
  58.             // avoid this same listener from creating a loop on this table
  59.             $table->addOption(self::PROCESSING_TABLE_FLAGtrue);
  60.             $createTableSql $event->getPlatform()->getCreateTableSQL($table);
  61.             /*
  62.              * Add all the SQL needed to create the table and tell Doctrine
  63.              * to "preventDefault" so that only our SQL is used. This is
  64.              * the only way to inject some extra SQL.
  65.              */
  66.             $event->addSql($createTableSql);
  67.             foreach ($extraSql as $sql) {
  68.                 $event->addSql($sql);
  69.             }
  70.             $event->preventDefault();
  71.             return;
  72.         }
  73.     }
  74.     public function getSubscribedEvents(): array
  75.     {
  76.         $subscribedEvents = [];
  77.         if (class_exists(ToolEvents::class)) {
  78.             $subscribedEvents[] = ToolEvents::postGenerateSchema;
  79.         }
  80.         if (class_exists(Events::class)) {
  81.             $subscribedEvents[] = Events::onSchemaCreateTable;
  82.         }
  83.         return $subscribedEvents;
  84.     }
  85. }