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