SetVersionCommand.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Command;
  4. use RangeException;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. use function file_put_contents;
  10. use function preg_match;
  11. use function sprintf;
  12. final class SetVersionCommand extends Command
  13. {
  14. /** @var string */
  15. protected static $defaultName = 'set-version';
  16. /** @var string */
  17. private static $generatedClassTemplate = <<<'PHP'
  18. <?php
  19. declare(strict_types=1);
  20. namespace PhpMyAdmin;
  21. /**
  22. * This class is generated by scripts/console.
  23. *
  24. * @see \PhpMyAdmin\Command\SetVersionCommand
  25. */
  26. final class Version
  27. {
  28. // The VERSION_SUFFIX constant is defined at libraries/vendor_config.php
  29. public const VERSION = '%1$u.%2$u.%3$u%4$s' . VERSION_SUFFIX;
  30. public const SERIES = '%1$u.%2$u';
  31. public const MAJOR = %1$u;
  32. public const MINOR = %2$u;
  33. public const PATCH = %3$u;
  34. public const ID = %1$u%2$02u%3$02u;
  35. public const PRE_RELEASE_NAME = '%5$s';
  36. public const IS_DEV = %6$s;
  37. }
  38. PHP;
  39. protected function configure(): void
  40. {
  41. $this->setDescription('Sets the version number');
  42. $this->setHelp('This command generates the PhpMyAdmin\Version class based on the version number provided.');
  43. $this->addArgument('version', InputArgument::REQUIRED, 'The version number');
  44. }
  45. protected function execute(InputInterface $input, OutputInterface $output): int
  46. {
  47. /** @var string $version */
  48. $version = $input->getArgument('version');
  49. $generatedClass = $this->getGeneratedClass($version);
  50. if (! $this->writeGeneratedClassFile($generatedClass)) {
  51. // failure
  52. return 1;
  53. }
  54. $output->writeln('PhpMyAdmin\Version class successfully generated!');
  55. // success
  56. return 0;
  57. }
  58. private function getGeneratedClass(string $version): string
  59. {
  60. // Do not allow any major below 5
  61. $return = preg_match('/^([5-9]+)\.(\d{1,2})\.(\d{1,2})(-([a-z0-9]+))?$/', $version, $matches);
  62. if ($return === false || $return === 0) {
  63. throw new RangeException('The version number is in the wrong format: ' . $version);
  64. }
  65. return sprintf(
  66. self::$generatedClassTemplate,
  67. $matches[1],
  68. $matches[2],
  69. $matches[3],
  70. $matches[4] ?? '',
  71. $matches[5] ?? '',
  72. ($matches[5] ?? '') === 'dev' ? 'true' : 'false'
  73. );
  74. }
  75. private function writeGeneratedClassFile(string $generatedClass): bool
  76. {
  77. $result = file_put_contents(
  78. ROOT_PATH . 'libraries/classes/Version.php',
  79. $generatedClass
  80. );
  81. return $result !== false;
  82. }
  83. }