Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

142 wiersze
3.8 KiB

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