This little library makes it easy to generate short, unique, YouTube-like IDs that you can use instead of UUID in your application.
Using shorter (but still unique) ids will have positive inpact on database performance (especially MySQL, which don't handle UUID well).
This library consists of both generator itself, as well as Doctrine integration.
It allows you to customize length of default generator, as well as to write generators of your own.
Let's look at how many unique values can standard 8 characters long sequence have:
Example id:
9ZGpFadq
8 characters, each can have one of 64 values.
64^8 = 281 474 976 710 656 different values
In the end even UUID might not be unique, it depends on the way you see it. In the end, unless you're using really short ids, it's very close to impossible to get two duplicate strings. I ran multiple tests of generating large amounts of ids (tens of millions) in very short time and couldn't make it to get two duplicate ids.
If you are really afraid of duplicate values, you can extend the key length, but in general this is not neccesary.
Use Composer to install it:
composer require ex3v/random-string-key-generator
$factory = new GeneratorFactory();
$generator = $factory->getGenerator(); //will give you default generator with basic config
$id = $generator->generateId();
Just set up generator annotations (GeneratedValue(strategy="CUSTOM")
and CustomIdGenerator(...)
) on top of field declaration.
/**
* @var string
*
* @ORM\Id()
* @ORM\Column(name="id", type="string")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ex3v\RandomStringKeyGenerator\Doctrine\RandomStringKeyGenerator")
*/
private $id;
This library comes with single generator, that returns 8 characters long strings.
//now default generator will return 12 characters long keys
GeneratorFactory::setDefaultStrategy(UniqIdBasedGenerator::class, [GeneratorInterface::KEY_LENGTH => 12]);
$factory = new GeneratorFactory();
$generator = $factory->getGenerator(YourGeneratorImplementingGeneratorInterface::class, ['generator' => 'options']);
Unfortunately, Doctrine metadata factory restricts from passing any parameters or container parameters/services to ID Generators. Your only option here is to set GeneratorFactory defaults before app initialization. In case of Symfony, the place you're looking for is Kernel class.
Just run
composer test
in this repository, to run the test suite.
Have idea on how to develop it? Found a bug and want to fix it? Pull Requests and Issues are welcome :)