GenerateUlidCommand.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Uid\Command;
  11. use Symfony\Component\Console\Attribute\AsCommand;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Completion\CompletionInput;
  14. use Symfony\Component\Console\Completion\CompletionSuggestions;
  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. #[AsCommand(name: 'ulid:generate', description: 'Generate a ULID')]
  22. class GenerateUlidCommand extends Command
  23. {
  24. public function __construct(
  25. private UlidFactory $factory = new UlidFactory(),
  26. ) {
  27. parent::__construct();
  28. }
  29. protected function configure(): void
  30. {
  31. $this
  32. ->setDefinition([
  33. new InputOption('time', null, InputOption::VALUE_REQUIRED, 'The ULID timestamp: a parsable date/time string'),
  34. new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of ULID to generate', 1),
  35. new InputOption('format', 'f', InputOption::VALUE_REQUIRED, \sprintf('The ULID output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'base32'),
  36. ])
  37. ->setHelp(<<<'EOF'
  38. The <info>%command.name%</info> command generates a ULID.
  39. <info>php %command.full_name%</info>
  40. To specify the timestamp:
  41. <info>php %command.full_name% --time="2021-02-16 14:09:08"</info>
  42. To generate several ULIDs:
  43. <info>php %command.full_name% --count=10</info>
  44. To output a specific format:
  45. <info>php %command.full_name% --format=rfc4122</info>
  46. EOF
  47. )
  48. ;
  49. }
  50. protected function execute(InputInterface $input, OutputInterface $output): int
  51. {
  52. $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  53. if (null !== $time = $input->getOption('time')) {
  54. try {
  55. $time = new \DateTimeImmutable($time);
  56. } catch (\Exception $e) {
  57. $io->error(\sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage())));
  58. return 1;
  59. }
  60. }
  61. $formatOption = $input->getOption('format');
  62. if (\in_array($formatOption, $this->getAvailableFormatOptions(), true)) {
  63. $format = 'to'.ucfirst($formatOption);
  64. } else {
  65. $io->error(\sprintf('Invalid format "%s", supported formats are "%s".', $formatOption, implode('", "', $this->getAvailableFormatOptions())));
  66. return 1;
  67. }
  68. $count = (int) $input->getOption('count');
  69. try {
  70. for ($i = 0; $i < $count; ++$i) {
  71. $output->writeln($this->factory->create($time)->$format());
  72. }
  73. } catch (\Exception $e) {
  74. $io->error($e->getMessage());
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
  80. {
  81. if ($input->mustSuggestOptionValuesFor('format')) {
  82. $suggestions->suggestValues($this->getAvailableFormatOptions());
  83. }
  84. }
  85. /** @return string[] */
  86. private function getAvailableFormatOptions(): array
  87. {
  88. return [
  89. 'base32',
  90. 'base58',
  91. 'rfc4122',
  92. ];
  93. }
  94. }