| 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\Ulid;
|
|
|
23 |
|
|
|
24 |
#[AsCommand(name: 'ulid:inspect', description: 'Inspect a ULID')]
|
|
|
25 |
class InspectUlidCommand extends Command
|
|
|
26 |
{
|
|
|
27 |
protected function configure(): void
|
|
|
28 |
{
|
|
|
29 |
$this
|
|
|
30 |
->setDefinition([
|
|
|
31 |
new InputArgument('ulid', InputArgument::REQUIRED, 'The ULID to inspect'),
|
|
|
32 |
])
|
|
|
33 |
->setHelp(<<<'EOF'
|
|
|
34 |
The <info>%command.name%</info> displays information about a ULID.
|
|
|
35 |
|
|
|
36 |
<info>php %command.full_name% 01EWAKBCMWQ2C94EXNN60ZBS0Q</info>
|
|
|
37 |
<info>php %command.full_name% 1BVdfLn3ERmbjYBLCdaaLW</info>
|
|
|
38 |
<info>php %command.full_name% 01771535-b29c-b898-923b-b5a981f5e417</info>
|
|
|
39 |
EOF
|
|
|
40 |
)
|
|
|
41 |
;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
45 |
{
|
|
|
46 |
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
|
|
|
47 |
|
|
|
48 |
try {
|
|
|
49 |
$ulid = Ulid::fromString($input->getArgument('ulid'));
|
|
|
50 |
} catch (\InvalidArgumentException $e) {
|
|
|
51 |
$io->error($e->getMessage());
|
|
|
52 |
|
|
|
53 |
return 1;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
$io->table(['Label', 'Value'], [
|
|
|
57 |
['toBase32 (canonical)', (string) $ulid],
|
|
|
58 |
['toBase58', $ulid->toBase58()],
|
|
|
59 |
['toRfc4122', $ulid->toRfc4122()],
|
|
|
60 |
['toHex', $ulid->toHex()],
|
|
|
61 |
new TableSeparator(),
|
|
|
62 |
['Time', $ulid->getDateTime()->format('Y-m-d H:i:s.v \U\T\C')],
|
|
|
63 |
]);
|
|
|
64 |
|
|
|
65 |
return 0;
|
|
|
66 |
}
|
|
|
67 |
}
|