diff --git a/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php b/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php index b7c9d7c031969..fd010203d4437 100644 --- a/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php +++ b/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php @@ -41,7 +41,7 @@ protected function configure(): void { $this ->setDefinition([ - new InputOption('time', null, InputOption::VALUE_REQUIRED, 'The ULID timestamp: a parsable date/time string. It must be greater than or equals to the UNIX epoch (1970-01-01 00:00:00)'), + new InputOption('time', null, InputOption::VALUE_REQUIRED, 'The ULID timestamp: a parsable date/time string'), new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of ULID to generate', 1), new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The ULID output format: base32, base58 or rfc4122', 'base32'), ]) @@ -78,40 +78,32 @@ protected function execute(InputInterface $input, OutputInterface $output) try { $time = new \DateTimeImmutable($time); } catch (\Exception $e) { - $io->error(sprintf('Invalid timestamp "%s". %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage()))); + $io->error(sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage()))); return 1; } - - if (0 > $time->format('U')) { - $io->error(sprintf('Invalid timestamp "%s". It must be greater than or equals to the UNIX epoch (1970-01-01 00:00:00).', $input->getOption('time'))); - - return 2; - } } switch ($input->getOption('format')) { - case 'base32': - $format = 'strval'; - - break; - case 'base58': - $format = static function (Ulid $ulid): string { return $ulid->toBase58(); }; + case 'base32': $format = 'toBase32'; break; + case 'base58': $format = 'toBase58'; break; + case 'rfc4122': $format = 'toRfc4122'; break; - break; - case 'rfc4122': - $format = static function (Ulid $ulid): string { return $ulid->toRfc4122(); }; - - break; default: - $io->error(sprintf('Invalid format "%s". Supported formats are base32, base58 and rfc4122.', $input->getOption('format'))); + $io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format'))); - return 3; + return 1; } $count = (int) $input->getOption('count'); - for ($i = 0; $i < $count; ++$i) { - $output->writeln($format($this->factory->create($time))); + try { + for ($i = 0; $i < $count; ++$i) { + $output->writeln($this->factory->create($time)->$format()); + } + } catch (\Exception $e) { + $io->error($e->getMessage()); + + return 1; } return 0; diff --git a/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php b/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php index 7c24dc84ff0b4..1f11bf69ed3f0 100644 --- a/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php +++ b/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php @@ -41,7 +41,7 @@ protected function configure(): void { $this ->setDefinition([ - new InputOption('time-based', null, InputOption::VALUE_REQUIRED, 'The timestamp, to generate a time-based UUID: a parsable date/time string. It must be greater than or equals to the UUID epoch (1582-10-15 00:00:00)'), + new InputOption('time-based', null, InputOption::VALUE_REQUIRED, 'The timestamp, to generate a time-based UUID: a parsable date/time string'), new InputOption('node', null, InputOption::VALUE_REQUIRED, 'The UUID whose node part should be used as the node of the generated UUID'), new InputOption('name-based', null, InputOption::VALUE_REQUIRED, 'The name, to generate a name-based UUID'), new InputOption('namespace', null, InputOption::VALUE_REQUIRED, 'The UUID to use at the namespace for named-based UUIDs'), @@ -100,17 +100,31 @@ protected function execute(InputInterface $input, OutputInterface $output) $namespace = $input->getOption('namespace'); $random = $input->getOption('random-based'); - switch (true) { - case !$time && !$node && !$name && !$namespace && !$random: - $create = [$this->factory, 'create']; + if (null !== ($time ?? $name ?? $random ?? null) && !(null !== $time xor null !== $name xor null !== $random)) { + $io->error('Only one of "--time-based", "--name-based" or "--random-based" can be provided at a time.'); - break; - case $time && !$name && !$namespace && !$random: - if ($node) { + return 1; + } + + if (null === $time && null !== $node) { + $io->error('Option "--node" can only be used with "--time-based".'); + + return 1; + } + + if (null === $name && null !== $namespace) { + $io->error('Option "--namespace" can only be used with "--name-based".'); + + return 1; + } + + switch (true) { + case null !== $time: + if (null !== $node) { try { $node = Uuid::fromString($node); } catch (\InvalidArgumentException $e) { - $io->error(sprintf('Invalid node "%s". %s', $node, $e->getMessage())); + $io->error(sprintf('Invalid node "%s": %s', $node, $e->getMessage())); return 1; } @@ -119,74 +133,69 @@ protected function execute(InputInterface $input, OutputInterface $output) try { $time = new \DateTimeImmutable($time); } catch (\Exception $e) { - $io->error(sprintf('Invalid timestamp "%s". %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage()))); - - return 2; - } - - if ($time < new \DateTimeImmutable('@-12219292800')) { - $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'))); + $io->error(sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage()))); - return 3; + return 1; } - $create = function () use ($node, $time): Uuid { return $this->factory->timeBased($node)->create($time); }; - + $create = function () use ($node, $time): Uuid { + return $this->factory->timeBased($node)->create($time); + }; break; - case $name && !$time && !$node && !$random: + + case null !== $name: if ($namespace) { try { $namespace = Uuid::fromString($namespace); } catch (\InvalidArgumentException $e) { - $io->error(sprintf('Invalid namespace "%s". %s', $namespace, $e->getMessage())); + $io->error(sprintf('Invalid namespace "%s": %s', $namespace, $e->getMessage())); - return 4; - } - } else { - $refl = new \ReflectionProperty($this->factory, 'nameBasedNamespace'); - $refl->setAccessible(true); - if (null === $refl->getValue($this->factory)) { - $io->error('Missing namespace. Use the "--namespace" option or configure a default namespace in the underlying factory.'); - - return 5; + return 1; } } - $create = function () use ($namespace, $name): Uuid { return $this->factory->nameBased($namespace)->create($name); }; + $create = function () use ($namespace, $name): Uuid { + try { + $factory = $this->factory->nameBased($namespace); + } catch (\LogicException $e) { + $io->error('Missing namespace: use the "--namespace" option or configure a default namespace in the underlying factory.'); + return 1; + } + + return $factory->create($name); + }; break; - case $random && !$time && !$node && !$name && !$namespace: - $create = [$this->factory->randomBased(), 'create']; + case null !== $random: + $create = [$this->factory->randomBased(), 'create']; break; - default: - $io->error('Invalid combination of options.'); - return 6; + default: + $create = [$this->factory, 'create']; + break; } switch ($input->getOption('format')) { - case 'rfc4122': - $format = 'strval'; - - break; - case 'base58': - $format = static function (Uuid $uuid): string { return $uuid->toBase58(); }; - - break; - case 'base32': - $format = static function (Uuid $uuid): string { return $uuid->toBase32(); }; + case 'base32': $format = 'toBase32'; break; + case 'base58': $format = 'toBase58'; break; + case 'rfc4122': $format = 'toRfc4122'; break; - break; default: - $io->error(sprintf('Invalid format "%s". Supported formats are rfc4122, base58 and base32.', $input->getOption('format'))); + $io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format'))); - return 7; + return 1; } $count = (int) $input->getOption('count'); - for ($i = 0; $i < $count; ++$i) { - $io->writeln($format($create())); + try { + for ($i = 0; $i < $count; ++$i) { + $output->writeln($create()->$format()); + } + } catch (\Exception $e) { + $io->error($e->getMessage()); + + return 1; } return 0; diff --git a/src/Symfony/Component/Uid/Command/InspectUlidCommand.php b/src/Symfony/Component/Uid/Command/InspectUlidCommand.php index c6877d635b800..ba6c45c9b8475 100644 --- a/src/Symfony/Component/Uid/Command/InspectUlidCommand.php +++ b/src/Symfony/Component/Uid/Command/InspectUlidCommand.php @@ -56,7 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output) try { $ulid = Ulid::fromString($input->getArgument('ulid')); } catch (\InvalidArgumentException $e) { - $io->error(sprintf('Invalid ULID "%s".', $input->getArgument('ulid'))); + $io->error($e->getMessage()); return 1; } diff --git a/src/Symfony/Component/Uid/Command/InspectUuidCommand.php b/src/Symfony/Component/Uid/Command/InspectUuidCommand.php index 1a8880f610e44..6b6bbf3ed3bcc 100644 --- a/src/Symfony/Component/Uid/Command/InspectUuidCommand.php +++ b/src/Symfony/Component/Uid/Command/InspectUuidCommand.php @@ -59,7 +59,7 @@ protected function execute(InputInterface $input, OutputInterface $output) /** @var Uuid $uuid */ $uuid = Uuid::fromString($input->getArgument('uuid')); } catch (\InvalidArgumentException $e) { - $io->error(sprintf('Invalid UUID "%s".', $input->getArgument('uuid'))); + $io->error($e->getMessage()); return 1; }