From e81a9b8c3899463ed47e779dff6aa33c2280e408 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 20 Feb 2022 07:18:54 +0100 Subject: [PATCH] Command to upgrade files. --- .../Commands/UpgradeImportConfigurations.php | 120 ++++++++++++++++++ .../Shared/Configuration/Configuration.php | 6 +- 2 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 app/Console/Commands/UpgradeImportConfigurations.php diff --git a/app/Console/Commands/UpgradeImportConfigurations.php b/app/Console/Commands/UpgradeImportConfigurations.php new file mode 100644 index 00000000..96daeeea --- /dev/null +++ b/app/Console/Commands/UpgradeImportConfigurations.php @@ -0,0 +1,120 @@ +argument('directory'); + + if (!file_exists($directory)) { + $this->error(sprintf('"%s" does not exist.', $directory)); + return 1; + } + if (!is_dir($directory)) { + $this->error(sprintf('"%s" is not a directory.', $directory)); + return 1; + } + + $this->processRoot($directory); + return 0; + } + + /** + * @param string $directory + */ + private function processRoot(string $directory): void + { + $dir = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS); + $files = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST); + /** + * @var string $name + * @var SplFileInfo $object + */ + foreach ($files as $name => $object) { + $this->processFile($name); + } + } + + /** + * @param string $name + */ + private function processFile(string $name): void + { + if ('json' !== $this->getExtension($name) || is_dir($name)) { + return; + } + $this->line(sprintf('Now processing "%s" ...', $name)); + $content = (string) file_get_contents($name); + if (!$this->isValidJson($content)) { + $this->error('File does not contain valid JSON. Skipped.'); + return; + } + $configuration = Configuration::fromFile(json_decode($content, true)); + $newJson = $configuration->toArray(); + file_put_contents($name, json_encode($newJson, JSON_PRETTY_PRINT)); + } + + /** + * @param string $name + * @return string + */ + private function getExtension(string $name): string + { + $parts = explode('.', $name); + + return $parts[count($parts) - 1]; + } + + /** + * @param string $content + * @return bool + */ + private function isValidJson(string $content): bool + { + if ('' === $content) { + return false; + } + $json = json_decode($content, true); + if (false === $json) { + return false; + } + return true; + } +} diff --git a/app/Services/Shared/Configuration/Configuration.php b/app/Services/Shared/Configuration/Configuration.php index a81444a0..d887b86f 100644 --- a/app/Services/Shared/Configuration/Configuration.php +++ b/app/Services/Shared/Configuration/Configuration.php @@ -330,13 +330,9 @@ class Configuration // loop roles from classic file: $roles = $data['column-roles'] ?? []; - app('log')->debug(sprintf('There are %d roles in the array.', count($roles))); foreach ($roles as $index => $role) { // some roles have been given a new name some time in the past. - $role = $classicRoleNames[$role] ?? $role; - - app('log')->debug(sprintf('Role #%d is "%s".', $index, $role)); - + $role = $classicRoleNames[$role] ?? $role; $config = config(sprintf('csv.import_roles.%s', $role)); if (null !== $config) { $object->roles[$index] = $role;