-
-
Notifications
You must be signed in to change notification settings - Fork 81
Auto import: Honor JSON_CONFIGURATION_DIR #782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
use GrumpyDictator\FFIIIApiSupport\Request\GetAccountRequest; | ||
use Illuminate\Contracts\Filesystem\FileNotFoundException; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Facades\Storage; | ||
|
||
/** | ||
* Trait AutoImports | ||
|
@@ -80,22 +81,30 @@ private function getFiles(string $directory): array | |
return []; | ||
} | ||
$files = array_diff($array, $ignore); | ||
$return = []; | ||
$importableFiles = []; | ||
$jsonFiles = []; | ||
foreach ($files as $file) { | ||
// import importable file with JSON companion | ||
// TODO may also need to be able to detect other file types. Or: detect any file with accompanying json file | ||
if ('csv' === $this->getExtension($file) && $this->hasJsonConfiguration($directory, $file)) { | ||
$return[] = $file; | ||
if (in_array($this->getExtension($file), ['csv', 'xml'])) { | ||
$importableFiles[] = $file; | ||
} | ||
|
||
if ('xml' === $this->getExtension($file) && $this->hasJsonConfiguration($directory, $file)) { | ||
$return[] = $file; | ||
// import JSON config files. | ||
if ('json' === $this->getExtension($file)) { | ||
$jsonFiles[] = $file; | ||
} | ||
|
||
// import JSON with no importable file. | ||
// TODO must detect json files without accompanying camt/csv/whatever file. | ||
if ('json' === $this->getExtension($file) && !$this->hasCsvFile($directory, $file)) { | ||
$return[] = $file; | ||
} | ||
$return = []; | ||
foreach ($importableFiles as $importableFile) { | ||
$jsonFile = $this->getJsonConfiguration($directory, $importableFile); | ||
if($jsonFile) { | ||
$return[$jsonFile] = sprintf('%s/%s', $directory, $importableFile); | ||
} | ||
} | ||
foreach ($jsonFiles as $jsonFile) { | ||
$fullJson = sprintf('%s/%s', $directory, $jsonFile); | ||
if (!array_key_exists($fullJson, $return)) { | ||
$return[$fullJson] = $fullJson; | ||
} | ||
} | ||
|
||
|
@@ -112,75 +121,64 @@ private function getExtension(string $file): string | |
return strtolower($parts[count($parts) - 1]); | ||
} | ||
|
||
/** | ||
* This method only works on files with an extension with exactly three letters | ||
* (i.e. "csv", "xml"). | ||
*/ | ||
private function hasJsonConfiguration(string $directory, string $file): bool | ||
private function getExtensionLength(string $file): int | ||
{ | ||
$short = substr($file, 0, -4); | ||
$parts = explode('.', $file); | ||
if (1 === count($parts)) { | ||
return 0; | ||
} | ||
return strlen($parts[count($parts) - 1]) + 1; | ||
} | ||
|
||
private function getJsonConfiguration(string $directory, string $file): ?string | ||
{ | ||
$extensionLength = $this->getExtensionLength($file); | ||
$short = substr($file, 0, -$extensionLength); | ||
$jsonFile = sprintf('%s.json', $short); | ||
$fullJson = sprintf('%s/%s', $directory, $jsonFile); | ||
|
||
if (!file_exists($fullJson)) { | ||
$hasFallbackConfig = $this->hasFallbackConfig($directory); | ||
if ($hasFallbackConfig) { | ||
$this->line('Found fallback configuration file, which will be used for this file.'); | ||
|
||
return true; | ||
} | ||
$this->warn(sprintf('Cannot find JSON file "%s" nor fallback file expected to go with file "%s". This file will be ignored.', $jsonFile, $file)); | ||
if (file_exists($fullJson)) { | ||
return $fullJson; | ||
} | ||
if (Storage::disk('configurations')->exists($jsonFile)){ | ||
return Storage::disk('configurations')->path($jsonFile); | ||
} | ||
$fallbackConfig = $this->getFallbackConfig($directory); | ||
if ($fallbackConfig) { | ||
$this->line('Found fallback configuration file, which will be used for this file.'); | ||
|
||
return false; | ||
return $fallbackConfig; | ||
} | ||
$this->warn(sprintf('Cannot find JSON file "%s" nor fallback file expected to go with file "%s". This file will be ignored.', $jsonFile, $file)); | ||
|
||
return true; | ||
return null; | ||
} | ||
|
||
private function hasFallbackConfig(string $directory): bool | ||
private function getFallbackConfig(string $directory): ?string | ||
{ | ||
if (false === config('importer.fallback_in_dir')) { | ||
return false; | ||
return null; | ||
} | ||
$configJsonFile = sprintf('%s/%s', $directory, config('importer.fallback_configuration')); | ||
if (file_exists($configJsonFile) && is_readable($configJsonFile)) { | ||
$content = file_get_contents($configJsonFile); | ||
|
||
return json_validate($content); | ||
return $configJsonFile; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* TODO this function must be more universal. | ||
*/ | ||
private function hasCsvFile(string $directory, string $file): bool | ||
{ | ||
$short = substr($file, 0, -5); | ||
$csvFile = sprintf('%s.csv', $short); | ||
$fullJson = sprintf('%s/%s', $directory, $csvFile); | ||
if (!file_exists($fullJson)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
return null; | ||
} | ||
|
||
private function importFiles(string $directory, array $files): array | ||
{ | ||
$exitCodes = []; | ||
|
||
/** @var string $file */ | ||
foreach ($files as $file) { | ||
$key = sprintf('%s/%s', $directory, $file); | ||
|
||
foreach ($files as $jsonFile => $importableFile) { | ||
try { | ||
$exitCodes[$key] = $this->importFile($directory, $file); | ||
$exitCodes[$importableFile] = $this->importFile($jsonFile, $importableFile); | ||
} catch (ImporterErrorException $e) { | ||
app('log')->error(sprintf('Could not complete import from file "%s".', $file)); | ||
app('log')->error($e->getMessage()); | ||
$exitCodes[$key] = 1; | ||
$exitCodes[$importableFile] = 1; | ||
} | ||
// report has already been sent. Reset errors and continue. | ||
$this->conversionErrors = []; | ||
999F
|
@@ -199,36 +197,8 @@ private function importFiles(string $directory, array $files): array | |
/** | ||
* @throws ImporterErrorException | ||
*/ | ||
private function importFile(string $directory, string $file): int | ||
private function importFile(string $jsonFile, string $importableFile): int | ||
{ | ||
app('log')->debug(sprintf('ImportFile: directory "%s"', $directory)); | ||
app('log')->debug(sprintf('ImportFile: file "%s"', $file)); | ||
$importableFile = sprintf('%s/%s', $directory, $file); | ||
$jsonFile = sprintf('%s/%s.json', $directory, substr($file, 0, -5)); | ||
$fallbackJsonFile = sprintf('%s/%s', $directory, config('importer.fallback_configuration')); | ||
|
||
// TODO not yet sure why the distinction is necessary. | ||
// TODO this may also be necessary for camt files. | ||
if ('csv' === $this->getExtension($file)) { | ||
$jsonFile = sprintf('%s/%s.json', $directory, substr($file, 0, -4)); | ||
} | ||
// same for XML files. | ||
if ('xml' === $this->getExtension($file)) { | ||
$jsonFile = sprintf('%s/%s.json', $directory, substr($file, 0, -4)); | ||
} | ||
$jsonFileExists = file_exists($jsonFile); | ||
$hasFallbackConfig = $this->hasFallbackConfig($directory); | ||
|
||
// Should not happen | ||
if (!$jsonFileExists && !$hasFallbackConfig) { | ||
$this->error(sprintf('No JSON configuration found. Checked for both "%s" and "%s"', $jsonFile, $fallbackJsonFile)); | ||
app('log')->error(sprintf('Exit code is %s.', ExitCode::CANNOT_READ_CONFIG->name)); | ||
|
||
return ExitCode::CANNOT_READ_CONFIG->value; | ||
} | ||
|
||
$jsonFile = $jsonFileExists ? $jsonFile : $fallbackJsonFile; | ||
|
||
app('log')->debug(sprintf('ImportFile: importable "%s"', $importableFile)); | ||
app('log')->debug(sprintf('ImportFile: JSON "%s"', $jsonFile)); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.