Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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