GenerateUlidCommand.php 3.7 KB

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