-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Uid] Add UidFactory to create Ulid and Uuid from timestamps and randomness/nodes #39507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\IdGenerator; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Id\AbstractIdGenerator; | ||
use Symfony\Component\Uid\Factory\UuidFactory; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
final class UuidGenerator extends AbstractIdGenerator | ||
{ | ||
private $protoFactory; | ||
private $factory; | ||
private $entityGetter; | ||
|
||
public function __construct(UuidFactory $factory = null) | ||
{ | ||
$this->protoFactory = $this->factory = $factory ?? new UuidFactory(); | ||
} | ||
|
||
public function generate(EntityManager $em, $entity): Uuid | ||
{ | ||
if (null !== $this->entityGetter) { | ||
if (\is_callable([$entity, $this->entityGetter])) { | ||
return $this->factory->create($entity->{$this->entityGetter}()); | ||
} | ||
|
||
return $this->factory->create($entity->{$this->entityGetter}); | ||
} | ||
|
||
return $this->factory->create(); | ||
} | ||
|
||
/** | ||
* @param Uuid|string|null $namespace | ||
* | ||
* @return static | ||
*/ | ||
public function nameBased(string $entityGetter, $namespace = null): self | ||
{ | ||
$clone = clone $this; | ||
$clone->factory = $clone->protoFactory->nameBased($namespace); | ||
$clone->entityGetter = $entityGetter; | ||
|
||
return $clone; | ||
} | ||
|
||
/** | ||
* @return static | ||
*/ | ||
public function randomBased(): self | ||
{ | ||
$clone = clone $this; | ||
$clone->factory = $clone->protoFactory->randomBased(); | ||
$clone->entityGetter = null; | ||
|
||
return $clone; | ||
} | ||
|
||
/** | ||
* @param Uuid|string|null $node | ||
* | ||
* @return static | ||
*/ | ||
public function timeBased($node = null): self | ||
{ | ||
$clone = clone $this; | ||
$clone->factory = $clone->protoFactory->timeBased($node); | ||
$clone->entityGetter = null; | ||
|
||
return $clone; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/Symfony/Bridge/Doctrine/Tests/IdGenerator/UuidGeneratorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\IdGenerator; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator; | ||
use Symfony\Component\Uid\Factory\UuidFactory; | ||
use Symfony\Component\Uid\NilUuid; | ||
use Symfony\Component\Uid\Uuid; | ||
use Symfony\Component\Uid\UuidV4; | ||
use Symfony\Component\Uid\UuidV6; | ||
|
||
/** | ||
* @requires function \Symfony\Component\Uid\Factory\UuidFactory::create | ||
*/ | ||
class UuidGeneratorTest extends TestCase | ||
{ | ||
public function testUuidCanBeGenerated() | ||
{ | ||
$em = new EntityManager(); | ||
$generator = new UuidGenerator(); | ||
$uuid = $generator->generate($em, new Entity()); | ||
|
||
$this->assertInstanceOf(Uuid::class, $uuid); | ||
} | ||
|
||
public function testCustomUuidfactory() | ||
{ | ||
$uuid = new NilUuid(); | ||
$em = new EntityManager(); | ||
$factory = $this->createMock(UuidFactory::class); | ||
$factory->expects($this->any()) | ||
->method('create') | ||
->willReturn($uuid); | ||
$generator = new UuidGenerator($factory); | ||
|
||
$this->assertSame($uuid, $generator->generate($em, new Entity())); | ||
} | ||
|
||
public function testUuidfactory() | ||
{ | ||
$em = new EntityManager(); | ||
$generator = new UuidGenerator(); | ||
$this->assertInstanceOf(UuidV6::class, $generator->generate($em, new Entity())); | ||
|
||
$generator = $generator->randomBased(); | ||
$this->assertInstanceOf(UuidV4::class, $generator->generate($em, new Entity())); | ||
|
||
$generator = $generator->timeBased(); | ||
$this->assertInstanceOf(UuidV6::class, $generator->generate($em, new Entity())); | ||
|
||
$generator = $generator->nameBased('prop1', Uuid::NAMESPACE_OID); | ||
$this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '3'), $generator->generate($em, new Entity())); | ||
|
||
$generator = $generator->nameBased('prop2', Uuid::NAMESPACE_OID); | ||
$this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '2'), $generator->generate($em, new Entity())); | ||
|
||
$generator = $generator->nameBased('getProp4', Uuid::NAMESPACE_OID); | ||
$this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '4'), $generator->generate($em, new Entity())); | ||
|
||
$factory = new UuidFactory(6, 6, 5, 5, null, Uuid::NAMESPACE_OID); | ||
$generator = new UuidGenerator($factory); | ||
$generator = $generator->nameBased('prop1'); | ||
$this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '3'), $generator->generate($em, new Entity())); | ||
} | ||
} | ||
|
||
class Entity | ||
{ | ||
public $prop1 = 1; | ||
public $prop2 = 2; | ||
|
||
public function prop1() | ||
{ | ||
return 3; | ||
} | ||
|
||
public function getProp4() | ||
{ | ||
return 4; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.