8000 [Uid] Add Generate and Inspect commands · symfony/symfony@b9b5b7b · GitHub
[go: up one dir, main page]

Skip to content

Commit b9b5b7b

Browse files
committed
[Uid] Add Generate and Inspect commands
1 parent 6ae59a9 commit b9b5b7b

10 files changed

+1013
-0
lines changed

src/Symfony/Component/Uid/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* [BC BREAK] Replace `UuidV1::getTime()`, `UuidV6::getTime()` and `Ulid::getTime()` by `UuidV1::getDateTime()`, `UuidV6::getDateTime()` and `Ulid::getDateTime()`
1010
* Add `Uuid::NAMESPACE_*` constants from RFC4122
1111
* Add `UlidFactory`, `UuidFactory`, `RandomBasedUuidFactory`, `TimeBasedUuidFactory` and `NameBasedUuidFactory`
12+
* Add commands to generate and inspect UUIDs and ULIDs
1213

1314
5.2.0
1415
-----
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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\Command\Command;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Input\InputOption;
17+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Style\SymfonyStyle;
20+
use Symfony\Component\Uid\Factory\UlidFactory;
21+
use Symfony\Component\Uid\Ulid;
22+
23+
class GenerateUlidCommand extends Command
24+
{
25+
protected static $defaultName = 'ulid:generate';
26+
protected static $defaultDescription = 'Generates a ULID';
27+
28+
private $factory;
29+
30+
public function __construct(UlidFactory $factory = null)
31+
{
32+
$this->factory = $factory ?? new UlidFactory();
33+
34+
parent::__construct();
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected function configure(): void
41+
{
42+
$this
43+
->setDefinition([
44+
new InputOption('timestamp', null, InputOption::VALUE_REQUIRED, 'The ULID timestamp: a parsable date/time string by the \DateTimeImmutable constructor. It must be greater than or equals to the UNIX epoch (1970-01-01 00:00:00)'),
45+
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of ULID to generate', 1),
46+
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The ULID output format: canonical, base_58 or rfc_4122', 'canonical'),
47+
])
48+
->setDescription(self::$defaultDescription)
49+
->setHelp(<<<'EOF'
50+
The <info>%command.name%</info> command generates a ULID.
51+
52+
<info>php %command.full_name%</info>
53+
54+
To specify the timestamp:
55+
56+
<info>php %command.full_name% --timestamp="2021-02-16 14:09:08"</info>
57+
58+
To generate several ULIDs:
59+
60+
<info>php %command.full_name% --count=10</info>
61+
62+
To output a specific format:
63+
64+
<info>php %command.full_name% --format=rfc_4122</info>
65+
EOF
66+
)
67+
;
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
protected function execute(InputInterface $input, OutputInterface $output)
74+
{
75+
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
76+
77+
if (null !== $time = $input->getOption('timestamp')) {
78+
try {
79+
$time = new \DateTimeImmutable($time);
80+
} catch (\Exception $e) {
81+
$io->error(sprintf('Invalid timestamp "%s". It cannot be parsed.', $time));
82+
83+
return 1;
84+
}
85+
86+
if (0 > $time->format('U')) {
87+
$io->error(sprintf('Invalid timestamp "%s". It must be greater than or equals to the UNIX epoch (1970-01-01 00:00:00).', $input->getOption('timestamp')));
88+
89+
return 2;
90+
}
91+
}
92+
93+
switch ($input->getOption('format')) {
94+
case 'canonical':
95+
$format = 'strval';
96+
97+
break;
98+
case 'base_58':
99+
$format = static function (Ulid $ulid): string { return $ulid->toBase58(); };
100+
101+
break;
102+
case 'rfc_4122':
103+
$format = static function (Ulid $ulid): string { return $ulid->toRfc4122(); };
104+
105+
break;
106+
default:
107+
$io->error(sprintf('Invalid format "%s". Supported formats are canonical, base_58 and rfc_4122.', $input->getOption('format')));
108+
109+
return 3;
110+
}
111+
112+
$count = (int) $input->getOption('count');
113+
for ($i = 0; $i < $count; ++$i) {
114+
$output->writeln($format($this->factory->create($time)));
115+
}
116+
117+
return 0;
118+
}
119+
}
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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\Command\Command;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Input\InputOption;
17+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Style\SymfonyStyle;
20+
use Symfony\Component\Uid\Factory\UuidFactory;
21+
use Symfony\Component\Uid\Uuid;
22+
23+
class GenerateUuidCommand extends Command
24+
{
25+
protected static $defaultName = 'uuid:generate';
26+
protected static $defaultDescription = 'Generates a UUID';
27+
28+
private $factory;
29+
30+
public function __construct(UuidFactory $factory = null)
31+
{
32+
$this->factory = $factory ?? new UuidFactory();
33+
34+
parent::__construct();
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected function configure(): void
41+
{
42+
$this
43+
->setDefinition([
44+
new InputOption('time-based', null, InputOption::VALUE_REQUIRED, 'The timestamp, to generate a time based UUID: a parsable date/time string by the \DateTimeImmutable constructor. It must be greater than or equals to the UUID epoch (1582-10-15 00:00:00)'),
45+
new InputOption('node', null, InputOption::VALUE_REQUIRED, "The time based UUID's node: a UUID, in any of the supported formats (base 58, base 32 or RFC 4122)"),
46+
new InputOption('name-based', null, InputOption::VALUE_REQUIRED, 'The name, to generate a name based UUID'),
47+
new InputOption('namespace', null, InputOption::VALUE_REQUIRED, "The name based UUID's namespace: a UUID, in any of the supported formats (base 58, base 32 or RFC 4122)"),
48+
new InputOption('random-based', null, InputOption::VALUE_NONE, 'To generate a random based UUID'),
49+
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of UUID to generate', 1),
50+
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The UUID output format: canonical, base_58 or base_32', 'canonical'),
51+
])
52+
->setDescription(self::$defaultDescription)
53+
->setHelp(<<<'EOF'
54+
The <info>%command.name%</info> generates a UUID.
55+
56+
<info>php %command.full_name%</info>
57+
58+
To generate a time based UUID:
59+
60+
<info>php %command.full_name% --time-based=now</info>
61+
62+
To specify a time based UUID's node:
63+
64+
<info>php %command.full_name% --time-based=@1613480254 --node=fb3502dc-137e-4849-8886-ac90d07f64a7</info>
65+
66+
To generate a name based UUID:
67+
68+
<info>php %command.full_name% --name-based=foo</info>
69+
70+
To specify a name based UUID's namespace:
71+
72+
<info>php %command.full_name% --name-based=bar --namespace=fb3502dc-137e-4849-8886-ac90d07f64a7</info>
73+
74+
To generate a random based UUID:
75+
76+
<info>php %command.full_name% --random-based</info>
77+
78+
To generate several UUIDs:
79+
80+
<info>php %command.full_name% --count=10</info>
81+
82+
To output a specific format:
83+
84+
<info>php %command.full_name% --format=base_58</info>
85+
EOF
86+
)
87+
;
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
protected function execute(InputInterface $input, OutputInterface $output)
94+
{
95+
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
96+
97+
$time = $input->getOption('time-based');
98+
$node = $input->getOption('node');
99+
$name = $input->getOption('name-based');
100+
$namespace = $input->getOption('namespace');
101+
$random = $input->getOption('random-based');
102+
103+
switch (true) {
104+
case !$time && !$node && !$name && !$namespace && !$random:
105+
$create = [$this->factory, 'create'];
106+
107+
break;
108+
case $time && !$name && !$namespace && !$random:
109+
if ($node) {
110+
try {
111+
$node = Uuid::fromString($node);
112+
} catch (\InvalidArgumentException $e) {
113+
$io->error(sprintf('Invalid node "%s". It cannot be parsed.', $node));
114+
115+
return 1;
116+
}
117+
}
118+
119+
try {
120+
$time = new \DateTimeImmutable($time);
121+
} catch (\Exception $e) {
122+
$io->error(sprintf('Invalid timestamp "%s". It cannot be parsed.', $input->getOption('time-based')));
123+
124+
return 2;
125+
}
126+
127+
if ($time < new \DateTimeImmutable('@-12219292800')) {
128+
$io->error(sprintf('Invalid timestamp "%s". It must be greater than or equals to the UUID epoch (1582-10-15 00:00:00).', $input->getOption('time-based')));
129+
130+
return 3;
131+
}
132+
133+
$create = function () use ($node, $time): Uuid { return $this->factory->timeBased($node)->create($time); };
134+
135+
break;
136+
case $name && !$time && !$node && !$random:
137+
if ($namespace) {
138+
try {
139+
$namespace = Uuid::fromString($namespace);
140+
} catch (\InvalidArgumentException $e) {
141+
$io->error(sprintf('Invalid namespace "%s". It cannot be parsed.', $namespace));
142+
143+
return 4;
144+
}
145+
} else {
146+
$refl = new \ReflectionProperty($this->factory, 'nameBasedNamespace');
147+
$refl->setAccessible(true);
148+
if (null === $refl->getValue($this->factory)) {
149+
$io->error('Missing namespace. Use the "--namespace" option or configure a default namespace in the underlying factory.');
150+
151+
return 5;
152+
}
153+
}
154+
155+
$create = function () use ($namespace, $name): Uuid { return $this->factory->nameBased($namespace)->create($name); };
156+
157+
break;
158+
case $random && !$time && !$node && !$name && !$namespace:
159+
$create = [$this->factory->randomBased(), 'create'];
160+
161+
break;
162+
default:
163+
$io->error('Invalid combination of options.');
164+
165+
return 6;
166+
}
167+
168+
switch ($input->getOption('format')) {
169+
case 'canonical':
170+
$format = 'strval';
171+
172+
break;
173+
case 'base_58':
174+
$format = static function (Uuid $uuid): string { return $uuid->toBase58(); };
175+
176+
break;
177+
case 'base_32':
178+
$format = static function (Uuid $uuid): string { return $uuid->toBase32(); };
179+
180+
break;
181+
default:
182+
$io->error(sprintf('Invalid format "%s". Supported formats are canonical, base_58 and base_32.', $input->getOption('format')));
183+
184+
return 7;
185+
}
186+
187+
$count = (int) $input->getOption('count');
188+
for ($i = 0; $i < $count; ++$i) {
189+
$io->writeln($format($create()));
190+
}
191+
192+
return 0;
193+
}
194+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\Command\Command;
15+
use Symfony\Component\Console\Helper\TableSeparator;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Style\SymfonyStyle;
21+
use Symfony\Component\Uid\Ulid;
22+
23+
class InspectUlidCommand extends Command
24+
{
25+
protected static $defaultName = 'ulid:inspect';
26+
protected static $defaultDescription = 'Inspects a ULID';
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function configure(): void
32+
{
33+
$this
34+
->setDefinition([
35+
new InputArgument('ulid', InputArgument::REQUIRED, 'The ULID to inspect, in any of the supported formats (base 58, base 32 or RFC 4122)'),
36+
])
37+
->setDescription(self::$defaultDescription)
38+
->setHelp(<<<'EOF'
39+
The <info>%command.name%</info> display information about a ULID.
40+
41+
<info>php %command.full_name% 01EWAKBCMWQ2C94EXNN60ZBS0Q</info>
42+
<info>php %command.full_name% 1BVdfLn3ERmbjYBLCdaaLW</info>
43+
<info>php %command.full_name% 01771535-b29c-b898-923b-b5a981f5e417</info>
44+
EOF
45+
)
46+
;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
protected function execute(InputInterface $input, OutputInterface $output)
53+
{
54+
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
55+
56+
try {
57+
/** @var Ulid $ulid */
58+
$ulid = Ulid::fromString($input->getArgument('ulid'));
59+
} catch (\InvalidArgumentException $e) {
60+
$io->error(sprintf('Invalid ULID "%s".', $input->getArgument('ulid')));
61+
62+
return 1;
63+
}
64+
65+
$io->table(['Label', 'Value'], [
66+
['Canonical (Base 32)', (string) $ulid],
67+
['Base 58', $ulid->toBase58()],
68+
['RFC 4122', $ulid->toRfc4122()],
69+
new TableSeparator(),
70+
['Timestamp', ($ulid->getDateTime())->format('Y-m-d H:i:s.v')],
71+
]);
72+
73+
return 0;
74+
}
75+
}

0 commit comments

Comments
 (0)
0