| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
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 |
/** @var Uuid $uuid */
|
|
|
53 |
$uuid = Uuid::fromString($input->getArgument('uuid'));
|
|
|
54 |
} catch (\InvalidArgumentException $e) {
|
|
|
55 |
$io->error($e->getMessage());
|
|
|
56 |
|
|
|
57 |
return 1;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
if (new NilUuid() == $uuid) {
|
|
|
61 |
$version = 'nil';
|
|
|
62 |
} elseif (new MaxUuid() == $uuid) {
|
|
|
63 |
$version = 'max';
|
|
|
64 |
} else {
|
|
|
65 |
$version = uuid_type($uuid);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
$rows = [
|
|
|
69 |
['Version', $version],
|
|
|
70 |
['toRfc4122 (canonical)', (string) $uuid],
|
|
|
71 |
['toBase58', $uuid->toBase58()],
|
|
|
72 |
['toBase32', $uuid->toBase32()],
|
|
|
73 |
['toHex', $uuid->toHex()],
|
|
|
74 |
];
|
|
|
75 |
|
|
|
76 |
if ($uuid instanceof TimeBasedUidInterface) {
|
|
|
77 |
$rows[] = new TableSeparator();
|
|
|
78 |
$rows[] = ['Time', $uuid->getDateTime()->format('Y-m-d H:i:s.u \U\T\C')];
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
$io->table(['Label', 'Value'], $rows);
|
|
|
82 |
|
|
|
83 |
return 0;
|
|
|
84 |
}
|
|
|
85 |
}
|