| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
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 |
private const FORMAT_OPTIONS = [
|
|
|
29 |
'base32',
|
|
|
30 |
'base58',
|
|
|
31 |
'rfc4122',
|
|
|
32 |
];
|
|
|
33 |
|
|
|
34 |
private UlidFactory $factory;
|
|
|
35 |
|
|
|
36 |
public function __construct(UlidFactory $factory = null)
|
|
|
37 |
{
|
|
|
38 |
$this->factory = $factory ?? new UlidFactory();
|
|
|
39 |
|
|
|
40 |
parent::__construct();
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
protected function configure(): void
|
|
|
44 |
{
|
|
|
45 |
$this
|
|
|
46 |
->setDefinition([
|
|
|
47 |
new InputOption('time', null, InputOption::VALUE_REQUIRED, 'The ULID timestamp: a parsable date/time string'),
|
|
|
48 |
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of ULID to generate', 1),
|
|
|
49 |
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The ULID output format: base32, base58 or rfc4122', 'base32'),
|
|
|
50 |
])
|
|
|
51 |
->setHelp(<<<'EOF'
|
|
|
52 |
The <info>%command.name%</info> command generates a ULID.
|
|
|
53 |
|
|
|
54 |
<info>php %command.full_name%</info>
|
|
|
55 |
|
|
|
56 |
To specify the timestamp:
|
|
|
57 |
|
|
|
58 |
<info>php %command.full_name% --time="2021-02-16 14:09:08"</info>
|
|
|
59 |
|
|
|
60 |
To generate several ULIDs:
|
|
|
61 |
|
|
|
62 |
<info>php %command.full_name% --count=10</info>
|
|
|
63 |
|
|
|
64 |
To output a specific format:
|
|
|
65 |
|
|
|
66 |
<info>php %command.full_name% --format=rfc4122</info>
|
|
|
67 |
EOF
|
|
|
68 |
)
|
|
|
69 |
;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
73 |
{
|
|
|
74 |
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
|
|
|
75 |
|
|
|
76 |
if (null !== $time = $input->getOption('time')) {
|
|
|
77 |
try {
|
|
|
78 |
$time = new \DateTimeImmutable($time);
|
|
|
79 |
} catch (\Exception $e) {
|
|
|
80 |
$io->error(sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage())));
|
|
|
81 |
|
|
|
82 |
return 1;
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$formatOption = $input->getOption('format');
|
|
|
87 |
|
|
|
88 |
if (\in_array($formatOption, self::FORMAT_OPTIONS)) {
|
|
|
89 |
$format = 'to'.ucfirst($formatOption);
|
|
|
90 |
} else {
|
|
|
91 |
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format')));
|
|
|
92 |
|
|
|
93 |
return 1;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$count = (int) $input->getOption('count');
|
|
|
97 |
try {
|
|
|
98 |
for ($i = 0; $i < $count; ++$i) {
|
|
|
99 |
$output->writeln($this->factory->create($time)->$format());
|
|
|
100 |
}
|
|
|
101 |
} catch (\Exception $e) {
|
|
|
102 |
$io->error($e->getMessage());
|
|
|
103 |
|
|
|
104 |
return 1;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
return 0;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
|
|
|
111 |
{
|
|
|
112 |
if ($input->mustSuggestOptionValuesFor('format')) {
|
|
|
113 |
$suggestions->suggestValues(self::FORMAT_OPTIONS);
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
}
|