vendor/symfony/doctrine-bridge/IdGenerator/UuidGenerator.php line 55

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\IdGenerator;
  11. use Doctrine\ORM\EntityManager;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\ORM\Id\AbstractIdGenerator;
  14. use Symfony\Component\Uid\Factory\NameBasedUuidFactory;
  15. use Symfony\Component\Uid\Factory\RandomBasedUuidFactory;
  16. use Symfony\Component\Uid\Factory\TimeBasedUuidFactory;
  17. use Symfony\Component\Uid\Factory\UuidFactory;
  18. use Symfony\Component\Uid\Uuid;
  19. final class UuidGenerator extends AbstractIdGenerator
  20. {
  21.     private UuidFactory $protoFactory;
  22.     private UuidFactory|NameBasedUuidFactory|RandomBasedUuidFactory|TimeBasedUuidFactory $factory;
  23.     private ?string $entityGetter null;
  24.     public function __construct(UuidFactory $factory null)
  25.     {
  26.         $this->protoFactory $this->factory $factory ?? new UuidFactory();
  27.     }
  28.     /**
  29.      * doctrine/orm < 2.11 BC layer.
  30.      */
  31.     public function generate(EntityManager $em$entity): Uuid
  32.     {
  33.         return $this->generateId($em$entity);
  34.     }
  35.     public function generateId(EntityManagerInterface $em$entity): Uuid
  36.     {
  37.         if (null !== $this->entityGetter) {
  38.             if (\is_callable([$entity$this->entityGetter])) {
  39.                 return $this->factory->create($entity->{$this->entityGetter}());
  40.             }
  41.             return $this->factory->create($entity->{$this->entityGetter});
  42.         }
  43.         return $this->factory->create();
  44.     }
  45.     public function nameBased(string $entityGetterUuid|string $namespace null): static
  46.     {
  47.         $clone = clone $this;
  48.         $clone->factory $clone->protoFactory->nameBased($namespace);
  49.         $clone->entityGetter $entityGetter;
  50.         return $clone;
  51.     }
  52.     public function randomBased(): static
  53.     {
  54.         $clone = clone $this;
  55.         $clone->factory $clone->protoFactory->randomBased();
  56.         $clone->entityGetter null;
  57.         return $clone;
  58.     }
  59.     public function timeBased(Uuid|string $node null): static
  60.     {
  61.         $clone = clone $this;
  62.         $clone->factory $clone->protoFactory->timeBased($node);
  63.         $clone->entityGetter null;
  64.         return $clone;
  65.     }
  66. }