|
| 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 | +namespaceSymfony\Component\Uid\Command; |
| 13 | + |
| 14 | +useSymfony\Component\Console\Command\Command; |
| 15 | +useSymfony\Component\Console\Input\InputInterface; |
| 16 | +useSymfony\Component\Console\Input\InputOption; |
| 17 | +useSymfony\Component\Console\Output\ConsoleOutputInterface; |
| 18 | +useSymfony\Component\Console\Output\OutputInterface; |
| 19 | +useSymfony\Component\Console\Style\SymfonyStyle; |
| 20 | +useSymfony\Component\Uid\Factory\UuidFactory; |
| 21 | +useSymfony\Component\Uid\Uuid; |
| 22 | + |
| 23 | +class GenerateUuidCommandextends Command |
| 24 | +{ |
| 25 | +protectedstatic$defaultName ='uuid:generate'; |
| 26 | +protectedstatic$defaultDescription ='Generates a UUID'; |
| 27 | + |
| 28 | +private$factory; |
| 29 | + |
| 30 | +publicfunction__construct(UuidFactory$factory =null) |
| 31 | + { |
| 32 | +$this->factory =$factory ??newUuidFactory(); |
| 33 | + |
| 34 | +parent::__construct(); |
| 35 | + } |
| 36 | + |
| 37 | +/** |
| 38 | + * {@inheritdoc} |
| 39 | + */ |
| 40 | +protectedfunctionconfigure():void |
| 41 | + { |
| 42 | +$this |
| 43 | + ->setDefinition([ |
| 44 | +newInputOption('time-based',null, InputOption::VALUE_REQUIRED,'The timestamp, to generate a time based UUID: a parsable date/time string by the \DateTimeImmutable constructor. It must be greater than or equals to the UUID epoch (1582-10-15 00:00:00)'), |
| 45 | +newInputOption('node',null, InputOption::VALUE_REQUIRED,"The time based UUID's node: a UUID, in any of the supported formats (base 58, base 32 or RFC 4122)"), |
| 46 | +newInputOption('name-based',null, InputOption::VALUE_REQUIRED,'The name, to generate a name based UUID'), |
| 47 | +newInputOption('namespace',null, InputOption::VALUE_REQUIRED,"The name based UUID's namespace: a UUID, in any of the supported formats (base 58, base 32 or RFC 4122)"), |
| 48 | +newInputOption('random-based',null, InputOption::VALUE_NONE,'To generate a random based UUID'), |
| 49 | +newInputOption('count','c', InputOption::VALUE_REQUIRED,'The number of UUID to generate',1), |
| 50 | +newInputOption('format','f', InputOption::VALUE_REQUIRED,'The UUID output format: rfc4122, base58 or base32','rfc4122'), |
| 51 | + ]) |
| 52 | + ->setDescription(self::$defaultDescription) |
| 53 | + ->setHelp(<<<'EOF' |
| 54 | +The <info>%command.name%</info> generates a UUID. |
| 55 | +
|
| 56 | + <info>php %command.full_name%</info> |
| 57 | +
|
| 58 | +To generate a time based UUID: |
| 59 | +
|
| 60 | + <info>php %command.full_name% --time-based=now</info> |
| 61 | +
|
| 62 | +To specify a time based UUID's node: |
| 63 | +
|
| 64 | + <info>php %command.full_name% --time-based=@1613480254 --node=fb3502dc-137e-4849-8886-ac90d07f64a7</info> |
| 65 | +
|
| 66 | +To generate a name based UUID: |
| 67 | +
|
| 68 | + <info>php %command.full_name% --name-based=foo</info> |
| 69 | +
|
| 70 | +To specify a name based UUID's namespace: |
| 71 | +
|
| 72 | + <info>php %command.full_name% --name-based=bar --namespace=fb3502dc-137e-4849-8886-ac90d07f64a7</info> |
| 73 | +
|
| 74 | +To generate a random based UUID: |
| 75 | +
|
| 76 | + <info>php %command.full_name% --random-based</info> |
| 77 | +
|
| 78 | +To generate several UUIDs: |
| 79 | +
|
| 80 | + <info>php %command.full_name% --count=10</info> |
| 81 | +
|
| 82 | +To output a specific format: |
| 83 | +
|
| 84 | + <info>php %command.full_name% --format=base58</info> |
| 85 | +EOF |
| 86 | + ) |
| 87 | + ; |
| 88 | + } |
| 89 | + |
| 90 | +/** |
| 91 | + * {@inheritdoc} |
| 92 | + */ |
| 93 | +protectedfunctionexecute(InputInterface$input,OutputInterface$output) |
| 94 | + { |
| 95 | +$io =newSymfonyStyle($input,$outputinstanceof ConsoleOutputInterface ?$output->getErrorOutput() :$output); |
| 96 | + |
| 97 | +$time =$input->getOption('time-based'); |
| 98 | +$node =$input->getOption('node'); |
| 99 | +$name =$input->getOption('name-based'); |
| 100 | +$namespace =$input->getOption('namespace'); |
| 101 | +$random =$input->getOption('random-based'); |
| 102 | + |
| 103 | +switch (true) { |
| 104 | +case !$time && !$node && !$name && !$namespace && !$random: |
| 105 | +$create = [$this->factory,'create']; |
| 106 | + |
| 107 | +break; |
| 108 | +case$time && !$name && !$namespace && !$random: |
| 109 | +if ($node) { |
| 110 | +try { |
| 111 | +$node = Uuid::fromString($node); |
| 112 | + }catch (\InvalidArgumentException$e) { |
| 113 | +$io->error(sprintf('Invalid node "%s". It cannot be parsed.',$node)); |
| 114 | + |
| 115 | +return1; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +try { |
| 120 | +$time =new \DateTimeImmutable($time); |
| 121 | + }catch (\Exception$e) { |
| 122 | +$io->error(sprintf('Invalid timestamp "%s". It cannot be parsed.',$input->getOption('time-based'))); |
| 123 | + |
| 124 | +return2; |
| 125 | + } |
| 126 | + |
| 127 | +if ($time <new \DateTimeImmutable('@-12219292800')) { |
| 128 | +$io->error(sprintf('Invalid timestamp "%s". It must be greater than or equals to the UUID epoch (1582-10-15 00:00:00).',$input->getOption('time-based'))); |
| 129 | + |
| 130 | +return3; |
| 131 | + } |
| 132 | + |
| 133 | +$create =function ()use ($node,$time):Uuid {return$this->factory->timeBased($node)->create($time); }; |
| 134 | + |
| 135 | +break; |
| 136 | +case$name && !$time && !$node && !$random: |
| 137 | +if ($namespace) { |
| 138 | +try { |
| 139 | +$namespace = Uuid::fromString($namespace); |
| 140 | + }catch (\InvalidArgumentException$e) { |
| 141 | +$io->error(sprintf('Invalid namespace "%s". It cannot be parsed.',$namespace)); |
| 142 | + |
| 143 | +return4; |
| 144 | + } |
| 145 | + }else { |
| 146 | +$refl =new \ReflectionProperty($this->factory,'nameBasedNamespace'); |
| 147 | +$refl->setAccessible(true); |
| 148 | +if (null ===$refl->getValue($this->factory)) { |
| 149 | +$io->error('Missing namespace. Use the "--namespace" option or configure a default namespace in the underlying factory.'); |
| 150 | + |
| 151 | +return5; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | +$create =function ()use ($namespace,$name):Uuid {return$this->factory->nameBased($namespace)->create($name); }; |
| 156 | + |
| 157 | +break; |
| 158 | +case$random && !$time && !$node && !$name && !$namespace: |
| 159 | +$create = [$this->factory->randomBased(),'create']; |
| 160 | + |
| 161 | +break; |
| 162 | +default: |
| 163 | +$io->error('Invalid combination of options.'); |
| 164 | + |
| 165 | +return6; |
| 166 | + } |
| 167 | + |
| 168 | +switch ($input->getOption('format')) { |
| 169 | +case'rfc4122': |
| 170 | +$format ='strval'; |
| 171 | + |
| 172 | +break; |
| 173 | +case'base58': |
| 174 | +$format =staticfunction (Uuid$uuid):string {return$uuid->toBase58(); }; |
| 175 | + |
| 176 | +break; |
| 177 | +case'base32': |
| 178 | +$format =staticfunction (Uuid$uuid):string {return$uuid->toBase32(); }; |
| 179 | + |
| 180 | +break; |
| 181 | +default: |
| 182 | +$io->error(sprintf('Invalid format "%s". Supported formats are rfc4122, base58 and base32.',$input->getOption('format'))); |
| 183 | + |
| 184 | +return7; |
| 185 | + } |
| 186 | + |
| 187 | +$count = (int)$input->getOption('count'); |
| 188 | +for ($i =0;$i <$count; ++$i) { |
| 189 | +$io->writeln($format($create())); |
| 190 | + } |
| 191 | + |
| 192 | +return0; |
| 193 | + } |
| 194 | +} |