@@ -386,6 +386,116 @@ of the ULID parameters::
386
386
}
387
387
}
388
388
389
+ Generating and Inspecting UUIDs/ULIDs in the Console
390
+ ----------------------------------------------------
391
+
392
+ .. versionadded :: 5.3
393
+
394
+ The commands to inspect and generate UUIDs/ULIDs were introduced in Symfony 5.3.
395
+
396
+ This component provides several commands to generate and inspect UUIDs/ULIDs in
397
+ the console. They are not enabled by default, so you must add the following
398
+ configuration in your application before using these commands:
399
+
400
+ .. configuration-block ::
401
+
402
+ .. code-block :: yaml
403
+
404
+ # config/services.yaml
405
+ services :
406
+ Symfony\Component\Uid\Command\GenerateUlidCommand : ~
407
+ Symfony\Component\Uid\Command\GenerateUuidCommand : ~
408
+ Symfony\Component\Uid\Command\InspectUlidCommand : ~
409
+ Symfony\Component\Uid\Command\InspectUuidCommand : ~
410
+
411
+ .. code-block :: xml
412
+
413
+ <!-- config/services.xml -->
414
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
415
+ <container xmlns =" http://symfony.com/schema/dic/services"
416
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
417
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
418
+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
419
+
420
+ <services >
421
+ <!-- ... -->
422
+
423
+ <service id =" Symfony\Component\Uid\Command\GenerateUlidCommand" />
424
+ <service id =" Symfony\Component\Uid\Command\GenerateUuidCommand" />
425
+ <service id =" Symfony\Component\Uid\Command\InspectUlidCommand" />
426
+ <service id =" Symfony\Component\Uid\Command\InspectUuidCommand" />
427
+ </services >
428
+ </container >
429
+
430
+ .. code-block :: php
431
+
432
+ // config/services.php
433
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
434
+
435
+ use Symfony\Component\Uid\Command\GenerateUlidCommand;
436
+ use Symfony\Component\Uid\Command\GenerateUuidCommand;
437
+ use Symfony\Component\Uid\Command\InspectUlidCommand;
438
+ use Symfony\Component\Uid\Command\InspectUuidCommand;
439
+
440
+ return static function (ContainerConfigurator $configurator): void {
441
+ // ...
442
+
443
+ $services
444
+ ->set(GenerateUlidCommand::class)
445
+ ->set(GenerateUuidCommand::class)
446
+ ->set(InspectUlidCommand::class)
447
+ ->set(InspectUuidCommand::class);
448
+ };
449
+
450
+ Now you can generate UUIDs/ULIDs as follows (add the ``--help `` option to the
451
+ commands to learn about all their options):
452
+
453
+ .. code-block :: terminal
454
+
455
+ # generate 1 random-based UUID
456
+ $ php bin/console uuid:generate --random-based
457
+
458
+ # generate 1 time-based UUID with a specific node
459
+ $ php bin/console uuid:generate --time-based=now --node=fb3502dc-137e-4849-8886-ac90d07f64a7
460
+
461
+ # generate 2 UUIDs and output them in base58 format
462
+ $ php bin/console uuid:generate --count=2 --format=base58
463
+
464
+ # generate 1 ULID with the current time as the timestamp
465
+ $ php bin/console ulid:generate
466
+
467
+ # generate 1 ULID with a specific timestamp
468
+ $ php bin/console ulid:generate --time="2021-02-02 14:00:00"
469
+
470
+ # generate 2 ULIDs and ouput them in RFC4122 format
471
+ $ php bin/console ulid:generate --count=2 --format=rfc4122
472
+
473
+ In addition to generating new UIDs, you can also inspect them with the following
474
+ commands to show all the information for a given UID:
475
+
476
+ .. code-block :: terminal
477
+
478
+ $ php bin/console uuid:inspect d0a3a023-f515-4fe0-915c-575e63693998
479
+ ---------------------- --------------------------------------
480
+ Label Value
481
+ ---------------------- --------------------------------------
482
+ Version 4
483
+ Canonical (RFC 4122) d0a3a023-f515-4fe0-915c-575e63693998
484
+ Base 58 SmHvuofV4GCF7QW543rDD9
485
+ Base 32 6GMEG27X8N9ZG92Q2QBSHPJECR
486
+ ---------------------- --------------------------------------
487
+
488
+ $ php bin/console ulid:inspect 01F2TTCSYK1PDRH73Z41BN1C4X
489
+ --------------------- --------------------------------------
490
+ Label Value
491
+ --------------------- --------------------------------------
492
+ Canonical (Base 32) 01F2TTCSYK1PDRH73Z41BN1C4X
493
+ Base 58 1BYGm16jS4kX3VYCysKKq6
494
+ RFC 4122 0178b5a6-67d3-0d9b-889c-7f205750b09d
495
+ --------------------- --------------------------------------
496
+ Timestamp 2021-04-09 08:01:24.947
497
+ --------------------- --------------------------------------
498
+
389
499
.. _`unique identifiers` : https://en.wikipedia.org/wiki/UID
390
500
.. _`UUIDs` : https://en.wikipedia.org/wiki/Universally_unique_identifier
391
501
.. _`ULIDs` : https://github.com/ulid/spec
0 commit comments