You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.0 KiB

  1. <?php
  2. /*
  3. * UpgradeImportConfigurations.php
  4. * Copyright (c) 2022 james@firefly-iii.org
  5. *
  6. * This file is part of the Firefly III Data Importer
  7. * (https://github.com/firefly-iii/data-importer).
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. declare(strict_types=1);
  23. namespace App\Console\Commands;
  24. use App\Services\Shared\Configuration\Configuration;
  25. use Illuminate\Console\Command;
  26. use RecursiveDirectoryIterator;
  27. use RecursiveIteratorIterator;
  28. use SplFileInfo;
  29. class UpgradeImportConfigurations extends Command
  30. {
  31. /**
  32. * The name and signature of the console command.
  33. *
  34. * @var string
  35. */
  36. protected $signature = 'importer:upgrade-import-configurations {directory}';
  37. /**
  38. * The console command description.
  39. *
  40. * @var string
  41. */
  42. protected $description = 'Pointed to a directory, will parse and OVERWRITE all JSON files found there according to the latest JSON configuration file standards.';
  43. /**
  44. * Create a new command instance.
  45. *
  46. * @return void
  47. */
  48. public function __construct()
  49. {
  50. parent::__construct();
  51. }
  52. /**
  53. * Execute the console command.
  54. *
  55. * @return int
  56. */
  57. public function handle(): int
  58. {
  59. $directory = (string) $this->argument('directory');
  60. if (!file_exists($directory)) {
  61. $this->error(sprintf('"%s" does not exist.', $directory));
  62. return 1;
  63. }
  64. if (!is_dir($directory)) {
  65. $this->error(sprintf('"%s" is not a directory.', $directory));
  66. return 1;
  67. }
  68. $this->processRoot($directory);
  69. return 0;
  70. }
  71. /**
  72. * @param string $directory
  73. */
  74. private function processRoot(string $directory): void
  75. {
  76. $dir = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
  77. $files = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
  78. /**
  79. * @var string $name
  80. * @var SplFileInfo $object
  81. */
  82. foreach ($files as $name => $object) {
  83. $this->processFile($name);
  84. }
  85. }
  86. /**
  87. * @param string $name
  88. */
  89. private function processFile(string $name): void
  90. {
  91. if ('json' !== $this->getExtension($name) || is_dir($name)) {
  92. return;
  93. }
  94. $this->line(sprintf('Now processing "%s" ...', $name));
  95. $content = (string) file_get_contents($name);
  96. if (!$this->isValidJson($content)) {
  97. $this->error('File does not contain valid JSON. Skipped.');
  98. return;
  99. }
  100. $configuration = Configuration::fromFile(json_decode($content, true));
  101. $newJson = $configuration->toArray();
  102. $newJson['mapping'] = [];
  103. $newJson['default_account'] = 0;
  104. file_put_contents($name, json_encode($newJson, JSON_PRETTY_PRINT));
  105. }
  106. /**
  107. * @param string $name
  108. * @return string
  109. */
  110. private function getExtension(string $name): string
  111. {
  112. $parts = explode('.', $name);
  113. return $parts[count($parts) - 1];
  114. }
  115. /**
  116. * @param string $content
  117. * @return bool
  118. */
  119. private function isValidJson(string $content): bool
  120. {
  121. if ('' === $content) {
  122. return false;
  123. }
  124. $json = json_decode($content, true);
  125. if (false === $json) {
  126. return false;
  127. }
  128. return true;
  129. }
  130. }