| 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\Factory\UlidFactory; |
| 24 |
|
| 25 |
#[AsCommand(name: 'ulid:generate', description: 'Generate a ULID')] |
| 26 |
class GenerateUlidCommand extends Command |
| 27 |
{ |
| 28 |
public function __construct( |
| 29 |
private UlidFactory $factory = new UlidFactory(), |
| 30 |
) { |
| 31 |
parent::__construct(); |
| 32 |
} |
| 33 |
|
| 34 |
protected function configure(): void |
| 35 |
{ |
| 36 |
$this |
| 37 |
->setDefinition([ |
| 38 |
new InputOption('time', null, InputOption::VALUE_REQUIRED, 'The ULID timestamp: a parsable date/time string'), |
| 39 |
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of ULID to generate', 1), |
| 40 |
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, \sprintf('The ULID output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'base32'), |
| 41 |
]) |
| 42 |
->setHelp(<<<'EOF' |
| 43 |
The <info>%command.name%</info> command generates a ULID. |
| 44 |
|
| 45 |
<info>php %command.full_name%</info> |
| 46 |
|
| 47 |
To specify the timestamp: |
| 48 |
|
| 49 |
<info>php %command.full_name% --time="2021-02-16 14:09:08"</info> |
| 50 |
|
| 51 |
To generate several ULIDs: |
| 52 |
|
| 53 |
<info>php %command.full_name% --count=10</info> |
| 54 |
|
| 55 |
To output a specific format: |
| 56 |
|
| 57 |
<info>php %command.full_name% --format=rfc4122</info> |
| 58 |
EOF |
| 59 |
) |
| 60 |
; |
| 61 |
} |
| 62 |
|
| 63 |
protected function execute(InputInterface $input, OutputInterface $output): int |
| 64 |
{ |
| 65 |
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); |
| 66 |
|
| 67 |
if (null !== $time = $input->getOption('time')) { |
| 68 |
try { |
| 69 |
$time = new \DateTimeImmutable($time); |
| 70 |
} catch (\Exception $e) { |
| 71 |
$io->error(\sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage()))); |
| 72 |
|
| 73 |
return 1; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
$formatOption = $input->getOption('format'); |
| 78 |
|
| 79 |
if (\in_array($formatOption, $this->getAvailableFormatOptions(), true)) { |
| 80 |
$format = 'to'.ucfirst($formatOption); |
| 81 |
} else { |
| 82 |
$io->error(\sprintf('Invalid format "%s", supported formats are "%s".', $formatOption, implode('", "', $this->getAvailableFormatOptions()))); |
| 83 |
|
| 84 |
return 1; |
| 85 |
} |
| 86 |
|
| 87 |
$count = (int) $input->getOption('count'); |
| 88 |
try { |
| 89 |
for ($i = 0; $i < $count; ++$i) { |
| 90 |
$output->writeln($this->factory->create($time)->$format()); |
| 91 |
} |
| 92 |
} catch (\Exception $e) { |
| 93 |
$io->error($e->getMessage()); |
| 94 |
|
| 95 |
return 1; |
| 96 |
} |
| 97 |
|
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
| 102 |
{ |
| 103 |
if ($input->mustSuggestOptionValuesFor('format')) { |
| 104 |
$suggestions->suggestValues($this->getAvailableFormatOptions()); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
private function getAvailableFormatOptions(): array |
| 110 |
{ |
| 111 |
return [ |
| 112 |
'base32', |
| 113 |
'base58', |
| 114 |
'rfc4122', |
| 115 |
]; |
| 116 |
} |
| 117 |
} |