From ed3c430078d7ce3684ccab6f4c4060975fa7ab48 Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Thu, 21 Nov 2024 19:15:16 -0500 Subject: [PATCH 1/6] initial import --- .../Interpreter/JsonFileInterpreter.php | 86 +++++++++++++++++-- .../components/interpreter/json.js | 6 ++ 2 files changed, 84 insertions(+), 8 deletions(-) diff --git a/src/DataSource/Interpreter/JsonFileInterpreter.php b/src/DataSource/Interpreter/JsonFileInterpreter.php index 4b969d21..b587a651 100644 --- a/src/DataSource/Interpreter/JsonFileInterpreter.php +++ b/src/DataSource/Interpreter/JsonFileInterpreter.php @@ -20,6 +20,8 @@ class JsonFileInterpreter extends AbstractInterpreter { + protected string $path; + /** * @var array|null */ @@ -34,8 +36,13 @@ protected function loadData(string $path): array { if ($this->cachedFilePath === $path && !empty($this->cachedContent)) { $content = file_get_contents($path); + $data = json_decode($this->prepareContent($content), true); + + if (!empty($this->path)) { + return $this->getValueFromPath($this->path, $data); + } - return json_decode($this->prepareContent($content), true); + return $data; } else { return $this->cachedContent; } @@ -52,7 +59,7 @@ protected function doInterpretFileAndCallProcessRow(string $path): void public function setSettings(array $settings): void { - //nothing to do + $this->path = $settings['path']; } /** @@ -90,18 +97,18 @@ public function fileValid(string $path, bool $originalFilename = false): bool $data = json_decode($this->prepareContent($content), true); - if (json_last_error() === JSON_ERROR_NONE) { - $this->cachedContent = $data; - $this->cachedFilePath = $path; - - return true; - } else { + if (!$this->isJsonValid($data)) { $this->applicationLogger->error('Reading file ERROR: ' . json_last_error_msg(), [ 'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $this->configName ]); return false; } + + $this->cachedContent = $data; + $this->cachedFilePath = $path; + + return true; } public function previewData(string $path, int $recordNumber = 0, array $mappedColumns = []): PreviewData @@ -132,4 +139,67 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo return new PreviewData($columns, $previewData, $readRecordNumber, $mappedColumns); } + + /** + * Returns false if any errors occurred during the last JSON decoding or + * if the user-specified path wasn't found in the array `$data` + */ + private function isJsonValid(?array $data): bool + { + if (json_last_error() !== JSON_ERROR_NONE) { + return false; + } + + if (!empty($this->path) && !$this->isValueOnPath($this->path, $data)) { + return false; + } + + return true; + } + + /** + * Checks if a value exists at the specified path in a nested array `$data`. + * + * This method takes a path string (in the form of '/key1/key2/.../keyN') and traverses + * the provided array `$data` to determine if the specified path exists. + * + * @param string $path The path to check, represented as a string with keys separated by slashes. + * @param array $data The associative array to search through. + * + * @return bool Returns true if the entire path exists in the data array + * false if any part of the path does not exist or if the value on the path isn't array + */ + private function isValueOnPath(string $path, array $data): bool + { + $pathParts = explode('/', trim($path, '/')); + + foreach ($pathParts as $pathPart) { + if (isset($data[$pathPart])) { + $data = $data[$pathPart]; + } else { + return false; + } + } + + if (!is_array($data)) { + return false; + } + + return true; + } + + /** + * Returns a value from the specified path in a nested array `$data`. + * Validation is done by the function `JsonFileInterpreter::isValueOnPath` + */ + private function getValueFromPath(string $path, array $data): array + { + $pathParts = explode('/', trim($path, '/')); + + foreach ($pathParts as $pathPart) { + $data = $data[$pathPart]; + } + + return $data; + } } diff --git a/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js b/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js index 63f0866a..3ecf6675 100644 --- a/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js +++ b/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js @@ -26,6 +26,12 @@ pimcore.plugin.pimcoreDataImporterBundle.configuration.components.interpreter.js }, border: false, items: [ + { + xtype: 'textfield', + fieldLabel: 'Path', + name: this.dataNamePrefix + 'path', + value: this.data.path || '', + } ] }); } From ab8ad903398af7e51fe963afb3f22fa51caf4fb7 Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Thu, 21 Nov 2024 19:28:56 -0500 Subject: [PATCH 2/6] use library --- composer.json | 1 + .../Interpreter/JsonFileInterpreter.php | 72 +++---------------- 2 files changed, 10 insertions(+), 63 deletions(-) diff --git a/composer.json b/composer.json index daff1cd7..ba8028c4 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "doctrine/dbal": "^2.12 || ^3.5", "dragonmantank/cron-expression": "^3.1", "league/flysystem-sftp-v3": "^3.0", + "mtdowling/jmespath.php": "^2.8", "nesbot/carbon": "^2.27", "phpoffice/phpspreadsheet": "^1.24 || ^2.2", "pimcore/compatibility-bridge-v10": "^1.0", diff --git a/src/DataSource/Interpreter/JsonFileInterpreter.php b/src/DataSource/Interpreter/JsonFileInterpreter.php index b587a651..6e0c3240 100644 --- a/src/DataSource/Interpreter/JsonFileInterpreter.php +++ b/src/DataSource/Interpreter/JsonFileInterpreter.php @@ -15,6 +15,7 @@ namespace Pimcore\Bundle\DataImporterBundle\DataSource\Interpreter; +use JmesPath; use Pimcore\Bundle\DataImporterBundle\PimcoreDataImporterBundle; use Pimcore\Bundle\DataImporterBundle\Preview\Model\PreviewData; @@ -97,18 +98,18 @@ public function fileValid(string $path, bool $originalFilename = false): bool $data = json_decode($this->prepareContent($content), true); - if (!$this->isJsonValid($data)) { + if (json_last_error() === JSON_ERROR_NONE) { + $this->cachedContent = $data; + $this->cachedFilePath = $path; + + return true; + } else { $this->applicationLogger->error('Reading file ERROR: ' . json_last_error_msg(), [ 'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $this->configName ]); return false; } - - $this->cachedContent = $data; - $this->cachedFilePath = $path; - - return true; } public function previewData(string $path, int $recordNumber = 0, array $mappedColumns = []): PreviewData @@ -140,66 +141,11 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo return new PreviewData($columns, $previewData, $readRecordNumber, $mappedColumns); } - /** - * Returns false if any errors occurred during the last JSON decoding or - * if the user-specified path wasn't found in the array `$data` - */ - private function isJsonValid(?array $data): bool - { - if (json_last_error() !== JSON_ERROR_NONE) { - return false; - } - - if (!empty($this->path) && !$this->isValueOnPath($this->path, $data)) { - return false; - } - - return true; - } - - /** - * Checks if a value exists at the specified path in a nested array `$data`. - * - * This method takes a path string (in the form of '/key1/key2/.../keyN') and traverses - * the provided array `$data` to determine if the specified path exists. - * - * @param string $path The path to check, represented as a string with keys separated by slashes. - * @param array $data The associative array to search through. - * - * @return bool Returns true if the entire path exists in the data array - * false if any part of the path does not exist or if the value on the path isn't array - */ - private function isValueOnPath(string $path, array $data): bool - { - $pathParts = explode('/', trim($path, '/')); - - foreach ($pathParts as $pathPart) { - if (isset($data[$pathPart])) { - $data = $data[$pathPart]; - } else { - return false; - } - } - - if (!is_array($data)) { - return false; - } - - return true; - } - /** * Returns a value from the specified path in a nested array `$data`. - * Validation is done by the function `JsonFileInterpreter::isValueOnPath` */ - private function getValueFromPath(string $path, array $data): array + private function getValueFromPath(string $path, array $data): mixed { - $pathParts = explode('/', trim($path, '/')); - - foreach ($pathParts as $pathPart) { - $data = $data[$pathPart]; - } - - return $data; + return JmesPath\Env::search($path, $data); } } From 1298362ea1a8c923c3c0f642ef46c336cb970594 Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Thu, 21 Nov 2024 19:30:28 -0500 Subject: [PATCH 3/6] don't pass path --- src/DataSource/Interpreter/JsonFileInterpreter.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/DataSource/Interpreter/JsonFileInterpreter.php b/src/DataSource/Interpreter/JsonFileInterpreter.php index 6e0c3240..e7f7c638 100644 --- a/src/DataSource/Interpreter/JsonFileInterpreter.php +++ b/src/DataSource/Interpreter/JsonFileInterpreter.php @@ -40,7 +40,7 @@ protected function loadData(string $path): array $data = json_decode($this->prepareContent($content), true); if (!empty($this->path)) { - return $this->getValueFromPath($this->path, $data); + return $this->getValueFromPath($data); } return $data; @@ -144,8 +144,8 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo /** * Returns a value from the specified path in a nested array `$data`. */ - private function getValueFromPath(string $path, array $data): mixed + private function getValueFromPath(array $data): mixed { - return JmesPath\Env::search($path, $data); + return JmesPath\Env::search($this->path, $data); } } From daf7074fd9a6e66d8d0f26e3dd2bc443d6287cb2 Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Thu, 21 Nov 2024 19:43:42 -0500 Subject: [PATCH 4/6] Translate --- src/DataSource/Interpreter/JsonFileInterpreter.php | 4 ++-- .../js/pimcore/configuration/components/interpreter/json.js | 2 +- src/Resources/translations/admin.en.yml | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/DataSource/Interpreter/JsonFileInterpreter.php b/src/DataSource/Interpreter/JsonFileInterpreter.php index e7f7c638..534c8561 100644 --- a/src/DataSource/Interpreter/JsonFileInterpreter.php +++ b/src/DataSource/Interpreter/JsonFileInterpreter.php @@ -15,7 +15,7 @@ namespace Pimcore\Bundle\DataImporterBundle\DataSource\Interpreter; -use JmesPath; +use JmesPath\Env as JmesPath; use Pimcore\Bundle\DataImporterBundle\PimcoreDataImporterBundle; use Pimcore\Bundle\DataImporterBundle\Preview\Model\PreviewData; @@ -146,6 +146,6 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo */ private function getValueFromPath(array $data): mixed { - return JmesPath\Env::search($this->path, $data); + return JmesPath::search($this->path, $data); } } diff --git a/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js b/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js index 3ecf6675..2d7ebc7d 100644 --- a/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js +++ b/src/Resources/public/js/pimcore/configuration/components/interpreter/json.js @@ -28,7 +28,7 @@ pimcore.plugin.pimcoreDataImporterBundle.configuration.components.interpreter.js items: [ { xtype: 'textfield', - fieldLabel: 'Path', + fieldLabel: t('plugin_pimcore_datahub_data_importer_configpanel_json_path'), name: this.dataNamePrefix + 'path', value: this.data.path || '', } diff --git a/src/Resources/translations/admin.en.yml b/src/Resources/translations/admin.en.yml index 918854e2..a401e568 100644 --- a/src/Resources/translations/admin.en.yml +++ b/src/Resources/translations/admin.en.yml @@ -45,6 +45,7 @@ plugin_pimcore_datahub_data_importer_configpanel_csv_escape: Escape plugin_pimcore_datahub_data_importer_configpanel_xlsx_sheet: Sheet plugin_pimcore_datahub_data_importer_configpanel_xml_xpath: XPath plugin_pimcore_datahub_data_importer_configpanel_xml_schema: Schema +plugin_pimcore_datahub_data_importer_configpanel_json_path: Path plugin_pimcore_datahub_data_importer_configpanel_import_settings: Import Settings plugin_pimcore_datahub_data_importer_configpanel_import_preview: Import Preview plugin_pimcore_datahub_data_importer_configpanel_preview_data_clone: Copy preview data from data source for preview. From db629cb54be10a0afb9d09a5189770dc38621bf3 Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Thu, 21 Nov 2024 19:52:25 -0500 Subject: [PATCH 5/6] fix cache --- .../Interpreter/JsonFileInterpreter.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/DataSource/Interpreter/JsonFileInterpreter.php b/src/DataSource/Interpreter/JsonFileInterpreter.php index 534c8561..41bdf00e 100644 --- a/src/DataSource/Interpreter/JsonFileInterpreter.php +++ b/src/DataSource/Interpreter/JsonFileInterpreter.php @@ -35,18 +35,17 @@ class JsonFileInterpreter extends AbstractInterpreter protected function loadData(string $path): array { - if ($this->cachedFilePath === $path && !empty($this->cachedContent)) { + if ($this->cachedFilePath !== $path || empty($this->cachedContent)) { $content = file_get_contents($path); $data = json_decode($this->prepareContent($content), true); - - if (!empty($this->path)) { - return $this->getValueFromPath($data); - } - - return $data; } else { - return $this->cachedContent; + $data = $this->cachedContent; + } + + if (!empty($this->path)) { + return $this->getValueFromPath($data); } + return $data; } protected function doInterpretFileAndCallProcessRow(string $path): void From 43b8400a893c0c8d3e34d372d203a15cdea7d3c1 Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Thu, 21 Nov 2024 19:57:40 -0500 Subject: [PATCH 6/6] cut dupe code --- src/DataSource/Interpreter/JsonFileInterpreter.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/DataSource/Interpreter/JsonFileInterpreter.php b/src/DataSource/Interpreter/JsonFileInterpreter.php index 41bdf00e..ff53c2ee 100644 --- a/src/DataSource/Interpreter/JsonFileInterpreter.php +++ b/src/DataSource/Interpreter/JsonFileInterpreter.php @@ -33,11 +33,15 @@ class JsonFileInterpreter extends AbstractInterpreter */ protected $cachedFilePath = null; + protected function loadDataRaw(string $path): array { + $content = file_get_contents($path); + return json_decode($this->prepareContent($content), true); + } + protected function loadData(string $path): array { if ($this->cachedFilePath !== $path || empty($this->cachedContent)) { - $content = file_get_contents($path); - $data = json_decode($this->prepareContent($content), true); + $data = $this->loadDataRaw($path); } else { $data = $this->cachedContent; } @@ -93,9 +97,7 @@ public function fileValid(string $path, bool $originalFilename = false): bool } } - $content = file_get_contents($path); - - $data = json_decode($this->prepareContent($content), true); + $data = $this->loadDataRaw($path); if (json_last_error() === JSON_ERROR_NONE) { $this->cachedContent = $data;