FlexConvertDataCommand.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Grav\Plugin\Console;
  3. use Exception;
  4. use Grav\Common\Yaml;
  5. use Grav\Console\ConsoleCommand;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use function count;
  8. /**
  9. * Class FlushQueueCommand
  10. * @package Grav\Console\Cli\
  11. */
  12. class FlexConvertDataCommand extends ConsoleCommand
  13. {
  14. /** @var array */
  15. protected $options = [];
  16. /**
  17. * @return void
  18. */
  19. protected function configure(): void
  20. {
  21. $this
  22. ->setName('convert-data')
  23. ->setAliases(['convertdata'])
  24. ->addOption(
  25. 'in',
  26. 'i',
  27. InputOption::VALUE_REQUIRED,
  28. 'path to file to convert from (valid types: [json|yaml])'
  29. )
  30. ->addOption(
  31. 'out',
  32. 'o',
  33. InputOption::VALUE_REQUIRED,
  34. 'format of file to convert to [json|yaml]'
  35. )
  36. ->setDescription('Converts data from one format to another')
  37. ->setHelp('The <info>clear-queue-failures</info> command clears any queue failures that have accumulated');
  38. }
  39. /**
  40. * @return int
  41. */
  42. protected function serve(): int
  43. {
  44. $input = $this->getInput();
  45. $io = $this->getIO();
  46. $out_raw = null;
  47. $in = $input->getOption('in');
  48. $in_parts = pathinfo($in);
  49. $in_extension = $in_parts['extension'];
  50. $out_extension = $input->getOption('out');
  51. $io->title('Flex Convert Data');
  52. if (!file_exists($in)) {
  53. $io->error('cannot find the file: ' . realpath($in));
  54. return 1;
  55. }
  56. if (!$in_extension) {
  57. $io->error($in . ' has no file extension defined');
  58. return 1;
  59. }
  60. if (!$out_extension) {
  61. $io->error($out_extension . ' is not a valid extension');
  62. return 1;
  63. }
  64. $in_raw = file_get_contents($in);
  65. // Get the input data
  66. if ($in_extension === 'yaml' || $in_extension === 'yml') {
  67. $in_data = Yaml::parse($in_raw);
  68. } elseif ($in_extension === 'json' ) {
  69. $in_data = json_decode($in_raw, true, 512, JSON_THROW_ON_ERROR);
  70. } else {
  71. $io->error('input files with extension ' . $in_extension . ', is not supported');
  72. return 1;
  73. }
  74. // Simple progress bar
  75. $progress = $io->createProgressBar(count($in_data));
  76. $progress->setFormat('verbose');
  77. $progress->start();
  78. // add Unique Id if needed
  79. $index = 0;
  80. $out_data = [];
  81. foreach ($in_data as $key => $entry) {
  82. if ($key === $index++) {
  83. $out_data[$this->generateKey()] = $entry;
  84. } else {
  85. $out_data[$key] = $entry;
  86. }
  87. $progress->advance();
  88. }
  89. // render progress
  90. $progress->finish();
  91. $io->newLine(2);
  92. // Convert to output format
  93. if ($out_extension === 'yaml' || $out_extension === 'yml') {
  94. $out_raw = Yaml::dump($out_data);
  95. } elseif ($out_extension === 'json' ) {
  96. $out_raw = json_encode($out_data, JSON_PRETTY_PRINT);
  97. } else {
  98. $io->error('input files with extension ' . $out_extension . ', is not supported');
  99. return 1;
  100. }
  101. // Write the file:
  102. $out_filename = $in_parts['dirname'] . '/' . $in_parts['filename'] . '.' . $out_extension;
  103. file_put_contents($out_filename, $out_raw);
  104. $io->success('successfully converted the file and saved as: ' . $out_filename);
  105. return 0;
  106. }
  107. /**
  108. * @return string|false
  109. * @throws Exception
  110. */
  111. protected function generateKey()
  112. {
  113. return substr(hash('sha256', random_bytes(32)), 0, 32);
  114. }
  115. }