InspectUuidCommand.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Component\Uid\Command;
  11. use Symfony\Component\Console\Attribute\AsCommand;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\TableSeparator;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Style\SymfonyStyle;
  19. use Symfony\Component\Uid\MaxUuid;
  20. use Symfony\Component\Uid\NilUuid;
  21. use Symfony\Component\Uid\TimeBasedUidInterface;
  22. use Symfony\Component\Uid\Uuid;
  23. #[AsCommand(name: 'uuid:inspect', description: 'Inspect a UUID')]
  24. class InspectUuidCommand extends Command
  25. {
  26. protected function configure(): void
  27. {
  28. $this
  29. ->setDefinition([
  30. new InputArgument('uuid', InputArgument::REQUIRED, 'The UUID to inspect'),
  31. ])
  32. ->setHelp(<<<'EOF'
  33. The <info>%command.name%</info> displays information about a UUID.
  34. <info>php %command.full_name% a7613e0a-5986-11eb-a861-2bf05af69e52</info>
  35. <info>php %command.full_name% MfnmaUvvQ1h8B14vTwt6dX</info>
  36. <info>php %command.full_name% 57C4Z0MPC627NTGR9BY1DFD7JJ</info>
  37. EOF
  38. )
  39. ;
  40. }
  41. protected function execute(InputInterface $input, OutputInterface $output): int
  42. {
  43. $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  44. try {
  45. /** @var Uuid $uuid */
  46. $uuid = Uuid::fromString($input->getArgument('uuid'));
  47. } catch (\InvalidArgumentException $e) {
  48. $io->error($e->getMessage());
  49. return 1;
  50. }
  51. if (new NilUuid() == $uuid) {
  52. $version = 'nil';
  53. } elseif (new MaxUuid() == $uuid) {
  54. $version = 'max';
  55. } else {
  56. $version = uuid_type($uuid);
  57. }
  58. $rows = [
  59. ['Version', $version],
  60. ['toRfc4122 (canonical)', (string) $uuid],
  61. ['toBase58', $uuid->toBase58()],
  62. ['toBase32', $uuid->toBase32()],
  63. ['toHex', $uuid->toHex()],
  64. ];
  65. if ($uuid instanceof TimeBasedUidInterface) {
  66. $rows[] = new TableSeparator();
  67. $rows[] = ['Time', $uuid->getDateTime()->format('Y-m-d H:i:s.u \U\T\C')];
  68. }
  69. $io->table(['Label', 'Value'], $rows);
  70. return 0;
  71. }
  72. }