vendor/doctrine/orm/lib/Doctrine/ORM/Id/SequenceGenerator.php line 54

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM\Id;
  4. use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Serializable;
  7. use function serialize;
  8. use function unserialize;
  9. /**
  10.  * Represents an ID generator that uses a database sequence.
  11.  */
  12. class SequenceGenerator extends AbstractIdGenerator implements Serializable
  13. {
  14.     /**
  15.      * The allocation size of the sequence.
  16.      *
  17.      * @var int
  18.      */
  19.     private $_allocationSize;
  20.     /**
  21.      * The name of the sequence.
  22.      *
  23.      * @var string
  24.      */
  25.     private $_sequenceName;
  26.     /** @var int */
  27.     private $_nextValue 0;
  28.     /** @var int|null */
  29.     private $_maxValue null;
  30.     /**
  31.      * Initializes a new sequence generator.
  32.      *
  33.      * @param string $sequenceName   The name of the sequence.
  34.      * @param int    $allocationSize The allocation size of the sequence.
  35.      */
  36.     public function __construct($sequenceName$allocationSize)
  37.     {
  38.         $this->_sequenceName   $sequenceName;
  39.         $this->_allocationSize $allocationSize;
  40.     }
  41.     /**
  42.      * {@inheritDoc}
  43.      */
  44.     public function generateId(EntityManagerInterface $em$entity)
  45.     {
  46.         if ($this->_maxValue === null || $this->_nextValue === $this->_maxValue) {
  47.             // Allocate new values
  48.             $connection $em->getConnection();
  49.             $sql        $connection->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName);
  50.             if ($connection instanceof PrimaryReadReplicaConnection) {
  51.                 $connection->ensureConnectedToPrimary();
  52.             }
  53.             $this->_nextValue = (int) $connection->fetchOne($sql);
  54.             $this->_maxValue  $this->_nextValue $this->_allocationSize;
  55.         }
  56.         return $this->_nextValue++;
  57.     }
  58.     /**
  59.      * Gets the maximum value of the currently allocated bag of values.
  60.      *
  61.      * @return int|null
  62.      */
  63.     public function getCurrentMaxValue()
  64.     {
  65.         return $this->_maxValue;
  66.     }
  67.     /**
  68.      * Gets the next value that will be returned by generate().
  69.      *
  70.      * @return int
  71.      */
  72.     public function getNextValue()
  73.     {
  74.         return $this->_nextValue;
  75.     }
  76.     /**
  77.      * @return string
  78.      *
  79.      * @final
  80.      */
  81.     public function serialize()
  82.     {
  83.         return serialize($this->__serialize());
  84.     }
  85.     /** @return array<string, mixed> */
  86.     public function __serialize(): array
  87.     {
  88.         return [
  89.             'allocationSize' => $this->_allocationSize,
  90.             'sequenceName' => $this->_sequenceName,
  91.         ];
  92.     }
  93.     /**
  94.      * @param string $serialized
  95.      *
  96.      * @return void
  97.      *
  98.      * @final
  99.      */
  100.     public function unserialize($serialized)
  101.     {
  102.         $this->__unserialize(unserialize($serialized));
  103.     }
  104.     /** @param array<string, mixed> $data */
  105.     public function __unserialize(array $data): void
  106.     {
  107.         $this->_sequenceName   $data['sequenceName'];
  108.         $this->_allocationSize $data['allocationSize'];
  109.     }
  110. }