| 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\Completion\CompletionInput; |
| 17 |
use Symfony\Component\Console\Completion\CompletionSuggestions; |
| 18 |
use Symfony\Component\Console\Input\InputInterface; |
| 19 |
use Symfony\Component\Console\Input\InputOption; |
| 20 |
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
| 21 |
use Symfony\Component\Console\Output\OutputInterface; |
| 22 |
use Symfony\Component\Console\Style\SymfonyStyle; |
| 23 |
use Symfony\Component\Uid\Exception\LogicException; |
| 24 |
use Symfony\Component\Uid\Factory\UuidFactory; |
| 25 |
use Symfony\Component\Uid\Uuid; |
| 26 |
|
| 27 |
#[AsCommand(name: 'uuid:generate', description: 'Generate a UUID')] |
| 28 |
class GenerateUuidCommand extends Command |
| 29 |
{ |
| 30 |
public function __construct( |
| 31 |
private UuidFactory $factory = new UuidFactory(), |
| 32 |
) { |
| 33 |
parent::__construct(); |
| 34 |
} |
| 35 |
|
| 36 |
protected function configure(): void |
| 37 |
{ |
| 38 |
$this |
| 39 |
->setDefinition([ |
| 40 |
new InputOption('time-based', null, InputOption::VALUE_REQUIRED, 'The timestamp, to generate a time-based UUID: a parsable date/time string'), |
| 41 |
new InputOption('node', null, InputOption::VALUE_REQUIRED, 'The UUID whose node part should be used as the node of the generated UUID'), |
| 42 |
new InputOption('name-based', null, InputOption::VALUE_REQUIRED, 'The name, to generate a name-based UUID'), |
| 43 |
new InputOption('namespace', null, InputOption::VALUE_REQUIRED, 'The UUID to use at the namespace for named-based UUIDs, predefined namespaces keywords "dns", "url", "oid" and "x500" are accepted'), |
| 44 |
new InputOption('random-based', null, InputOption::VALUE_NONE, 'To generate a random-based UUID'), |
| 45 |
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of UUID to generate', 1), |
| 46 |
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, \sprintf('The UUID output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'rfc4122'), |
| 47 |
]) |
| 48 |
->setHelp(<<<'EOF' |
| 49 |
The <info>%command.name%</info> generates a UUID. |
| 50 |
|
| 51 |
<info>php %command.full_name%</info> |
| 52 |
|
| 53 |
To generate a time-based UUID: |
| 54 |
|
| 55 |
<info>php %command.full_name% --time-based=now</info> |
| 56 |
|
| 57 |
To specify a time-based UUID's node: |
| 58 |
|
| 59 |
<info>php %command.full_name% --time-based=@1613480254 --node=fb3502dc-137e-4849-8886-ac90d07f64a7</info> |
| 60 |
|
| 61 |
To generate a name-based UUID: |
| 62 |
|
| 63 |
<info>php %command.full_name% --name-based=foo</info> |
| 64 |
|
| 65 |
To specify a name-based UUID's namespace: |
| 66 |
|
| 67 |
<info>php %command.full_name% --name-based=bar --namespace=fb3502dc-137e-4849-8886-ac90d07f64a7</info> |
| 68 |
|
| 69 |
To generate a random-based UUID: |
| 70 |
|
| 71 |
<info>php %command.full_name% --random-based</info> |
| 72 |
|
| 73 |
To generate several UUIDs: |
| 74 |
|
| 75 |
<info>php %command.full_name% --count=10</info> |
| 76 |
|
| 77 |
To output a specific format: |
| 78 |
|
| 79 |
<info>php %command.full_name% --format=base58</info> |
| 80 |
EOF |
| 81 |
) |
| 82 |
; |
| 83 |
} |
| 84 |
|
| 85 |
protected function execute(InputInterface $input, OutputInterface $output): int |
| 86 |
{ |
| 87 |
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); |
| 88 |
|
| 89 |
$time = $input->getOption('time-based'); |
| 90 |
$node = $input->getOption('node'); |
| 91 |
$name = $input->getOption('name-based'); |
| 92 |
$namespace = $input->getOption('namespace'); |
| 93 |
$random = $input->getOption('random-based'); |
| 94 |
|
| 95 |
if (false !== ($time ?? $name ?? $random) && 1 < ((null !== $time) + (null !== $name) + $random)) { |
| 96 |
$io->error('Only one of "--time-based", "--name-based" or "--random-based" can be provided at a time.'); |
| 97 |
|
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
if (null === $time && null !== $node) { |
| 102 |
$io->error('Option "--node" can only be used with "--time-based".'); |
| 103 |
|
| 104 |
return 1; |
| 105 |
} |
| 106 |
|
| 107 |
if (null === $name && null !== $namespace) { |
| 108 |
$io->error('Option "--namespace" can only be used with "--name-based".'); |
| 109 |
|
| 110 |
return 1; |
| 111 |
} |
| 112 |
|
| 113 |
switch (true) { |
| 114 |
case null !== $time: |
| 115 |
if (null !== $node) { |
| 116 |
try { |
| 117 |
$node = Uuid::fromString($node); |
| 118 |
} catch (\InvalidArgumentException $e) { |
| 119 |
$io->error(\sprintf('Invalid node "%s": %s', $node, $e->getMessage())); |
| 120 |
|
| 121 |
return 1; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
try { |
| 126 |
new \DateTimeImmutable($time); |
| 127 |
} catch (\Exception $e) { |
| 128 |
$io->error(\sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage()))); |
| 129 |
|
| 130 |
return 1; |
| 131 |
} |
| 132 |
|
| 133 |
$create = fn (): Uuid => $this->factory->timeBased($node)->create(new \DateTimeImmutable($time)); |
| 134 |
break; |
| 135 |
|
| 136 |
case null !== $name: |
| 137 |
if ($namespace && !\in_array($namespace, ['dns', 'url', 'oid', 'x500'], true)) { |
| 138 |
try { |
| 139 |
$namespace = Uuid::fromString($namespace); |
| 140 |
} catch (\InvalidArgumentException $e) { |
| 141 |
$io->error(\sprintf('Invalid namespace "%s": %s', $namespace, $e->getMessage())); |
| 142 |
|
| 143 |
return 1; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
$create = function () use ($namespace, $name): Uuid { |
| 148 |
try { |
| 149 |
$factory = $this->factory->nameBased($namespace); |
| 150 |
} catch (LogicException) { |
| 151 |
throw new \InvalidArgumentException('Missing namespace: use the "--namespace" option or configure a default namespace in the underlying factory.'); |
| 152 |
} |
| 153 |
|
| 154 |
return $factory->create($name); |
| 155 |
}; |
| 156 |
break; |
| 157 |
|
| 158 |
case $random: |
| 159 |
$create = $this->factory->randomBased()->create(...); |
| 160 |
break; |
| 161 |
|
| 162 |
default: |
| 163 |
$create = $this->factory->create(...); |
| 164 |
break; |
| 165 |
} |
| 166 |
|
| 167 |
$formatOption = $input->getOption('format'); |
| 168 |
|
| 169 |
if (\in_array($formatOption, $this->getAvailableFormatOptions(), true)) { |
| 170 |
$format = 'to'.ucfirst($formatOption); |
| 171 |
} else { |
| 172 |
$io->error(\sprintf('Invalid format "%s", supported formats are "%s".', $formatOption, implode('", "', $this->getAvailableFormatOptions()))); |
| 173 |
|
| 174 |
return 1; |
| 175 |
} |
| 176 |
|
| 177 |
$count = (int) $input->getOption('count'); |
| 178 |
try { |
| 179 |
for ($i = 0; $i < $count; ++$i) { |
| 180 |
$output->writeln($create()->$format()); |
| 181 |
} |
| 182 |
} catch (\Exception $e) { |
| 183 |
$io->error($e->getMessage()); |
| 184 |
|
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
| 192 |
{ |
| 193 |
if ($input->mustSuggestOptionValuesFor('format')) { |
| 194 |
$suggestions->suggestValues($this->getAvailableFormatOptions()); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
private function getAvailableFormatOptions(): array |
| 200 |
{ |
| 201 |
return [ |
| 202 |
'base32', |
| 203 |
'base58', |
| 204 |
'rfc4122', |
| 205 |
]; |
| 206 |
} |
| 207 |
} |