-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Uid] Add Generate and Inspect commands #39883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/Symfony/Component/Uid/Command/GenerateUlidCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Uid\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Uid\Factory\UlidFactory; | ||
|
||
class GenerateUlidCommand extends Command | ||
fancyweb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
protected static $defaultName = 'ulid:generate'; | ||
protected static $defaultDescription = 'Generates a ULID'; | ||
|
||
private $factory; | ||
|
||
public function __construct(UlidFactory $factory = null) | ||
{ | ||
$this->factory = $factory ?? new UlidFactory(); | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this | ||
->setDefinition([ | ||
new InputOption('time', null, InputOption::VALUE_REQUIRED, 'The ULID timestamp: a parsable date/time string'), | ||
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of ULID to generate', 1), | ||
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The ULID output format: base32, base58 or rfc4122', 'base32'), | ||
]) | ||
->setDescription(self::$defaultDescription) | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> command generates a ULID. | ||
|
||
<info>php %command.full_name%</info> | ||
|
||
To specify the timestamp: | ||
|
||
<info>php %command.full_name% --time="2021-02-16 14:09:08"</info> | ||
|
||
To generate several ULIDs: | ||
|
||
<info>php %command.full_name% --count=10</info> | ||
|
||
To output a specific format: | ||
|
||
<info>php %command.full_name% --format=rfc4122</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); | ||
|
||
if (null !== $time = $input->getOption('time')) { | ||
try { | ||
$time = new \DateTimeImmutable($time); | ||
} catch (\Exception $e) { | ||
$io->error(sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage()))); | ||
|
||
return 1; | ||
} | ||
} | ||
|
||
switch ($input->getOption('format')) { | ||
case 'base32': $format = 'toBase32'; break; | ||
case 'base58': $format = 'toBase58'; break; | ||
case 'rfc4122': $format = 'toRfc4122'; break; | ||
default: | ||
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format'))); | ||
|
||
return 1; | ||
} | ||
|
||
$count = (int) $input->getOption('count'); | ||
try { | ||
for ($i = 0; $i < $count; ++$i) { | ||
$output->writeln($this->factory->create($time)->$format()); | ||
} | ||
} catch (\Exception $e) { | ||
$io->error($e->getMessage()); | ||
|
||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
} |
200 changes: 200 additions & 0 deletions
200
src/Symfony/Component/Uid/Command/GenerateUuidCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Uid\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Uid\Factory\UuidFactory; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
class GenerateUuidCommand extends Command | ||
{ | ||
protected static $defaultName = 'uuid:generate'; | ||
protected static $defaultDescription = 'Generates a UUID'; | ||
|
||
private $factory; | ||
|
||
public function __construct(UuidFactory $factory = null) | ||
{ | ||
$this->factory = $factory ?? new UuidFactory(); | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this | ||
->setDefinition([ | ||
new InputOption('time-based', null, InputOption::VALUE_REQUIRED, 'The timestamp, to generate a time-based UUID: a parsable date/time string'), | ||
new InputOption('node', null, InputOption::VALUE_REQUIRED, 'The UUID whose node part should be used as the node of the generated UUID'), | ||
new InputOption('name-based', null, InputOption::VALUE_REQUIRED, 'The name, to generate a name-based UUID'), | ||
new InputOption('namespace', null, InputOption::VALUE_REQUIRED, 'The UUID to use at the namespace for named-based UUIDs'), | ||
new InputOption('random-based', null, InputOption::VALUE_NONE, 'To generate a random-based UUID'), | ||
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of UUID to generate', 1), | ||
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The UUID output format: rfc4122, base58 or base32', 'rfc4122'), | ||
]) | ||
->setDescription(self::$defaultDescription) | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> generates a UUID. | ||
|
||
<info>php %command.full_name%</info> | ||
|
||
To generate a time-based UUID: | ||
|
||
<info>php %command.full_name% --time-based=now</info> | ||
|
||
To specify a time-based UUID's node: | ||
|
||
<info>php %command.full_name% --time-based=@1613480254 --node=fb3502dc-137e-4849-8886-ac90d07f64a7</info> | ||
|
||
To generate a name-based UUID: | ||
|
||
<info>php %command.full_name% --name-based=foo</info> | ||
|
||
To specify a name-based UUID's namespace: | ||
|
||
<info>php %command.full_name% --name-based=bar --namespace=fb3502dc-137e-4849-8886-ac90d07f64a7</info> | ||
|
||
To generate a random-based UUID: | ||
|
||
<info>php %command.full_name% --random-based</info> | ||
|
||
To generate several UUIDs: | ||
|
||
<info>php %command.full_name% --count=10</info> | ||
|
||
To output a specific format: | ||
|
||
<info>php %command.full_name% --format=base58</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); | ||
|
||
$time = $input->getOption('time-based'); | ||
$node = $input->getOption('node'); | ||
$name = $input->getOption('name-based'); | ||
$namespace = $input->getOption('namespace'); | ||
$random = $input->getOption('random-based'); | ||
|
||
if (false !== ($time ?? $name ?? $random) && 1 < ((null !== $time) + (null !== $name) + $random)) { | ||
$io->error('Only one of "--time-based", "--name-based" or "--random-based" can be provided at a time.'); | ||
|
||
return 1; | ||
} | ||
|
||
if (null === $time && null !== $node) { | ||
$io->error('Option "--node" can only be used with "--time-based".'); | ||
|
||
return 1; | ||
} | ||
|
||
if (null === $name && null !== $namespace) { | ||
$io->error('Option "--namespace" can only be used with "--name-based".'); | ||
|
||
return 1; | ||
} | ||
|
||
switch (true) { | ||
case null !== $time: | ||
if (null !== $node) { | ||
try { | ||
$node = Uuid::fromString($node); | ||
} catch (\InvalidArgumentException $e) { | ||
$io->error(sprintf('Invalid node "%s": %s', $node, $e->getMessage())); | ||
|
||
return 1; | ||
} | ||
} | ||
|
||
try { | ||
$time = new \DateTimeImmutable($time); | ||
} catch (\Exception $e) { | ||
$io->error(sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage()))); | ||
|
||
return 1; | ||
} | ||
|
||
$create = function () use ($node, $time): Uuid { | ||
return $this->factory->timeBased($node)->create($time); | ||
}; | ||
break; | ||
|
||
case null !== $name: | ||
if ($namespace) { | ||
try { | ||
$namespace = Uuid::fromString($namespace); | ||
} catch (\InvalidArgumentException $e) { | ||
$io->error(sprintf('Invalid namespace "%s": %s', $namespace, $e->getMessage())); | ||
|
||
return 1; | ||
} | ||
} | ||
|
||
$create = function () use ($namespace, $name): Uuid { | ||
try { | ||
$factory = $this->factory->nameBased($namespace); | ||
} catch (\LogicException $e) { | ||
throw new \InvalidArgumentException('Missing namespace: use the "--namespace" option or configure a default namespace in the underlying factory.'); | ||
} | ||
|
||
return $factory->create($name); | ||
}; | ||
break; | ||
|
||
case $random: | ||
$create = [$this->factory->randomBased(), 'create']; | ||
break; | ||
|
||
default: | ||
$create = [$this->factory, 'create']; | ||
break; | ||
} | ||
|
||
switch ($input->getOption('format')) { | ||
case 'base32': $format = 'toBase32'; break; | ||
case 'base58': $format = 'toBase58'; break; | ||
case 'rfc4122': $format = 'toRfc4122'; break; | ||
default: | ||
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format'))); | ||
|
||
return 1; | ||
} | ||
|
||
$count = (int) $input->getOption('count'); | ||
try { | ||
for ($i = 0; $i < $count; ++$i) { | ||
$output->writeln($create()->$format()); | ||
} | ||
} catch (\Exception $e) { | ||
$io->error($e->getMessage()); | ||
|
||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i feel like we should move the consts to
AbstractUid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
AbstractUid
nor theUid
component wouldn't use them anywhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because the formats are leaking now:
The ULID output format: base32, base58 or rfc4122
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these consts in Serializer are legit.
Then, the duplication in Uid is legit also - these are from different domains.
Also, the commands (on the cli), use inline values.
In the end, these consts in Uid won't serve any purpose for the devs.
They would actually make the public API of the component more complex to me.
I'd prefer not to add them as you might have understood :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think both serializer+console are duplicating what could be
AbstractUid::toFormat($format)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, then I'd recommend splitting this discussion into a separate PR or issue.